File I/O problem

C

ChrisW

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 :)
 
G

Gordon Beaton

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 either directly, or after assigning it to dataFile.

/gordon
 
A

Andrew Thompson

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.
 
C

ChrisW

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.

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.
 
G

Gordon Beaton

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.

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
 
C

ChrisW

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.

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

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
 
G

Gordon Beaton

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.")

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
 
C

ChrisW

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

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top