[英]When reading and writing a file the output changes
我正在嘗試讀取文件(與擴展名無關)並在此之后寫入,但是當我這樣做時,輸出文件與輸入不同。
我的代碼是下一個:
OutputStream outputStream = null;
FileReader fr = new FileReader("rute\\inputfile.PNG");
BufferedReader br = new BufferedReader(fr);
String line;
while ((line= br.readLine()) != null) {
content += line;
}
byte[] toBytes= content.getBytes();
InputStream inputStream = new ByteArrayInputStream(toBytes);
try {
outputStream = new FileOutputStream(new File("rute\\output.PNG"));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
inputStream.close();
如果您問我為什么要轉換為字節並從這種形式寫入,是因為我需要對數據做一些事情,並且需要這種轉換。 如果您告訴我無法在String上加載圖片,是的,我可以執行以下操作:
File fil = ~~~~;
FileInputStream fis = null;
fis = new FileInputStream(fil);
byte[] bytess = IOUtils.toByteArray(fis);
但是我不想這樣做,因為如果我要加載大文件,則堆大小不足,這可以通過讀取“每行一行”來解決。
謝謝你的回答
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.