Java's character-output stream [writer]

First, the character-output stream

  java.io.Writer abstract class is a superclass for all classes of characters to write the stream, the specified character information written to the destination.

    It defines the basic functionality common method output stream of bytes.

void write (int c) Write a single character. 
void write (char [] cbuf) writes the character array. 
a portion abstract void write (char [] cbuf , int off, int len) writes a character array, the array index off begins, len the number of characters written. 
void write (String str) write the string. 
a portion of the void write (String str, int off , int len) writes the string, starting at index off the string, len number of characters to write. 
void flush () Flush the stream. 
void close () Close the stream, flushing it first.

  Writer subclass of abstract class following structure:

 

   Dark node in FIG stream, a light stream is processed.

Two, FileWriter class

  java.io.FileWriter class is to write characters to the file of the convenience class. The system default character encoding and the default byte buffer is configured.

  Role : writing the character data in memory to a file.

  1, construction method

FileWriter (File file): create a new FileWriter, given File object to be read. 
FileWriter (String fileName): Create a new FileWriter, the name of the file to be read given.

     Parameters : destination data is written.

      File file: file object

      String fileName: path to the file.

      Constructors role :

      ① creates a FileWriter object

      ② will pass the path of the constructor document / file, create an object

      ③ FileWriter object will point to create a good document.

      Character-output stream using the steps of [ important ]:

      ① create FileWriter object constructor to bind the data to be written to the destination

      ② write FileWriter in use, data is written into a memory buffer (byte character conversion process)

      ③ flush FileWriter in use, the data in the memory buffer, to a file refreshed

      ④ release resources (will first refresh the memory buffer to a file)

  2, the basic write data

    (1) Write character : Write (B int) method, each time a character can write data, code demo:

. 1  public  static  void main (String [] args) throws IOException {
 2          // 1. Create FileWriter object constructor bound to write the data destination 
. 3          FileWriter FW = new new FileWriter ( "E: \\ d.txt " );
 4          // 2. a method in FileWriter write, the data is written into a memory buffer (byte character conversion process)
 . 5          // void write (C int) writing a single character. 
. 6          fw.write (97 );
 . 7          // 3. Use of flush FileWriter, the data buffer memory, a refresh to a file
 . 8          // fw.flush ();
 . 9          // 4. release resources (will first refresh the memory buffer to a file) 
10          fw.close ();
 . 11     }

 

     注意:如果未调用 close 方法,数据只是保存到了缓冲区,并未写出到文件中。

    (2)关闭与刷新

      区别:

flush :刷新缓冲区,流对象可以继续使用。
close :先刷新缓冲区,然后通知系统释放资源。流对象不可以再被使用了。

      Demo:

 1  public static void main(String[] args) throws IOException {
 2         //1.创建FileWriter对象,构造方法中绑定要写入数据的目的地
 3         FileWriter fw = new FileWriter("E:\\e.txt");
 4         //2.使用FileWriter中的方法write,把数据写入到内存缓冲区中(字符转换为字节的过程)
 5         //void write(int c) 写入单个字符。
 6         fw.write(97);
 7         //3.使用FileWriter中的方法flush,把内存缓冲区中的数据,刷新到文件中
 8         fw.flush();
 9         //刷新之后流可以继续使用
10         fw.write(98);
11 
12         //4.释放资源(会先把内存缓冲区中的数据刷新到文件中)
13         fw.close();
14 
15         //close方法之后流已经关闭了,已经从内存中消失了,流就不能再使用了
16         fw.write(99);//IOException: Stream closed
17     }

 

    注意即便是flush方法写出了数据,操作的最后还是要调用close方法,释放系统资源。 

  3、写出其他数据

    (1)写出字符数组:write(char[] cbuf) write(char[] cbuf, int off, int len) ,每次可以写出字符数组中的数据,用法类似FileOutputStream,代码使用演示:

 1  public static void main(String[] args) throws IOException {
 2         FileWriter fw = new FileWriter("E:\\f.txt");
 3         char[] cs = {'a','b','c','d','e'};
 4         //void write(char[] cbuf)写入字符数组。
 5         fw.write(cs);//abcde
 6 
 7         //void write(char[] cbuf, int off, int len)写入字符数组的某一部分,off数组的开始索引,len写的字符个数。
 8         fw.write(cs,1,3);//bcd
 9 
10         fw.close();
11     }

 

    (2)写出字符串:write(String str) write(String str, int off, int len) ,每次可以写出字符串中的数据,更为方便,代码使用演示:

 1  public static void main(String[] args) throws IOException {
 2         FileWriter fw = new FileWriter("E:\\f.txt");
 3    
 4         //void write(String str)写入字符串。
 5         fw.write("博客园");//博客园 6 
 7         //void write(String str, int off, int len) 写入字符串的某一部分,off字符串的开始索引,len写的字符个数。
 8         fw.write("天道酬勤啊",2,3);//酬勤啊
 9 
10         fw.close();
11     }

 

    (3)续写和换行

      续写,追加写:使用两个参数的构造方法

FileWriter(String fileName, boolean append)
FileWriter(File file, boolean append)

                     参数

        String fileName,File file:写入数据的目的地。

        boolean append:续写开关 true:不会创建新的文件覆盖源文件,可以续写; false:创建新的文件覆盖源文件。

      换行:换行符号

        windows:\r\n

        Linux / Unix:\n

        mac:\r

      Demo :

1 public static void main(String[] args) throws IOException {
2         FileWriter fw = new FileWriter("E:\\g.txt",true);
3         for (int i = 0; i <10 ; i++) {
4             fw.write("HelloWorld"+i+"\r\n");
5         }
6 
7         fw.close();
8     }

 

注意:字符流,只能操作文本文件,不能操作图片,视频等非文本文件。单纯读或者写文本文件时,使用字符流,其他情况使用字节流。

 

Guess you like

Origin www.cnblogs.com/niujifei/p/11489087.html