简体   繁体   English

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

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

I am trying to read a file from my computer but it says java.io.FileNotFoundException (The system cannot find the file specified) 我正在尝试从计算机中读取文件,但显示为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,则这是实现此目的的方法之一

if you use windows try: 如果您使用Windows,请尝试:

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

(that means: use slash / instead of backslash \\ ) (这意味着:使用斜杠/代替反斜杠\\)

You need to either: 您需要:

  • Specify the complete path to the file, or... 指定文件的完整路径,或...
  • Set the app's default run directory in the project's run settings, or... 在项目的运行设置中设置应用程序的默认运行目录,或...
  • Put the file on the classpath and read it as a resource 将文件放在类路径上并作为资源读取

Where is the path of Testing.txt ??? Testing.txt的路径在哪里?

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

Here java You are reading from out of src path if the file is in src you should modify the path 在这里,您正在从src路径中读取java,如果文件位于src中,则应修改路径

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

And another solution is enter the full path of file 另一个解决方案是输入文件的完整路径

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

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

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

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