Results 1 to 5 of 5
- 11-05-2010, 09:56 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
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();
}
}
- 11-05-2010, 10:02 PM #2
Simply read the error. The file "reverse.dat" does not exist. Where is the file located relative to the calling code?
- 11-05-2010, 10:05 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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.
- 11-05-2010, 11:47 PM #4
Member
- Join Date
- Oct 2010
- Posts
- 10
- Rep Power
- 0
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 :-)
-
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-05-2010 at 11:54 PM.
Similar Threads
-
Question about error "Exception in thread "main" java.lang.NoSuchMethodError: main
By ferdzz in forum New To JavaReplies: 5Last Post: 06-22-2010, 03:51 PM -
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11-20-2009, 07:58 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 6Last Post: 07-16-2009, 03:30 PM -
Exception in thread "main" java.lang.NullPointerException at LinkedList.main(Link
By kavitha_0821 in forum New To JavaReplies: 1Last Post: 07-16-2009, 10:35 AM -
ERROR: Exception in thread "main" java.lang.NoSuchMethodError: main
By barney in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:10 AM


LinkBack URL
About LinkBacks


Bookmarks