IO stream-character stream

Character stream

  字符流,以字符为单位读写数据
        Reader
            转换流  InpuStreamReader
            FileReader
            BufferedReader
          Writer
            转换流         OutStreamWriter
            FileWriter
            BufferedWriter

Reader's basic method reads a character and returns it as an integer, if it returns -1 to the end of the input stream.
The basic method of Writer writes a character data to the output stream, the byte data is 16 bits of parameter b

FileReader&&FileWriter

 public static void main(String[] args) throws IOException {
    
    
    
        FileReader reader = new FileReader("D:\\test.txt");
        FileWriter writer = new FileWriter("D:\\test1.txt");
        /*int c = ' ';
        while((c= reader.read())!=-1){
            System.out.println((char)c);
            writer.write(c);
        }*/
        //读取为字符类型数组
        char[] c = new char[5];
        int length;
        while((length= reader.read(c))!=-1){
    
    
            System.out.println(length);
            writer.write(c,0,length);
        }
        writer.close();
        reader.close();
    }

BufferedReader&&BufferedWriter

There is character splicing in the FileWriter method, the original content will not be overwritten when writing,
and the readLine(); method in the BufferedWriter will continue to be spliced ​​later , which realizes the function of reading one line of the file each time

 public static void main(String[] args) throws IOException {
    
    
        FileReader reader = new FileReader("D:\\test.txt");
        BufferedReader breader = new BufferedReader(reader);

        // FileWriter方法中有字符拼接,写入时不会覆盖原来的内容,会在后面继续拼接
        FileWriter writer = new FileWriter("D:\\test1.txt", true);
        BufferedWriter bwriter = new BufferedWriter(writer);

        //读完时返回null;
        //breader.readLine()一次读一行数据
        /*System.out.println(breader.readLine());
        System.out.println(breader.readLine());
        System.out.println(breader.readLine());*/

        String line = null;
        while ((line = breader.readLine()) != null) {
    
    
            bwriter.write(line);//一次一行写入
            bwriter.newLine();//新的一行
        }
        bwriter.flush();
        bwriter.close();
        breader.close();
    }

Print print flow

Print stream: One-way output data from the program
PrintWriter: Print character stream
Case: For example, output web page information from the server to the client browser.

 public static void main(String[] args) throws FileNotFoundException {
    
    
        
        PrintWriter  out = new PrintWriter("E:\\demo.html");
        out.println("<h1>这是从服务器端响应回来的数据</h1>");
        out.println("<h1>这是从服务器端响应回来的数据</h1>");
        out.write("<h2>这是从服务器端响应回来的数据</h2>");
        out.print(true);// print底层使用的还是write()  只是重载了多个,可以处理多种的数据类型
		out.close();
    }

Object input and output stream (object serialization and deserialization)

对象输入输出流
对象:内存中的对象
为什么要将对象输出?
内存中的数据在电脑关闭,服务器停止时数据就会消失
有时候需要将这些数据保存起来

The output stream of the
object : ObjectOutputStream  The input stream of the object: ObjectInputStream

 public static void main(String[] args) throws IOException, ClassNotFoundException {
    
    
// 对象的序列化
		FileOutputStream out = new FileOutputStream("D:\\demo.txt");
		
		//ObjectOutputStream中用writeObject()方法可以直接将对象保存到输出流中。
        ObjectOutputStream oout = new ObjectOutputStream(out);

        String s ="abc";
        Date date =new Date();

        oout.writeObject(s);
        oout.writeObject(date);
        oout.close();

		//反序列化
        FileInputStream in = new FileInputStream("D:\\demo.txt");
        
        //在ObjectInputStream 中用readObject()方法可以直接读取一个对象
        ObjectInputStream oin =new ObjectInputStream(in);
        String s =(String) oin.readObject();
        Date date = new Date();
        System.out.println(s);
        System.out.println(date);
        oin.close();
    }

New Student Class

/*
需要被序列化类的对象,此类必须要实现Serializable接口
 */
public class Student implements Serializable {
    
    
    //会自动为类生成一个 默认ID号,当此类中的内容发生修改后,id号会发生变化
    //实用工具生成一个序列化ID号,这样类发生修改后,此ID依然不会改变
    private static final long serialVersionUID = -7119195593092378008L;
    int num;
    String name;
    //被transient修饰的属性,不能被序列化
    transient String adress;

    public Student(int num, String name,String adress) {
    
    
        this.num = num;
        this.name = name;
        this.adress= adress;
    }

    @Override
    public String toString() {
    
    
        return "Student{" +
                "num=" + num +
                ", name='" + name + ",adress="+adress+'\'' +
                '}';
    }
}

    public static void main(String[] args) throws IOException, ClassNotFoundException {
    
    
        FileOutputStream fos = new FileOutputStream("D:\\demo.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        Student stu = new Student(12,"jim","陕西省");
        oos.writeObject(stu);
        oos.close();
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException {
    
    
        FileInputStream in = new FileInputStream("D:\\demo.txt");
        ObjectInputStream oin = new ObjectInputStream(in);
        Student student = (Student)oin.readObject();
        System.out.println(student);
        oin.close();
    }

Guess you like

Origin blog.csdn.net/XiaoFanMi/article/details/112791628