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?

Answers


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


FileNotFoundException is a checked Exception (as is the parent class IOException thrown by readLine), modify main to re-throw1 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();
}

1But 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;
        }
    }
}

Need Your Help

Java: Implementing Comparable Issue

java object arraylist overriding compareto

I'm new here and was looking for some advise on my code. I'm trying to make it possible for an arrayList ArrayList<patient> heartPatientArray = new ArrayList<patient>(); //heart patient...