简体   繁体   中英

Read file name from user input on linux terminal - JAVA

I want to make a little script in JAVA to receive a file name in the linux terminal and read that file.

This is what i'm trying:

import java.util.Scanner;
import java.io.*;

class ItauScript {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        System.out.println("Filename: ");
        String fileName = reader.next();

        FileReader fileReader = new FileReader(fileName);
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        System.out.println(bufferedReader.readLine());
    }
}

But the code doesn't compile. I get this error message:

hello.java:10: error: unreported exception FileNotFoundException; must be caught or declared to be thrown FileReader fileReader = new FileReader(fileName); ^ hello.java:13: error: unreported exception IOException; must be caught or declared to be thrown System.out.println(bufferedReader.readLine());

I can open the file if i put it on hardcode on a string. But i need to receive it as an input from the terminal.

What am i missing?

FileNotFoundException is a checked Exception (as is the parent class IOException thrown by readLine ), modify main to re-throw 1 it like

public static void main(String[] args) throws IOException {

or surround it with a try-catch (with resources) like

try (FileReader fileReader = new FileReader(fileName);
        BufferedReader bufferedReader = new BufferedReader(fileReader)) {
    System.out.println(bufferedReader.readLine());
} catch (IOException e) {
    e.printStackTrace();
}

1 But you should still close the bufferedReader in a finally .

You need to handle the possible exception. You can specify that the enclosing method main throws the exception, but it would be better to handle it yourself.

import java.util.Scanner;
import java.io.*;

class ItauScript {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        try
        {
            System.out.println("Filename: ");
            String fileName = reader.next();

            FileReader fileReader = new FileReader(fileName);
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            System.out.println(bufferedReader.readLine());
        }
        catch(IOException e)
        {
            e.printStackTrace();
            //TODO handle error
            return;
        }
    }
}

Try:

import java.util.Scanner;
import java.io.*;

class ItauScript {
public static void main(String[] args) {
    Scanner reader = new Scanner(System.in);
    System.out.println("Filename: ");
    String fileName = reader.next();

    try {
       FileReader fileReader = new FileReader(fileName);
       BufferedReader bufferedReader = new BufferedReader(fileReader);

       System.out.println(bufferedReader.readLine());
    } catch (IOException e) {
        // handle exception (if any) here
    }
}
}

And as others suggested, it's very helpful to read what the IDE/Compiler tells you in case of errors ...

Hope that helps

Every time you want to access a file you need to handle the IOException. There are many ways to do that. First one is to use a try-catch block .

import java.util.Scanner;
import java.io.*;

class ItauScript {
    public static void main(String[] args) {
        try {
            Scanner reader = new Scanner(System.in);
            System.out.println("Filename: ");
            String fileName = reader.next();
            FileReader fileReader = new FileReader(fileName);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            System.out.println(bufferedReader.readLine());

            bufferedReader.close();
            fileReader.close();
            reader.close();
       }catch(IOException e){
            System.err.println("Exception");
            e.printStackTrace();
       }
    }
}

Second way is to use the keyword throws

import java.util.Scanner;
import java.io.*;

class ItauScript {
    public static void main(String[] args) throws IOException {
        Scanner reader = new Scanner(System.in);
        System.out.println("Filename: ");
        String fileName = reader.next();
        FileReader fileReader = new FileReader(fileName);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        System.out.println(bufferedReader.readLine());

        bufferedReader.close();
        fileReader.close();
        reader.close();
    }
}

The drawback of throws is that you can't use the IOException as you want. Also you may need to catch the IOException in the function that called the one that produces the exception. For instance:

import java.util.Scanner;
import java.io.*;

class ItauScript {

    public static void test() throws IOException {
        Scanner reader = new Scanner(System.in);
        System.out.println("Filename: ");
        String fileName = reader.next();
        FileReader fileReader = new FileReader(fileName);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        System.out.println(bufferedReader.readLine());

        bufferedReader.close();
        fileReader.close();
        reader.close();
    }
    
    public static void main(String[] args) throws IOException {
        test();
    }
}

PS Please don't forget to use .close() to your streams.

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