Java - IO File 處理
Java IO Framework
Ref:
- Java read text files - FileReader, InputStreamReader, FileInputStream
- How to Read Text and Binary Files in Java (ULTIMATE GUIDE)
- Java Practices->Reading and writing binary files
Image:
Reading Text Files
This is step of reading file to string:
Class | Desc | |
---|---|---|
File | read path to file | |
FileInputStream | read file to stream | |
InputStream | InputStream | read stream to string |
InputStreamReader | read stream to string with "specific charset" | |
BufferedReader | read stream to string with buffer |
Here is description:
- InputStream: 是所有字节输入流的超类,一般使用它的子类。
- InputStreamReader: 是字节流与字符流之间的桥梁,能将字节流输出为字符流,并且能为字节流指定字符集,可输出一个个的字符。
- BufferedReader: 提供通用的缓冲方式文本读取,
readLine
读取一个文本行,从字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取。Ref: 【Java基础】InputStream 、 InputStreamReader和BufferedReader - zgljl2012的专栏 - CSDN博客
InputStream vs InputStreamReader vs FileReader
InputStream | InputStreamReader | FileReader | |
---|---|---|---|
level | abstract class InputStream | extends Reader | extends InputStreamReader |
read | binary | all input streams: (files, socket, network connections, classpath resources, a database blob …) |
from a file in the file system |
return | based on implement class | string | string |
charset | based on implement class | Yes specifying a specific character encoding |
Before Java 11: No based on platform default encoding After Java 11: Yes new constructor have been added to FileReader which allows you to specify Charset |
Reading binary files
Step of reading binary files:
File
→ FileInputStream
→
BufferedInputStream
DataInputStream
ByteArrayInputStream
See more detail on above image.
Close Stream
如果沒有關閉IO流會怎樣?這是我在項目曾經遇到的一個問題,後果是造成server”卡死”
當然卡死不是一個精確的programming language,用程序語言來說,很可能因為沒有關閉(close)造成【死鎖】,於是我朝著這個方向去尋找資料。
在InputStream (Java Platform SE 8 )說明:
Closes this input stream and releases any system resources associated with the stream.
這裡說明沒有關閉IO將造成流無法釋放資源。
另外,參考以下文章,結論如下:
- java - When does a stream close if its not closed manually? - Stack Overflow
- java - Do I need to close my FileOutputStreams? - Code Review Stack Exchange
FileInputStream
’sfinalize()
will try to close the file, but the garbage collector only frees the memory occupied bu the object being gc’d, it does not attempt to free any resources other than memory, including closing the file descriptor the OS assigned.
FileInputStream的finalize()將嘗試關閉文件,但是垃圾收集器僅釋放被gc’d對象佔用的內存,它本身不嘗試釋放內存以外的任何資源,包括關閉操作系統分配的文件描述符。
If you are not closing it manually then all the unmanaged resources will be released when the process terminates.
如果沒有手動關閉它,那麼當進程終止時,所有非託管資源都將被釋放。
如果從進程的角度來看待:Process (Java Platform SE 8 )
The
ProcessBuilder.start()
andRuntime.exec
methods create a native process and return an instance of a subclass ofProcess
that can bu used to control the process and obtain information about it. The classProcess
provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the precess.
Process
這個類的功能可以用來控制進程,並且提供input、output,以及流程的狀態控制。
By default, the created subprocess does not have its own terminal or console. All its standard I/O (i.e. stdin, stdout, sterr) operations will be redirected to the parent process, where they can be accessed via the streams obtained using the methods
getOutputStream()
,getInputStream()
, andgetErrorStream()
. The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may case the subprocess to block, or even deadlock.
默認情況下,創建的子進程沒有自己的終端或控制台。其所有的標準I / O(即標準輸入,標準輸出,標準錯誤)操作將被重定向到父進程,
在那裡他們可以通過使用流中的方法獲得的被訪問 getOutputStream()
, getInputStream()
和 getErrorStream()
。父流程使用這些流將輸入饋入子流程並從子流程獲取輸出。
由於某些本機平台僅為標準輸入和輸出流提供了有限的緩衝區大小,因此未能及時寫入子進程的輸入流或讀取子進程的輸出流可能導致子進程阻塞甚至死鎖。
P.S. Process
class 的用法: