0
点赞
收藏
分享

微信扫一扫

File类字节输入、输出流

幺幺零 2022-04-21 阅读 52
java

目录

1、字节输出流:FileOutputStream

2、 字节输入流:FileIntputStream

运行结果:

3、字节输出流与字节输入流联合使用:

结果:

4、 缓存流:

 小结:

5、对象流:

1.创建一个对象

2.创建一个FileOutputStream对象:

3、序列化结果为: 

 4、实现反序列化


1、字节输出流:FileOutputStream

 @Test
    public void test() throws Exception {
        FileOutputStream fileOutputStream = new FileOutputStream("F:\\111\\1.txt");
        String str="lwh";
        byte[] bytes = str.getBytes();
        fileOutputStream.write(bytes);
        fileOutputStream.close();
    }

2、 字节输入流:FileIntputStream

 @Test
    public void test2() throws Exception{
        FileInputStream os = new FileInputStream("F:\\111\\1.txt");
        byte[] bytes = new byte[3];
        int c=0;
        while ((c=os.read(bytes))!=-1){
            String str = new String(bytes,0,c);
            System.out.println(str);
        }

运行结果:

3、字节输出流与字节输入流联合使用:

package demo420.demo02;

import org.junit.Test;

import java.io.*;

/**
 * @创建人 xiaoliu
 * @创建时间 2022/4/20
 * @描述
 */
public class Demo01 {
    @Test
    public void test() throws IOException {
        FileInputStream fos = new FileInputStream("C:\\Users\\Administrator\\Desktop\\1.jpg");
        FileOutputStream os = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\2.jpg");
        byte[] bytes = new byte[5];
        int len;
        while ((len=fos.read(bytes))!=-1) {
            os.write(bytes,0,len);

        }
        fos.close();
        os.close();
    }
}

结果:

4、 缓存流:

package demo420.demo04;

import org.junit.Test;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * @创建人 xiaoliu
 * @创建时间 2022/4/21
 * @描述
 */
public class DemoBuffer {
    @Test
    public void test() throws Exception{
        FileInputStream is = new FileInputStream("C:\\Users\\Administrator\\Desktop\\111\\6.txt");
        FileOutputStream fos = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\111\\7.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
       int c=0;
        byte[] bytes = new byte[10];
        while ((c=is.read(bytes))!=-1){
            String str = new String(bytes,0,c);
            bos.write(str.getBytes());// byte[] bytes = str.getBytes();转成字节数组
        }
       

    }
}

 小结:

5、对象流:

1.创建一个对象

package demo420.demo03;

import java.io.Serializable;

/**
 * @创建人 xiaoliu
 * @创建时间 2022/4/20
 * @描述
 */
public class Hero implements Serializable {
    private String name;
    private int age;
    private String level;

    public Hero(String name, int age, String level) {
        this.name = name;
        this.age = age;
        this.level = level;
    }

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

2.创建一个FileOutputStream对象:

  FileOutputStream is = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\demo.txt");
        ObjectOutputStream os = new ObjectOutputStream(is);
        Hero lwh = new Hero("lwh", 2, "3");
        os.writeObject(lwh);
        os.close();

3、序列化结果为: 

 4、实现反序列化

  FileInputStream is = new FileInputStream("C:\\Users\\Administrator\\Desktop\\demo.txt");
        ObjectInputStream objectInputStream = new ObjectInputStream(is);
        Object o = objectInputStream.readObject();
        System.out.println(o);
        is.close();

举报

相关推荐

0 条评论