Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

File I/O problem

Thread view: 
ChrisW - 07 Feb 2007 16:20 GMT
I'm trying to write a basic stats analysis program that analyses every
file in a folder. I can get a loop to go through the relevant files,
but I can't seem to use the current file name as an argument in the
File() method - I get a complile time error that basically says I need
a type string, not a type file:

This works:

for (int fileNumber = 0; fileNumber < listOfFiles.length; fileNumber+
+) {
        if (listOfFiles[fileNumber].isFile()) {

            System.out.println(listOfFiles[fileNumber]);  //This prints the
current file name and does work

            File dataFile =  new File("file1.csv");

This doesn't work:

for (int fileNumber = 0; fileNumber < listOfFiles.length; fileNumber+
+) {
        if (listOfFiles[fileNumber].isFile()) {

            System.out.println(listOfFiles[fileNumber]);  //This prints the
current file name and does work

            File fileName = listOfFiles[fileNumber];

            File dataFile =  new File(fileName);         //This doeesn't work

(I've also just tried File dataFile =  new
File(listOfFiles[fileNumber]);

If anyone can throw any light on the problem I'd be most grateful :)
Gordon Beaton - 07 Feb 2007 16:49 GMT
> I'm trying to write a basic stats analysis program that analyses
> every file in a folder. I can get a loop to go through the relevant
> files, but I can't seem to use the current file name as an argument
> in the File() method - I get a complile time error that basically
> says I need a type string, not a type file:

Each of the elements in "listOfFiles" is *already* a File object, you
don't need to do "new File()" to get a File from it. You can use
listOfFiles[i] either directly, or after assigning it to dataFile.

/gordon

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

Andrew Thompson - 07 Feb 2007 16:53 GMT
> I'm trying to write a basic stats analysis program that analyses every
> file in a folder. I can get a loop to go through the relevant files,
> but I can't seem to use the current file name as an argument in the
> File() method - I get a complile time error that basically says I need
> a type string, not a type file:
...
>                         File dataFile =  new File(fileName);         //This doeesn't work
>
> (I've also just tried File dataFile =  new
> File(listOfFiles[fileNumber]);
>
> If anyone can throw any light on the problem I'd be most grateful

Try consulting the JavaDocs, rather than
just typing statements at random..
<http://java.sun.com/javase/6/docs/api/java/io/
File.html#constructor_summary>

Andrew T.
ChrisW - 07 Feb 2007 17:35 GMT
> > I'm trying to write a basic stats analysis program that analyses every
> > file in a folder. I can get a loop to go through the relevant files,
[quoted text clipped - 15 lines]
>
> Andrew T.

I started by reading the JavaDocs, got stuck, which is why I asked
here.  I didn't understand why it said that a string had to be the
argument.  All the tutorials about Java say that the best why to learn
is to play around to try and understand what's going on, hence why I
was writing "statements at random" (although I thought everything I
was writing seemed relatively logical).  If you've got any better
learning methods I'd be happy to hear them :)

I've now got it to read in 1 file, using

String fileName = listOfFiles[fileNumber].getName();
FileReader csvData = new FileReader (fileName);

but I get a FileNotFoundException when it's trying to read in the 2nd
file (which it must actually know exists - when I print out a list of
files it gets printed!) - I've looked to see if you need to use a
close() or destroy() method on the FIleReader, but it doesn't seem to
make any difference.
Gordon Beaton - 07 Feb 2007 18:00 GMT
> I've now got it to read in 1 file, using
>
[quoted text clipped - 6 lines]
> close() or destroy() method on the FIleReader, but it doesn't seem to
> make any difference.

You should always close the FileReader when you're finished reading
the file, but this isn't likely the cause of the
FileNotFoundException.

Does the filename appear odd in any way? Does it (the name) contain
any unusual characters?

There's really no need to extract the filename from the File, you can
use the File object directly to create the FileReader:

 FileReader csvData = new FileReader(listOfFiles[fileNumber]);

However it's usually a better idea to read text files in the following
manner, which lets you specify the correct encoding to use when
converting from byte to char:

 FileInputStream fis = new FileInputStream(listOfFiles[fileNumber]);
 InputStreamReader isr = new InputStreamReader(fis, "ISO-8859-1");

...then read from isr. Note that ISO-8859-1 here is just an example,
you should choose the appropriate charset name for your files.

/gordon

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

ChrisW - 07 Feb 2007 18:18 GMT
> > I've now got it to read in 1 file, using
>
[quoted text clipped - 34 lines]
> [ don't email me support questions or followups ]
> g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

Gordan,

Thanks - the FileInputStream method has got it working :) (there were
no unusual characters in the filename).  All I've got to do now is try
and understand *why* it works! (Since I'm only reading in characters
from a text file I'd have thought FileReader was better - the API says
"FileInputStream is meant for reading streams of raw bytes such as
image data. For reading streams of characters, consider using
FileReader.")

Chris
Gordon Beaton - 07 Feb 2007 18:34 GMT
> Thanks - the FileInputStream method has got it working :) (there were
> no unusual characters in the filename).  All I've got to do now is try
[quoted text clipped - 3 lines]
> image data. For reading streams of characters, consider using
> FileReader.")

Well yes. But

 new FileReader(file);

is equivalent to

 new InputStreamReader(new FileInputStream(file));

...so that part shouldn't have made any difference to your
FileNotFound problem. I recommended the latter only because it's
better style to specify the charset name like I suggested in my
previous post, which you can't do when you use FileReader, i.e.:

 new InputStreamReader(new FileInputStream(file, charset));

That your program works now is more likely due to not calling
File.getName() in order to open the FileReader/FileInputStream. When
you used File.getName() you lost information about the path to the
file. Unless you start your program in the directory containing the
files, this use of getName() will fail.

/gordon

Signature

[ don't email me support questions or followups ]
g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

ChrisW - 07 Feb 2007 18:58 GMT
> > Thanks - the FileInputStream method has got it working :) (there were
> > no unusual characters in the filename).  All I've got to do now is try
[quoted text clipped - 30 lines]
> [ don't email me support questions or followups ]
> g o r d o n  +  n e w s  @  b a l d e r 1 3 . s e

My program is in a different folder to my data and I did wonder if
that was the problem - although right at the beginning of the program
(outside the for loop), I did have a statement

File folder = new File("f:/data/files");

When I printed folder in my for loop it always did (and still does)
print the correct location which was confusing me (especially since it
was working for the 1st file)! If it's a technicality of the getName()
method though I'll bear that in mind for future use...


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.