I want to be able to read a txt file and call bufferReader from a different class to main, is that possible? Basically I want the console to ask the user to place the txt file he wants and if it exists, then he can read it and show it to the console.
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.");
}
Instead of Public static void main i want a class like (class BufferReader{} called by the main class)
Thank you all and sorry for my english!
You should read about Object and classes. For your help you can solve your problem like this.
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.");
}
}
}