如何解决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.");
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。