33 character output stream Writer

Knowledge points:

Function: Write the character data in the file to the file
java.io.Writer: character output stream, which is the top-level parent class of all character output streams, and is an abstract class
 common member method:
   void write(int c) writes a single character
   void write(char[]) write character array
   abstract void writer(char[] cbuf,int off,int len) write part of character array, off character array start index, len writes the number of characters
   void write(String str ) Write a string
   void write(String str,int off,int len) Write a certain part of the string, start the index of the off string, and write the number of characters in len
   void flush() Refresh the buffer
   void close() Close this    stream    , but

   flush it      first The File object constructs a FileWriter object File file A file      FileWriter(String filename) constructs a FileWriter object according to the given file name String filename The file path      construction method functions;





     1. A FileWriter object will be created
     2. A file will be created according to the file/file path passed in the constructor
     3. The FileWriter object will point to the created file
Steps:
1. Create a FileWriter object, which must be bound and written in the constructor 2. Use the method in FileWriter
, write, to write the data into the memory buffer (the process of converting characters to bytes)
3. Use the method flush in FileWriter to flush the data in the memory buffer to File, if you don't write item 3, write item 4 will also refresh
4. Release resources (the data in the memory buffer will be refreshed to the file)

package Demo33Writer;

import java.io.FileWriter;
import java.io.IOException;

/*
作用:把文件中的字符数据写入到文件
java.io.Writer:字符输出流,是所有字符输出流的顶层父类,是一个抽象类
 共性成员方法:
   void write(int c)写入单个字符
   void write(char[]) 写入字符数组
   abstract void writer(char[] cbuf,int off,int len) 写入字符数组的一部分,off字符数组开始索引,len写入字符个数
   void write(String str) 写入字符串
   void write(String str,int off,int len) 写入字符串的某一部分,off字符串开始索引,len写入字符个数
   void flush()刷新该留的缓冲
   void close() 关闭此流,但要先刷新它

   java.io.FileWriter extends继承 OutputStreamwriter-继承 Writer
   FileWriter:文件字符输出流 作用:把文件中的字符数据写入到文件
   构造方法:
     FileWriter(File file)根据给定的File对象构造一个FileWriter对象  File file 一个文件
     FileWriter(String filename)根据给定的文件名构造一个FileWriter对象  String filename文件路径
     构造方法作用;
     1.会创建一个FileWriter对象
     2.会根据构造方法中传递的文件/文件路径,创建文件
     3.会把FileWriter对象指向创建好的文件
步骤:
1.	创建FileWriter对象,构造方法中要绑定要写入数据的目的地
2.	使用FileWriter中的方法,write,把数据写入到内存缓冲区(字符转换为字节的过程)
3.	使用FileWriter中的方法flush,把内存缓冲区的数据,刷新到文件,不写第3条写第4条也会刷新
4.	释放资源(会把内存缓冲区中的数据刷新到文件)

 */
public class DemoWriter {
    public static void main(String[] args) throws IOException {
        //1.	创建FileWriter对象,构造方法中要绑定要写入数据的目的地
        FileWriter fw=new FileWriter("E:\\多线程\\d.txt");
        //2.	使用FileWriter中的方法,write,把数据写入到内存缓冲区(字符转换为字节的过程)
        fw.write(97);//跟字节输出流不一样,如果不刷新,不会写入数据到文件
        //3.	使用FileWriter中的方法flush,把内存缓冲区的数据,刷新到文件
        fw.flush();//写a

        //void write(int c)写入单个字符
        fw.write(98);//刷新之后,可以继续写数据 写b

        //void write(char[]) 写入字符数组
        char[] cs={'a','b','c','d','e'};//定义数组
        fw.write(cs);//写abcde

        //abstract void writer(char[] cbuf,int off,int len) 写入字符数组的一部分,off字符数组开始索引,len写入字符个数
        fw.write(cs,1,3);//写bd

        //void write(String str) 写入字符串
        fw.write("传智播客");//写传智播客

        //void write(String str,int off,int len) 写入字符串的某一部分,off字符串开始索引,len写入字符个数
        fw.write("黑马程序员",2,3);//写程序员

        System.out.println("写入成功");

        //4.	释放资源(会把内存缓冲区中的数据刷新到文件)
        fw.close();//关闭资源之后,就不能在写数据了

    }
}

empty d.txt file 

 Run the code:

 

 

Guess you like

Origin blog.csdn.net/dengfengling999/article/details/123997243