使用BufferedReader
和BufferedInputStream
有什麼區別?在Java中讀取數據
1
A
回答
1
顧名思義,一個用於讀取數據,另一個用於輸出數據。
4
A BufferedReader
用於讀數字符數據。 A BufferedOutputStream
用於寫入二進制數據。
任何類從Reader
或Writer
處理繼承與16位Unicode字符數據,而從類或InputStream
OutputStream
inherting關心的是處理二進制數據。類InputStreamReader
和OutputStreamWriter
可用於在兩類數據之間進行橋接。
3
Bufferedreader以字符串的形式從文件中讀取數據。 BufferedOutputStream以字節寫入文件。的BufferedInputStream以字節爲單位讀取數據
樣本的BufferedReader:
try {
BufferedReader br = new BufferedReader(new FileReader(new File(your_file));
while ((thisLine = br.readLine()) != null) {
System.out.println(thisLine);
}
}
樣品的BufferedOutputStream:
//Construct the BufferedOutputStream object
bufferedOutput = new BufferedOutputStream(new FileOutputStream(filename));
//Start writing to the output stream
bufferedOutput.write("Line 1".getBytes());
bufferedOutput.write("\r\n".getBytes());
bufferedOutput.write("Line 2".getBytes());
bufferedOutput.write("\r\n".getBytes());
的BufferedInputStream讀取字節:
樣品 :
//Construct the BufferedInputStream object
bufferedInput = new BufferedInputStream(new FileInputStream(filename));
int bytesRead = 0;
while ((bytesRead = bufferedInput.read(buffer)) != -1) {
String chunk = new String(buffer, 0, bytesRead);
System.out.print(chunk);
}
相關問題
- 1. 在java中從串口讀取數據
- 2. 在Java中讀取大量數據
- 3. Java卡從數組中讀取數據
- 4. 在javascript中讀取數據從數組中讀取數據
- 5. 的Java:從InputStream讀取數據,並不總是讀取數據
- 6. 讀取java中的XML數據庫列
- 7. Java:從過程中讀取元數據
- 8. 讀取數據,在PowerShell中
- 9. 從java數據庫讀取docx文件在java中
- 10. 如何在java中讀取json url中的數據?
- 11. 在java中讀取和使用csv文件中的數據
- 12. 我想讀取文件中某行的數據。在Java中
- 13. 從SYS_REFCURSOR在Oracle存儲過程中讀取數據,並在Java
- 14. 如何在從mysql讀取數據時在java中分割「\ n」?
- 15. 在Python中讀取protobuf。提取數據
- 16. 使用POI讀取Java數據庫(CLOB列)中的Excel數據
- 17. 如何讀取數據流忽略Java中的數據類型
- 18. 從文件中讀取數據並拆分爲Java數據
- 19. 在JAVA中讀取JSON數組
- 20. Java - 讀取並存儲在數組中
- 21. 在Java中讀取JSON數組
- 22. 在txt文件中讀取分數java
- 23. Google Api,Java,從網頁讀取數據
- 24. 使用Java讀取JSON數據
- 25. 讀取數據文件的地址(JAVA)
- 26. Java/Arduino - 從串口讀取數據
- 27. 從Java XML文件讀取數據
- 28. kafka java消費者未讀取數據
- 29. Java套接字不讀取數據
- 30. Java - 從網站讀取數據
你[R你的答案中的參考文件,但當然BufferedReader/BufferedOutputStream可以讀/寫任何目的地,而不僅僅是一個文件。 – Adamski
你說得對。但只是樣本,以顯示他如何使用...(更實際的方式來理解) – Kayser