Java IO流

2021-07-26 00:56

阅读:646

标签:append   load   port   fileread   tag   输入流   系统   通过   code   

今天系统的学习了 Java IO 这部分的知识,把一些之前没有用过的,或者忘记的方法和类重新学了一下。怕自己睡一觉把今天学的忘完了QaQ。所以,不得已写一下博客

IO概述

package com.coder.com;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class Demo01 {
    /**
     * IO流概述
     * 可以将数据传输的操作看成一种数据的流动,按照流动的方向可以分为输入Input和输出Output
     * Java中的IO操作主要指的是 java.io包下的一些常用类的使用, 通过这些类可以对数据进行读取(输入流)和写出(输出流)
     * 

* IO流分类: * 按照方向: * - 输入流 * - 输出流 * 按照数据类型: * - 字节流 * -输入 InputStream * -输出 OutputStream * - 字符流 * -输入 Reader * -输出 Writer * 一切皆字节: * 计算机中的任何数据都是以二进制形式存储的 * 传输时也是以二进制的形式传输的 *

* @param args */ public static void main(String[] args) throws IOException { //字节流 OutputStream os = new FileOutputStream("E:\\a.txt", true); os.write(65); byte[] bytes = {66, 67, 68}; os.write(bytes, 1, 2); os.write("你好,世界".getBytes(StandardCharsets.UTF_8)); os.close(); InputStream is = new FileInputStream("E:\\a.txt"); bytes = new byte[1024]; int len = is.read(bytes); System.out.println("new String(bytes, 0, len) = " + new String(bytes, 0, len)); is.close(); } }

字节输出流

方法:

  • void write(int b)
  • void write(byte b[])
  • void write(byte b[], int off, int len)
  • FileOutputStream
  • void flush()
  • void close()

常用子类:

  • FileOutputStream(File file, boolean append)
  • BufferedOutputStream(OutputStream out)
  • ObjectOutputStream(OutputStream out)
  • PrintStream(boolean autoFlush, OutputStream out)

字符流

package com.coder.com;

import java.io.*;

public class Demo02 {
    public static void main(String[] args) throws IOException {
        Writer writer = new FileWriter("E:\\b.txt");
        writer.write("你好,世界,");
        writer.append("你好,Java").append("你好,Golang");
        writer.close();
        Reader reader = new FileReader("E:\\b.txt");
        char[] chars = new char[1024];
        int len = reader.read(chars);
        String s = new String(chars, 0, len);
        System.out.println("content = " + s);
        reader.close();
    }
}

转换流

package com.coder.com;

import java.io.*;

public class Demo03 {
    /**
     * 转换流
     * 字节流 装饰为 字符流 :使用装饰着设计模式
     * @param args
     */
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("E:\\a.txt");
        //param1 要转换的字节流
        //param2 指定编码名称
        InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
        char[] buffs = new char[1024];
        int len = isr.read(buffs);
        for (int i = 0; i 

打印流

package com.coder.com;

import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;

public class Demo04 {
    /**
     * 打印流
     * @param args
     */
    public static void main(String[] args) throws IOException {
        PrintStream ps = new PrintStream("E:\\c.txt");
        ps.println("你好,世界");
        ps.println("你好,Java");
        ps.println("你好, Golang");
        ps.close();
        PrintWriter pw = new PrintWriter("E:\\c.txt");
        pw.println("你好Vue");
        pw.println("你好SpringBoot");
        pw.close();
    }
}

Properties

package com.coder.com;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class Demo05 {
    public static void main(String[] args) throws IOException {
//        Properties properties = new Properties();
//        properties.setProperty("name", "王令");
//        properties.setProperty("age", "20");
//        properties.setProperty("gender", "男");
//        properties.store(new FileWriter("E:\\d.properties"), "个人信息");
        Properties properties = new Properties();
        properties.load(new FileReader("E:\\d.properties"));
        System.out.println("name = " + properties.getProperty("name"));
        System.out.println("age = " + properties.getProperty("age"));
        System.out.println("gender = " + properties.getProperty("gender"));
    }
}

序列化 反 序列化

package com.coder.com;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Demo06 {
    /**
     * 序列化 反 序列化
     *
     * @param args
     */
    public static void main(String[] args) throws IOException, ClassNotFoundException {
//        Person wl = new Person("王令", 21, "男");
//        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E:\\person"));
//        oos.writeObject(wl);
//        oos.close();
//        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("E:\\person"));
//        Person wl = (Person) ois.readObject();
//        System.out.println("wl = " + wl);
//        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E:\\list_person"));
//        List people = new ArrayList();
//        people.add(new Person("张三", 20, "男"));
//        people.add(new Person("李四", 21, "女"));
//        people.add(new Person("王五", 22, "男"));
//        oos.writeObject(people);
//        oos.close();
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("E:\\list_person"));
        List people = (List) ois.readObject();
        for (Person person : people) {
            System.out.println(person);
        }
    }
}

class Person implements Serializable {
    private String name;
    private Integer age;
    private String gender;

    public Person() {
    }

    public Person(String name, Integer age, String gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ", gender=‘" + gender + ‘\‘‘ +
                ‘}‘;
    }
}

Java IO流

标签:append   load   port   fileread   tag   输入流   系统   通过   code   

原文地址:https://www.cnblogs.com/circle-coder/p/15040492.html


评论


亲,登录后才可以留言!