繁体   English   中英

在Eclipse中读取Java中的.txt文件

[英]Reading a .txt file in java in eclipse

我正在尝试从计算机中读取文件,但显示为java.io.FileNotFoundException(系统找不到指定的文件)

System.out.println("READING FILE"); 


    File file = new File("Testing.txt"); //Reading file from E
    FileInputStream in = null;
    BufferedInputStream buff = null;
    DataInputStream data= null;

    String Line=""; // declare a string 
    try
    {

        in = new FileInputStream(file); // pick up the file

        buff = new BufferedInputStream(in);
        data = new DataInputStream(buff);

        while (data.available() != 0) 
        { // Read the file line by line till it reaches the end of file

            Line=Line+data; // concatenate line into string

            System.out.println(data.readLine()); // print line by line 
        }

    } catch (IOException e)

    {
        e.printStackTrace();
    }
File file = new File("E:\\Testing.txt"); 

如果您尝试访问E,则这是实现此目的的方法之一

如果您使用Windows,请尝试:

File file = new File("e:/Testing.txt");

(这意味着:使用斜杠/代替反斜杠\\)

您需要:

  • 指定文件的完整路径,或...
  • 在项目的运行设置中设置应用程序的默认运行目录,或...
  • 将文件放在类路径上并作为资源读取

Testing.txt的路径在哪里?

File file = new File("Testing.txt"); //Reading file from E

在这里,您正在从src路径中读取java,如果文件位于src中,则应修改路径

File file = new File("src/Testing.txt"); //Reading file from E

另一个解决方案是输入文件的完整路径

指定文件的完整路径即可。

给出完整路径而不是相对路径

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM