Java找不到文件?
嗨我想设置一个扫描仪来打印出文本文件的内容。这里是我的代码:Java找不到文件?
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();
}
}
}
我已经在项目中创建了一个源文件夹,并将文本文件放在那里。不过,我不断收到此错误:
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)
您可以根据System.out.println(System.getProperty("user.dir"));
看到什么文件夹中的Java默认情况下,该文件看。
如果文件不存在,则必须指定文件的完整路径。
嗨,这是祸不单行,我发现路径为: c :\ Users \ Mara \ workspace \ ReadFile \ RSource \ CardNative.java.txt但是当我把File file = new File(“c:\ Users \ Mara \ workspace \ ReadFile \ RSource \ CardNative.java.txt”); c:\下划线为有错误: 无效转义序列(有效的转义序列为\ b \ t \ n \ f \ r \“\'\\ – mark16 2014-09-26 09:38:46
您需要将”\“更改为”\\“路径 – qbit 2014-09-26 09:42:06
感谢排序的问题! – mark16 2014-09-26 09:43:49
您需要确保您的文件放置在您运行程序的同一目录中。尝试在主函数的顶部添加它,查看当前目录是什么,以及如果您的文件实际上位于该目录中:
System.out.println(System.getProperty(“user.dir”));
我发现文件路径为c:\ Users \ Mara \ workspace \ ReadFile \ RSource \ CardNative.java.txt,但是当我将File file = new File(“c:\ Users \ Mara \ workspace \ ReadFile \ RSource \ CardNative.java.txt“); c:\下划线为有错误:无效的转义序列(有效的转义序列是\ b \ t \ n \ f \ r \“\'\\ – mark16 2014-09-26 09:40:52
new File(String pathname);
您正在使用的coinstructor将filepath作为参数,绝对或相对。如果它是相对的,它将是执行路径/ your_string。 所以你应该把文件放到编译后的.jar文件所在的文件夹中。
File file1 = new File("text.txt");
File file2 = new File("D:/documents/test.txt");
如果PROGRAMM选自C执行:/programms/myprj.jar,所以 文件1将打开 “C:/programms/test.txt” 和file2将打开“d:/documents/test.txt “独立于执行路径。
http://docs.oracle.com/javase/7/docs/api/java/io/File.html#File(java.lang.String)
我张贴我的评论的答案,但我不允许发表评论,因为我没有足够的声誉。正如在你的评论中,你在文件路径中使用反斜线。而是使用双斜杠\或一个前进/。例如C:/java/file.txt 你应该为它提供正确和实际的文件路径,或者确保文件位于源代码的位置。
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();
}
}
}
c:/现在下划线为有错误:转义序列无效(有效的是\ b \ t \ n \ f \ r \“\'\\) – mark16 2014-09-26 09:41:51
这应该有效。
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();
}
}
我怀疑,你只需要写'CardNative.java' – 2014-09-26 09:22:20
你想加载到类路径上的文件是? – imrichardcole 2014-09-26 09:22:47
不,它实际上是一个文本文件,全称是CardNative.java.txt,这听起来有点尴尬,但是作为imrichardcole的全名 – mark16 2014-09-26 09:23:35