I want to read a text file, convert it into a byte array, process the byte array and then write it into another file. To do so I don't want to lose any newline characters so that new lines shall also be written into the new file created in the previous step. This is what I have done so far:
StringBuilder line=null;
try (BufferedReader in = new BufferedReader(new FileReader(filePath))) {
line = new StringBuilder();
String tempLine=null;
fileSelect=true;
while ((tempLine=in.readLine()) != null) {
line.append(tempLine+System.lineSeparator());
}
}
byte[] plaintext =String.valueOf(line).getBytes("UTF-8");
// Encrypt the data
byte[] encrypted = cipher.doFinal(plaintext);
//String enc=new String(encrypted);
try (FileOutputStream out = new FileOutputStream(fileName)) {
out.write(encrypted);
}
Take filePath and fileName as valid identifiers in the above code snippet.
"%n"