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

Java IO流

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

Java IO流

个人笔记 

一、什么是IO流?

也就是In和Out输入和输出,也就是程序和外部设备之间的数据传递(文件、管道、网络连接)

流(Stream)是指一连串的数据(字节或者字符)通过先进先出的方式发送信息的通道(而数据又可以是文件、内存、网络连接);

二、IO流的分类

  • 按数据流的方向:输入流、输出流

  • 按处理数据单位:字节流、字符流

  • 按功能:节点流、处理流

三、字节流
public class 字节复制 {
    public static void main(String[] args) throws IOException {
        //选择要从哪个文件输入;
        File infile = new File("C:\Users\1\Desktop\三笠.jpg");
        //选择要输出到那个文件
        File outFile = new File("C:\Users\1\Desktop\三笠2.jpg");
        //字节输入流
        FileInputStream fis = new FileInputStream(infile);
        //字节输出流
        FileOutputStream fos = new FileOutputStream(outFile);
        //字节输入缓冲流
        BufferedInputStream bis = new BufferedInputStream(fis);
        //字节输出缓冲流
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        //一边输入一个输出
        //自定义缓存区
        byte[] bytes = new byte[2];
        int data = 0;
        while ((data=bis.read(bytes))!=-1){
            bos.write(bytes,0,data);
        }
        //关闭流
        bis.close();
        bos.close();
        System.out.println("复制成功");
    }
}
四、字符流
 public static void main(String[] args) throws IOException {

        //读取的文件
        File fie = new File("C:\Users\1\Desktop\CmdInfo.txt");
        //写入到哪个文件
        File foe = new File("C:\Users\1\Desktop\CmdInfo2.txt");
        
        //读取流
        FileReader fr = new FileReader(fie);
        //字符读取缓冲
        BufferedReader br = new BufferedReader(fr);
        
        //写入流
        FileWriter fw = new FileWriter(foe);
        //字符写入缓冲
        BufferedWriter bw = new BufferedWriter(fw);
        
        //操作
        int data = 0;
        char[] chars = new char[64];
        while ((data= br.read(chars))!=-1){
            bw.write((new String(chars,0,data)));
        }
        //关
        br.close();
        bw.close();
        System.out.println("写入成功");
    }
五、字节流与字符流的转换
public static void main(String[] args) throws IOException {
        //选择需要读取的文件
        File file = new File("C:\Users\1\Desktop\CmdInfo.txt");
        //字节读取
        FileInputStream fis = new FileInputStream(file);

        //字节转化字符类型
        InputStreamReader isr = new InputStreamReader(fis,"utf-8");

        BufferedReader br = new BufferedReader(isr);

        int data=0;
        StringBuilder sb = new StringBuilder();
        while ((data=br.read())!=-1){
            sb.append((char) data);
        }
        System.out.println(sb.toString());
        br.close();
    }
六、打印流
    public static void main(String[] args) throws FileNotFoundException {
        //选择需要写入到哪个文件
        File file = new File("C:\Users\1\Desktop\CmdInfo2.txt");
        //打印流
        PrintWriter pw = new PrintWriter(file);
        pw.println(7144);
        pw.println(true);
        pw.println('x');
        //关闭打印流
        pw.close();
        System.out.println("OK");
    }
七、序列化与反序列化
public class 序列化与反序列化 {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        SerFile();
        Thread.sleep(1000);
        FSerFile();
    }

    public static void SerFile() throws IOException {
        //选择需要写入到哪个文件
        File file = new File("C:\Users\1\Desktop\peo.bin");
        //字节写入流
        FileOutputStream fos = new FileOutputStream(file);
        //对象流
        ObjectOutputStream ojs = new ObjectOutputStream(fos);
        //序列化
        Person ps = new Person("罗罗诺亚",20);
        ojs.writeObject(ps);
        //关闭
        ojs.close();
        System.out.println("成功");
    }

    public static void FSerFile() throws IOException, ClassNotFoundException {
        //选择需要读取到哪个文件
        File file = new File("C:\Users\1\Desktop\peo.bin");
        //输入流
        FileInputStream fis = new FileInputStream(file);
        //对象流
        ObjectInputStream ois = new ObjectInputStream(fis);
        //反序列化
        Person ps = (Person) ois.readObject();
        //关闭流
        ois.close();
        System.out.println(ps);
        System.out.println("OK");
    }

}
class Person implements Serializable {
    private String name;
    private int age;

    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Person() {
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

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

 注意:在序列化一次后,该序列化对象会有一个唯一的ID;所以反序列化的时候ID也必须是同样的才能序列化成功;且在序列化的类要实现一个标志接口;

八、File类

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

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

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