How to open a text file?

…衆ロ難τιáo~ 提交于 2019-12-01 14:45:31

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.

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.

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".

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:

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.

2) Can be read by the application

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.

More informations:

Javadoc of File

Informations about Path in computing

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();
      }
   }
}
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/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!