我想读取一个文本文件,将其转换为字节数组,处理字节数组,然后将其写入另一个文件。为此,我不想丢失任何换行符,因此新行也应写入上一步创建的新文件中。这是我到目前为止所做的:
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);
}
将 filePath和fileName作为上述代码片段中的有效标识符。