Java 流操作的整理

来源:互联网 发布:java mysql增删改查 编辑:程序博客网 时间:2024/06/06 14:26

 

Java 流操作的整理

---------------------

1. 一些概念

1.1 File - An abstract representation of file and directory pathnames.

User interfaces and operating systems use system-dependent pathname-strings to name files and directories. This class presents an abstract, system-independent view of hierarchical pathnames.An abstract pathname has two components:

* An optional system-dependent prefix string, such as a disk-drive specifier, "/" for the UNIX root directory, or "////" for a Microsoft Windows UNC pathname, and

* A sequence of zero or more string names.

 

A pathname, whether abstract or in string form, may be either absolute or relative. An absolute pathname is complete in that no other information is required in order to locate the file that it denotes. A relative pathname, in contrast, must be interpreted in terms of information taken from some other pathname. By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir, and is typically the directory in which the Java virtual machine was invoked.

 

Instances of the File class are immutable; that is, once created, the abstract pathname represented by a File object will never change.

 

1.2 FileInputStream - A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment.

 

   FileOutputStream - A file output stream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other file-writing object) at a time. In such situations the constructors in this class will fail if the file involved is already open.

 

   FileInputStream and FileOutputStream are meant for reading and writing streams of raw bytes such as image data. For reading and writing streams of characters, consider using FileReader or FileWriter.(这个类一般是用于读取或者写入二进制流,例如图片,声音等。而如果读或写字符流的话,一般用FileReader或者FileWriter,它一般处理以字节为单位的二进制流,与编码无关,所以不存在乱码的问题)

 

1.3 FileReader - Convenience class for reading character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. To specify these values yourself, construct an InputStreamReader on a FileInputStream.

   FileWriter - Convenience class for writing character files. The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. To specify these values yourself, construct an OutputStreamWriter on a FileOutputStream

   最初Java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中write(char[] ch,int off,int length),flush()和close()方法为抽象方法,Reader中read(char[] ch,int off,int length)和close()方法是抽象方法。子类应该分别实现他们。 

 

   如果用户要自定义字符编码和buffer的大小,一般用OutputStreamWriter或InputStreamReader包装FileOutputStream或FileInputStream类来做,如

Writer out = new BufferedWriter(new OutputStreamWriter(System.out));

                 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("ming.txt")))

   有一点需要注意,当你写文件的时候,为了提高效率,写入的数据会先放入缓冲区,然后写入文件。因此有时候你需要主动调用flush()方法

 

1.4 InputStreamReader - An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.

   For top efficiency, consider wrapping an InputStreamReader within a BufferedReader. For example:

   BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

 

   InputStreamReader可以将读取的字节流转换成字符流,是Reader和Stream之间的桥梁。最初java是不支持对文本文件进行处理的,为了弥补这个缺憾而加入了Reader和Writer两个类。这个类在这里显得特别重要。

   OutputStreamWriter - An OutputStreamWriter is a bridge from character streams to byte streams: Characters written to it are encoded into bytes using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.

   For top efficiency, consider wrapping an OutputStreamWriter within a BufferedWriter so as to avoid frequent converter invocations. For example:

   Writer out = new BufferedWriter(new OutputStreamWriter(System.out));

1.5 BufferedReader - Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.

 

   BufferedWriter - Writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.

 

   PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));

   一般Writer(FileWriters and OutputStreamWriter)外都会包装一个BufferedWriter类来做缓冲,提供通用的缓冲方式文本读取,而且提供了readLine方式的读取。

 

2. 一些区别

2.1 File and FileInputStream

       File只是文件和目录路径的一个抽象,与平台无关,而FileInputStream是对File对象进行流操作,它更关心的是二进制流,而不是文件和目录的抽象。

2.2 FileInputStream and FileReader

       FileReader is meant for reading streams of characters. For reading streams of raw bytes, consider using a FileInputStream.

       FileInputStream以字节流的方法来读取数据,而FileReader会把字节流转换为字符流来读入,这边就涉及到一个编码的问题。这里的InputStream抽象类提供了对字节流byte的操作,而非字符文件的操作,而Reader抽象类提供了对char[]和String的操作。FileInputStream类以二进制输入/输出,效率很高,但是它的read()方法是读单个字节,不是很方便。而FileReader弥补了这个缺陷,可以以文本格式输入/输出,很方便。可以用它的read()来循环读取文件,也可以用封装的BufferedReader来读取行。

 

3. 例子

 

4. 参考

4.1 Java Doc API

4.2 http://dev.firnow.com/course/3_program/java/javaxl/2008821/136722.html

4.3 http://java.ccidnet.com/art/3737/20041108/523627_1.html

 

原创粉丝点击