java - bufferReader 可以在不同的类中,然后将其调用到主类吗?
问题描述
我希望能够读取一个 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.");
}
而不是 Public static void main 我想要一个类(主类调用的类 BufferReader{})
谢谢大家,对不起我的英语!
解决方案
您应该阅读有关对象和类的信息。为了您的帮助,您可以像这样解决您的问题。
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.");
}
}
}
推荐阅读
- android - 我应该如何在嵌套代码中返回“Observable/Flowable/Single”“onError”?
- apache - 服务器设置并在计算机上创建软件/应用程序
- spring - Could not connect to Redis at 10.XX.XX.28:6379: Unknown error - while accessing from Spring Batch or windows machine
- javascript - jquery 无法用于 https 页面
- vba - 枢轴宏不起作用
- django - 将迁移更改写入 django 简单历史记录
- vba - 如何保护 excel 文件,以便表单和代码可以防止复制?
- python - 当你使用 Process 或 Pool 实现多处理时,python 解释器做了什么?
- python - Python 错误:AttributeError:__enter__
- php - Oci8_12.dll 错误“未找到驱动程序”