简体   繁体   中英

Reading a line from a text file

I am trying to read a line from a text file, but the program keeps returning an error stating that the file's name cannot be located. Any ideas on how to solve the problem.

Source code:

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

public class Cipher {

    public String file_name;

    public Cipher(){
        file_name = "/Users/SubrataMohanty/IdeaProjects/CaesarCipher/src/cipher_text.txt";

    }

    public static void main(String[] args) {

        BufferedReader br = null;
        FileReader fr = null;

        Cipher cipher_1 = new Cipher();


        fr = new FileReader(cipher_1.file_name);
        br = new BufferedReader(fr);

        String current_line;

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

        }

    }

Upon debugging this is what I get,

Error:(25, 14) java: unreported exception java.io.FileNotFoundException; must be caught or declared to be thrown
Error:(30, 43) java: unreported exception java.io.IOException; must be caught or declared to be thrown

The above two lines are where :

  1. Variable fr is initialized.
  2. The while loop.

You are getting these errors because the methods and constructors you are calling throw exceptions. These either need to be caught with a try/catch block or be declared in the method signature.

These errors are compile time errors, not runtime. It's not saying that the file doesn't exist, but that you need to catch an exception just in case that is true.

Oracle Tutorial

Please Enter the complete path that is the Drive along with the folder location.

C:\....\Users/SubrataMohanty/IdeaProjects/CaesarCipher/src/cipher_text.txt

Like this. It should be like when you copy paste in the explorer you can jump to the file directly.

If using MAC then, right click on the text file and properties and copy the location and paste it in your code.

In your code, below lines need to catch

  fr = new FileReader(cipher_1.file_name);
  br = new BufferedReader(fr);

Use try-catch block or throws Exception to handle it.

try{  
        fr = new FileReader(cipher_1.file_name);
        br = new BufferedReader(fr);

        String current_line;

        while ((current_line = br.readLine()) != null){
            System.out.println(current_line);
}catch(Exception e)
        e.printStackTrace();
{

You need to handle the exceptions generated by your reader

Your file path should include the entire path for example:

"C:\\Users\\John Doe\\Desktop\\Impactor_0.9.41.txt"

Notice I used an extra '\\' but I'm not sure if that matters, however I always do that.

Also for clarity you could also change your br and fr like this, however what you did is fine as well. But it is important to do the opening of files in a try-catch block like this:

try{
    br = new BufferedReader(new FileReader(cipher1.file_name));
} catch(FileNotFoundException e){
    e.printStackTrace();
}

Also when reading and printing out file to console, put it in try catch:

try{
    String current_line;
    while((current_line = br.readLine()) != null){
        System.out.println(current_line);
        current_line = br.readLine();
    }
} catch(IOException e){
    e.printStackTrace();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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