Reputation: 25
Hi I am trying to set up a scanner to print out contents of a text file. Here is my code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ScannerReadFile {
public static void main(String[] args) {
// Location of file to read
File file = new File("CardNative.java.txt");
try
{
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine())
{
String line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
}
I have created a source folder in the project and put the text file in there. However I keep getting this error:
java.io.FileNotFoundException: CardNative.java.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.util.Scanner.<init>(Scanner.java:636)
at ScannerReadFile.main(ScannerReadFile.java:14)
Upvotes: 1
Views: 12438
Reputation: 1154
This should work.
public static void main(String[] args) {
Scanner scanner = new Scanner(ScannerReadFile.class.getResourceAsStream("CardNative.java.txt"));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
}
}
Upvotes: 1
Reputation: 734
I was posting my answer in comment but I am not allowed to comment because I have no enough reputations. As in your comment, you are using back slashes in the file path. Instead use double back slashes \ OR one forward /. eg C:/java/file.txt You should provide it the right and actual path of the file OR make sure that the file is lying there where your source is.
public class ScannerReadFile {
public static void main(String[] args) {
// Location of file to read
File file = new File("C:/Users/EastCorporation/Desktop/CardNative.java.txt");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Upvotes: 0
Reputation: 1398
new File(String pathname);
The coinstructor you are using takes filepath as an argument, absolute or relative. If it is relative, it will be the execute path/your_string. So you should put the file to the same folder as the compiled .jar file.
File file1 = new File("text.txt");
File file2 = new File("D:/documents/test.txt");
If the programm is executing from C:/programms/myprj.jar, so file1 will open "C:/programms/test.txt" and file2 will open "D:/documents/test.txt" independently of the executing path.
http://docs.oracle.com/javase/7/docs/api/java/io/File.html#File(java.lang.String)
Upvotes: 0
Reputation: 390
You need to make sure that your file is placed in the same directory from where you're running your program. Try adding this at the top of your main function, to see what's the current directory, and if your file is actually in that directory:
System.out.println(System.getProperty("user.dir"));
Upvotes: 0
Reputation: 2664
You can use use System.out.println(System.getProperty("user.dir"));
to see on what folder Java is looking by default for the file.
If the file doesn't exist there you have to specify the entire path to the file.
Upvotes: 7