How is I/O Handled in Java?
A File object encapsulates the properties of a file or a path, but does not contain the methods for reading/writing data from/to a file. In order to perform I/O, you need to create objects using appropriate Java I/O classes. 在Java中将外部数据读入内存或将内存数据保存到外部文件, 需要创建专门的I/O操作对象,最核心的类为输入输出流(InputStream和OutputStream),可以看做在内存和外部存储间建立了一个管道,将数据一个byte一个byte的输入或输出到目标位置。

Text and Binary Files
Files can be classified as either text or binary. A file that can be processed (read, created, or modified) using a text editor such as Notepad on Windows or vi on UNIX is called a text file. All the other files are called binary files. You cannot read binary files using a text editor—they are designed to be read by programs. For example, Java source programs are text files and can be read by a text editor, but Java class files are binary files and are read by the JVM. 文件分为文本文件和二进制文件两种类型,文本文件能被文本编辑器读写,其中又涉及到编码和解码,二进制文件则直接以byte为单位存储。例如Java的源代码文件就是一种文本文件,可以用编辑器打开编辑,而编译后的字节码文件则是一种二进制文件,不能直接由人或文本编辑器读写,需要由专门的计算机程序读写,例如Java虚拟机JVM。
Text I/O and Binary I/O
Text I/O requires encoding and decoding. The JVM converts a Unicode to a file specific encoding when writing a character and coverts a file specific encoding to a Unicode when reading a character. Binary I/O does not require conversions. When you write a byte to a file, the original byte is copied into the file. When you read a byte from a file, the exact byte in the file is returned.


