File I/O problem

Discussion in 'Java' started by ChrisW, Feb 7, 2007.

  1. ChrisW

    ChrisW Guest

    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 :)
    ChrisW, Feb 7, 2007
    #1
    1. Advertising

  2. On 7 Feb 2007 08:20:41 -0800, ChrisW wrote:
    > 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

    --
    [ 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
    Gordon Beaton, Feb 7, 2007
    #2
    1. Advertising

  3. On Feb 8, 3:20 am, "ChrisW" <> wrote:
    > 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.
    Andrew Thompson, Feb 7, 2007
    #3
  4. ChrisW

    ChrisW Guest

    On Feb 7, 4:53 pm, "Andrew Thompson" <> wrote:
    > On Feb 8, 3:20 am, "ChrisW" <> wrote:
    >
    > > 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.
    ChrisW, Feb 7, 2007
    #4
  5. On 7 Feb 2007 09:35:24 -0800, ChrisW wrote:
    > 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

    --
    [ 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
    Gordon Beaton, Feb 7, 2007
    #5
  6. ChrisW

    ChrisW Guest

    On Feb 7, 6:00 pm, Gordon Beaton <> wrote:
    > On 7 Feb 2007 09:35:24 -0800, ChrisW wrote:
    >
    > > 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
    >
    > --
    > [ 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
    ChrisW, Feb 7, 2007
    #6
  7. On 7 Feb 2007 10:18:27 -0800, ChrisW wrote:
    > 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

    --
    [ 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
    Gordon Beaton, Feb 7, 2007
    #7
  8. ChrisW

    ChrisW Guest

    On Feb 7, 6:34 pm, Gordon Beaton <> wrote:
    > On 7 Feb 2007 10:18:27 -0800, ChrisW wrote:
    >
    > > 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
    >
    > --
    > [ 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...
    ChrisW, Feb 7, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luis Esteban Valencia Muñoz
    Replies:
    3
    Views:
    1,392
    Scott Allen
    Jun 4, 2005
  2. Dave
    Replies:
    1
    Views:
    978
    Juan T. Llibre
    Jun 8, 2007
  3. =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki

    Reading of file by next of map file and by next of file descriptor.

    =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki, Jul 10, 2007, in forum: C++
    Replies:
    1
    Views:
    779
    Alf P. Steinbach
    Jul 10, 2007
  4. scad
    Replies:
    4
    Views:
    942
    James Kanze
    May 28, 2009
  5. Ludwigi Beethoven
    Replies:
    5
    Views:
    313
    Mike Hall
    Jul 26, 2003
Loading...

Share This Page