Results 1 to 5 of 5
  1. #1
    erin.ctm is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default Exception in thread "main" java.io.FileNotFoundException: reverse.dat (No such file o

    Hello!
    I am doing homework for a college class. Any ideas on why I might be getting this error. I get them alot and am not sure if its the directory I told Eclipse to use or what. I'm confused about. I appreciate any tips too.
    Exception in thread "main" java.io.FileNotFoundException: reverse.dat (No such file or directory)

    Here's my code so far. I'm pretty positive I'm doing something else wrong besides whatever is causing the exception error:

    //Application Reverse reads numbers into an array and prints them out in reverse order.
    import java.util.Scanner;
    import java.io.*;
    public class Reverse
    {
    public static void main(String[] args) throws IOException
    {
    final int MAX = 10;
    Scanner inFile = new Scanner(new FileReader("reverse.dat"));
    PrintWriter outFile;
    outFile = new PrintWriter(new FileWriter("outfile.dat"));

    int[] numbers;
    numbers = new int[MAX];

    int value;
    int index;
    for (index = 0; index < numbers.length; index++)
    numbers[index] = inFile.nextInt();
    for (index = 0; index < numbers.length; index++)
    outFile.println(numbers[index]);
    {
    // FILL IN Code to read value
    System.out.println("Please enter ten numbers.");
    numbers[1]= inFile.nextInt();
    numbers[2]= inFile.nextInt();
    numbers[3]= inFile.nextInt();
    numbers[4]= inFile.nextInt();
    numbers[5]= inFile.nextInt();
    numbers[6]= inFile.nextInt();
    numbers[7]= inFile.nextInt();
    numbers[8]= inFile.nextInt();
    numbers[9]= inFile.nextInt();
    }
    // FILL IN Code to store value into numbers

    for (index = MAX - 1; index >= 0; index--)
    // FILL IN Code to write numbers on the screen
    System.out.println("The list in reverse order is: " + numbers[index]);

    inFile.close();
    outFile.close();
    }
    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Simply read the error. The file "reverse.dat" does not exist. Where is the file located relative to the calling code?

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Java Code:
    Scanner inFile = new Scanner(new FileReader("reverse.dat"));

    You could break this step up into three parts:

    * create a File instance
    * create a FileReader instance using the file
    * create a Scanner instance using the file reader

    The reason for doing this is that the File class has methods that will enable you to get the full name of the file you are trying to use. And you can print this out with System.out.println() to see where it is your program is looking for files.

  4. #4
    erin.ctm is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default

    Simply read the error. The file "reverse.dat" does not exist. Where is the file located relative to the calling code?

    This is what is confusing me. I have no clue. I have this problem with all .dat files in my code. I know I missed some critical piece but don't even know what to call it to look it up :-)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by erin.ctm View Post
    This is what is confusing me. I have no clue. I have this problem with all .dat files in my code. I know I missed some critical piece but don't even know what to call it to look it up :-)

    Have you tried pbrockway's suggestions yet?

    Specifically:
    Java Code:
    import java.io.File;
    
    public class Fu1 {
       public static void main(String[] args) {
          File file = new File("reverse.dat");
          System.out.println(file.getAbsolutePath());
          System.out.println("user.dir is: " + System.getProperty("user.dir"));
       }
    }
    Last edited by Fubarable; 11-06-2010 at 12:54 AM.

Similar Threads

  1. Replies: 5
    Last Post: 06-22-2010, 04:51 PM
  2. Replies: 1
    Last Post: 11-20-2009, 08:58 PM
  3. Replies: 6
    Last Post: 07-16-2009, 04:30 PM
  4. Replies: 1
    Last Post: 07-16-2009, 11:35 AM
  5. Replies: 1
    Last Post: 08-07-2007, 08:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •