栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

1-19 I/O流

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

1-19 I/O流

定义
  • 流是一组有起点、终点和顺序的字节集合。
分类

输入流:读文件
输出流:写文件

字节流:数据流中最小的数据单元是字节(byte)。
字符流:数据流中最小的数据单元是字符,java中的字符是Unicode编码,一个字符占两个字节。

字节I/O与字符I/O实例
import java.io.*;

public class FileDemo {
    //字节流 读文件
    public static void readFile() throws IOException {
        //文件管理类
        File file = new File("E:/tmp.txt");
        //字节输入流
        InputStream in = new FileInputStream(file);
        //读取字节数
        int size = in.available();
        for (int i = 0; i < size; ++i){
            System.out.print((char) in.read());
        }
        System.out.println();
        in.close();
    }
    //字节流 写文件
    public static void writeFile() throws IOException {
        File file = new File("E:/tmp.txt");
        //不存在则创建
        if (!file.exists()){
            file.createNewFile();
        }
        //创建字节输出流,追加写入
        OutputStream out = new FileOutputStream(file, true);
        //要写入的信息
        String str = "abc";
        out.write(str.getBytes());
        //信息写入文件
        out.flush();
        out.close();
    }

    //字符流 读文件
    public static void readFile2() throws IOException {
        File file = new File("E:/tmp.txt");
        //创建字节输出流
        InputStream in = new FileInputStream(file);
        //转化为字符输出流
        InputStreamReader reader = new InputStreamReader(in, "UTF-8");
        //字符流处理类
        BufferedReader br = new BufferedReader(reader);
        String str = br.readLine();
        while (str != null){
            System.out.println(str);
            str = br.readLine();
        }
        //关闭资源
        br.close();
        reader.close();
        in.close();
    }

    public static void writeFile2() throws IOException {
        File file = new File("E:/tmp.txt");
        if(!file.exists()){
            file.createNewFile();
        }
        OutputStream out = new FileOutputStream(file);
        OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
        BufferedWriter bw = new BufferedWriter(writer);
        bw.write("老王");
        bw.flush();
        bw.close();
        writer.close();
        out.close();
    }

    public static void main(String[] args) {
        try{
            writeFile();
            readFile();
            writeFile2();
            readFile2();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

File类常用方法

大文件I/O实例
import java.io.*;

public class MediaCopy {

    public static void copyMediaFile() throws IOException {
        File sourFile = new File("E:/tmp.mp4");
        if(!sourFile.exists()){
            System.out.println("File is not found.");
            return;
        }
        InputStream in = new FileInputStream(sourFile);
        OutputStream out = new FileOutputStream(new File("E:/tmp2.mp4"));

        byte[] bytes = new byte[1024];
        int size;
        while((size = in.read(bytes)) != -1){
            out.write(bytes, 0, size);
            out.flush();
        }
        out.close();
        in.close();
    }
    public static void main(String[] args) {
        try {
            copyMediaFile();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

序列化与反序列化
  • 序列化:将对象转化为特定格式的字符串。
  • 反序列化:将特定格式的字符串转化为对象。
  • 序列化ID用来检验版本一致性。
  1. 先让类继承 Serializable接口,并设置序列化id作为类的成员变量
import java.io.Serializable;

public class User implements Serializable {
    //序列化ID
    private static final long serialVersionUID = 2021121301142583248L;
    private String name;

    public User(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + ''' +
                '}';
    }
}

  1. 使用Object流实现I/O
import java.io.*;

public class Serial {
    //序列化
    public static void serialize(Object object) throws IOException {
        File file = new File("E:/tmp.txt");
        if(!file.exists()){
            file.createNewFile();
        }
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
        //对象序列化
        out.writeObject(object);
        out.flush();
        out.close();
    }

    public static Object deserialize(String path) throws IOException, ClassNotFoundException {
        File file = new File(path);
        if(!file.exists()){
            System.out.println("File is not found.");
            return null;
        }
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
        Object object = in.readObject();
        in.close();
        return object;
    }
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        User user = new User("xiaoming");
        //序列化
        serialize(user);
        //反序列化
        User user2 = (User)deserialize("E:/tmp.txt");
        System.out.println(user2);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/684711.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号