【问题标题】:Can bufferReader be in different class and then call it to the main class?bufferedReader 可以在不同的类中,然后将其调用到主类吗?
【发布时间】:2021-09-01 22:39:21
【问题描述】:

我希望能够读取一个 txt 文件并从不同的类调用 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.");
            }

我想要一个类似于(由主类调用的类 BufferReader{})的类,而不是 Public static void main

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

【问题讨论】:

    标签: java bufferedreader filereader reader


    【解决方案1】:

    您应该阅读有关对象和类的信息。为了您的帮助,您可以像这样解决您的问题。

    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.");
                }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      • 1970-01-01
      • 2017-12-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多