Java 如何打开文本文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18551251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:59:15  来源:igfitidea点击:

How to open a text file?

javatext-filesnotepad

提问by Naomi Harris

I can't figure out for the life of me what is wrong with this program:

我一生都无法弄清楚这个程序有什么问题:

     import java.io.*;

     public class EncyptionAssignment 
     {
         public static void main (String[] args) throws IOException
         {
             String line;
             BufferedReader in;

             in = new BufferedReader(new FileReader("notepad encypt.me.txt"));
             line = in.readLine();

             while(line != null)
             {
                    System.out.println(line);
                    line = in.readLine();
             }

             System.out.println(line);
         }

    }

The error message says that the file can't be found, but I know that the file already exists. Do I need to save the file in a special folder?

错误消息说找不到该文件,但我知道该文件已存在。我需要将文件保存在特殊文件夹中吗?

回答by Vicky Thakor

package com.mkyong.io;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderExample {

    public static void main(String[] args) {

        BufferedReader br = null;

        try {

            String sCurrentLine;

            br = new BufferedReader(new FileReader("C:\testing.txt"));

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }
}

Reference: http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/

参考:http: //www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/

回答by Julien

The error is "notepad encypt.me.txt". Since your file is named "encypt.me.txt", you can't put a "notepad" in front of its name. Moreover, the file named "notepad encypt.me.txt" probably didn't exist or is not the one that you want to open.

错误是"notepad encypt.me.txt"。由于您的文件名为“encypt.me.txt”,因此您不能在其名称前放置“记事本”。此外,名为“notepad encypt.me.txt”的文件可能不存在或不是您要打开的文件。

Additionally, you have to provide the path ( absolute or relative ) of your file if it's not located in your project folder.

此外,如果文件不在项目文件夹中,则必须提供文件的路径(绝对或相对)。

I will take the hypothesis that your are on a Microsoft Windows system.

我将假设您使用的是 Microsoft Windows 系统。

If your file has as absolute path of "C:\foo\bar\encypt.me.txt", you will have to pass it as "C:\\foo\\bar\\encypt.me.txt"or as "C:"+File.separatorChar+"foo"+File.separatorChar+"bar"+File.separatorChar+encypt.me.txt".

如果您的文件的绝对路径为“C:\foo\bar\encypt.me.txt”,则必须将其作为"C:\\foo\\bar\\encypt.me.txt"或作为"C:"+File.separatorChar+"foo"+File.separatorChar+"bar"+File.separatorChar+encypt.me.txt".

If it's still not working, you should verify that the file :

如果它仍然不起作用,您应该验证该文件:

1) Exist at the path provided. You can do it by using the following piece of code:

1) 存在于提供的路径中。您可以使用以下代码段来完成此操作:

File encyptFile=new File("C:\foo\bar\encypt.me.txt");
System.out.println(encyptFile.exists());

If the path provided is the right one, it should be at true.

如果提供的路径是正确的,则它应该为 true。

2) Can be read by the application

2)可以被应用程序读取

You can do it by using the following piece of code:

您可以使用以下代码段来完成此操作:

File encyptFile=new File("C:\foo\bar\encypt.me.txt");
System.out.println(encyptFile.canRead());

If you have the permission to read the file, it should be at true.

如果您有读取文件的权限,它应该为 true。

More informations:

更多信息:

Javadoc of File

文件的Javadoc

Informations about Path in computing

关于计算路径的信息

回答by Jitendra

import java.io.*;

public class Test {
    public static void main(String [] args) {

    // The name of the file to open.
    String fileName = "temp.txt";

    // This will reference one line at a time
    String line = null;

    try {
        // FileReader reads text files in the default encoding.
        FileReader fileReader = 
            new FileReader(fileName);

        // Always wrap FileReader in BufferedReader.
        BufferedReader bufferedReader = 
            new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }   

        // Always close files.
        bufferedReader.close();         
    }
    catch(FileNotFoundException ex) {
        System.out.println(
            "Unable to open file '" + 
            fileName + "'");                
    }
    catch(IOException ex) {
        System.out.println(
            "Error reading file '" 
            + fileName + "'");                  
        // Or we could just do this: 
        // ex.printStackTrace();
      }
   }
}