从linux终端上的用户输入读取文件名 - JAVA

时间:2022-05-15 00:25:15

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

我想在JAVA中创建一个小脚本,以便在linux终端中接收文件名并读取该文件。

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

hello.java:10:错误:未报告的异常FileNotFoundException;必须被捕获或声明被抛出FileReader fileReader = new FileReader(fileName); ^ hello.java:13:错误:未报告的异常IOException;必须被捕获或声明被抛出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?

我错过了什么?

3 个解决方案

#1


1  

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

正如其他人所建议的那样,阅读IDE /编译器在出现错误时告诉您的内容非常有帮助...

Hope that helps

希望有所帮助

#2


1  

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

FileNotFoundException是一个经过检查的Exception(由readLine抛出的父类IOException),修改main以重新throw1它就像

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

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

或者用try-catch(有资源)包围它

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.

1)你仍然应该在最后关闭bufferedReader。

#3


1  

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.

您需要处理可能的异常。您可以指定封闭方法main抛出异常,但最好自己处理它。

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

#1


1  

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

正如其他人所建议的那样,阅读IDE /编译器在出现错误时告诉您的内容非常有帮助...

Hope that helps

希望有所帮助

#2


1  

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

FileNotFoundException是一个经过检查的Exception(由readLine抛出的父类IOException),修改main以重新throw1它就像

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

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

或者用try-catch(有资源)包围它

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.

1)你仍然应该在最后关闭bufferedReader。

#3


1  

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.

您需要处理可能的异常。您可以指定封闭方法main抛出异常,但最好自己处理它。

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