bufferReader可以在不同的class然后调用到mainclass吗?

Can bufferReader be in different class and then call it to the main class?

我希望能够读取一个 txt 文件并从另一个 class 调用 bufferReader 到 main,这可能吗? 基本上我希望控制台要求用户放置他想要的 txt 文件,如果它存在,那么他可以读取它并显示给控制台。

Public static void main(String[] args) throws IOException {
        System.out.println("Give me the path or the name of the file you want to encrypt : ");
        Scanner scanner = new Scanner(System.in);
        String checker = scanner.nextLine();
            if (checker.contains(".txt")) {
                try {
                    InputStream inputStream = new FileInputStream(checker);
                    Scanner sc = new Scanner(inputStream);
                    StringBuffer sb = new StringBuffer();
                    BufferedWriter bw = new BufferedWriter(new FileWriter("copy-"+checker));
                    while (sc.hasNextLine()) {
                        sb.append("\n" + sc.nextLine());

                    }
                    sc.close();
                    System.out.println(sb);
                    System.out.println(AES.encrypt(sb.toString()));
//                  append the result and make a new file... H-O-W?
                    bw.write(AES.encrypt(sb.toString()));
                    System.out.println(AES.decrypt(AES.encrypt(sb.toString())));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("The file does not exists.");
            }

而不是 Public static void main 我想要一个 class 像 (class BufferReader{} 由 main class)

谢谢大家,对不起我的英语!

您应该阅读有关 Object 和 类 的内容。为了您的帮助,您可以像这样解决您的问题。

class Main {
    Public static void main(String[] args) throws IOException {
          MyBuffClass c = new MyBuffClass();
          c.doStuff();
    }
}


class MyBuffClass {
    public void doStuff() {
        System.out.println("Give me the path or the name of the file you want to encrypt : ");
        Scanner scanner = new Scanner(System.in);
        String checker = scanner.nextLine();
            if (checker.contains(".txt")) {
                try {
                    InputStream inputStream = new FileInputStream(checker);
                    Scanner sc = new Scanner(inputStream);
                    StringBuffer sb = new StringBuffer();
                    BufferedWriter bw = new BufferedWriter(new FileWriter("copy-"+checker));
                    while (sc.hasNextLine()) {
                        sb.append("\n" + sc.nextLine());

                    }
                    sc.close();
                    System.out.println(sb);
                    System.out.println(AES.encrypt(sb.toString()));
//                  append the result and make a new file... H-O-W?
                    bw.write(AES.encrypt(sb.toString()));
                    System.out.println(AES.decrypt(AES.encrypt(sb.toString())));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            } else {
                System.out.println("The file does not exists.");
            }
    }
}