java.io.FileNotFoundException:in.txt,(系统找不到指定的文件)
我收到以下错误
java.io.FileNotFoundException: in.txt, (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(Unknown Source) at java.io.FileInputStream.(Unknown Source) at java.io.FileReader.(Unknown Source) at FileTest.main(FileTest.java:50)
我确定我已经在src,bin和root目录下创建了一个in.txt文件。 我也尝试在主参数中指定完整目录,但仍然无法正常工作。 为什么Eclipse不接受它?
import java.io.*; public class FileTest { public static void main(String[] args) { try { String inFileName = args[0]; String outFileName = args[1]; BufferedReader ins = new BufferedReader(new FileReader(inFileName)); BufferedReader con = new BufferedReader(new InputStreamReader(System.in)); PrintWriter outs = new PrintWriter(new FileWriter(outFileName)); String first = ins.readLine(); // read from file while (first != null) { System.out.print("Type in a word to follow " + first + ":"); String second = con.readLine(); // read from console // append and write outs.println(first + ", " + second); first = ins.readLine(); // read from file } ins.close(); outs.close(); } catch (IOException ex) { ex.printStackTrace(System.err); System.exit(1); } } }
我拿了你的代码并使用以下命令行参数执行它:
in.txt out.txt
它完全没有问题。 检查命令行。
鉴于错误消息,我猜Java正在寻找文件名in.txt,
,并带有一个逗号。
默认情况下,Eclipse会将工作目录设置为项目文件夹。 如果您对设置进行了更改,您仍然可以通过以下简单的代码行找到工作目录:
System.out.println(new java.io.File("").getAbsolutePath());
把你的文本文件放在打印的文件夹中,你应该没问题。
将它放在项目目录中,如果不起作用,则为bin文件夹
打开“调试配置”并在“参数”选项卡下设置工作目录。 相对路径将相对于工作目录。
我在我的eclipse中创建了一个新项目Learning
,并将in.txt
文件放在源目录中。 我使用示例java文件进行了测试。
public static void main(String[] args) { File f = new File("a.txt"); System.out.println(f.getAbsolutePath()); }
输出:
/home/bharathi/workspace/Learning/a.txt
它查找项目根目录。 您可以将in.txt放在项目的根目录中,或者将路径提供到源目录。
可能有两个问题:
-
在您的控制台中,错误显示为’,’check – >
java.io.FileNotFoundException:in.txt,
所以JVM可能会查找带有’,’的文件名 -
第二种情况可能是,你in.txt文件不在正确的位置。 检查它是否是项目的根目录。 或主要道路。
这将解决这些类型的问题。