我想讀取文本文件,將其轉換爲字節數組,然後將其寫入另一個文件。爲此,我不想丟失任何換行符,以便新行也應寫入上一步創建的新文件中。這是我迄今所做的:如何在將字節數組轉換爲字符串後將新的行字符寫入Java文件
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);
}
採取文件路徑和文件名作爲有效的標識符在上面的代碼片斷。
你也可以嘗試' 「%N」' –
你想在文件中的每一行seperatly,加密除了換行符? – Chriss