2021-08-04

OutputStream:
表示字节输出流的顶级父类
close();关闭流
flush();刷新流
write(byte[] bye):输出字节数组
write(byte[] bye,int off,int len):输出数组,从off位置开始输出len个长度的数据
FileOutputStream(字节输出流):
将内存上的数据输出到硬盘
构造方法(当这个文件不存在的时候会创建一个新的文件,如果存在会清空文件中的数据):

	//会有一个找不到文件的异常要处理
     //FileOutputStream构造方法
        File file=new File("d:/io/a.txt");
        FileOutputStream fos=new FileOutputStream(file);
        方式二:
        FileOutputStream fos=new FileOutputStream("d:/io/a.txt");

write方法:

  //write()方法 向文件存入abc
        fos.write(97);
        fos.write(98);
        fos.write(99);
        //write(byte[]byte) 向文件写入数组数据
        fos.write("我爱你胡".getBytes());
        //把abc写入数组中
        byte [] byt={97,98,99,100,101};
        fos.write(byt,0,3);
        fos.close();//操作完以后一定要关闭流释放资源

像刚才所说的,每次会清空文件里的数据,那么我不想让他清空文件的数据,继续接着写入应该怎么办

//追加续写,构造方法的时候在file对象或者字符串后面加一个true的参数就可以完成追加续写了
FileOutputStream fos=new FileOutputStream("d:/io/a.txt",true);

换行

 fos.write("\r\n".getBytes());//回车换行
        fos.write("\n".getBytes());//换行

InputStream:
字节输入流的超类
将硬盘的数据读取到内存
FileInputStream(字节输入流):
构造方法:

//方式一:
 File file=new File("d:/io/a.txt");
 FileInputStream fis=new FileInputStream(file);
 方式二:
 FileInputStream fis=new FileInputStream("d:/io/a.txt");//abcdefghi

read:

//read():读取一个字节,如果读取到文件末尾返回-1
        int read = fis.read();
        System.out.println(read);//97
//循环改进
        int read;
        while ((read=fis.read())!=-1){
            System.out.println((char)read);// abcdefghi
        }
 //read(byte[] byt) 读取数组长度的数据到字节数组
        byte[] byt=new byte[5];
        fis.read(byt);
        System.out.println(Arrays.toString(byt));//97 98 99 100 101
        for (byte b : byt) {
            System.out.println((char)b); // abcde
        }
//read(byte[] byt)返回值是读取的有效个数
        byte[] byt=new byte[5];
        int read = fis.read(byt);
        System.out.println(read); //5

//复制图片案列

            //1.创建输入流读取图片
            FileInputStream fis=new FileInputStream("d:/io/girl.jpg");
            //5.创建输出流实现复制功能
            FileOutputStream fos=new FileOutputStream("d:/庞传玉/a.jpg");
            //2.创建数组接受数据
            byte []byt=new byte[1024];
            //3.创建变量读取有效数字
            int len;
            //4.创建循环读取
            while ((len=fis.read(byt))!=-1){
                //6.读取数据写出去
                fos.write(byt,0,len);
                }
            fos.flush();
            fis.close();
            fos.close();

使用字节流读取中文的时候可能不会显示完整的字符,因为中文字符占用的字节多,所以java出现了字符流,专门处理文本.
Reader抽象类是表示用于读取字符流的所有类的超类
字符输入流:
FileReader:

//构造方法
        File file=new File("d:/io/portry.txt");
        FileReader fr=new FileReader(file);
        方式二:
        FileReader fr=new FileReader("d:/io/poetry.txt");

read()

//单个读取文本文件 每次读取一个字符,提升为int类型,读到文件末尾返回-1,
        int len;
        while ((len= fr.read())!=-1){
            System.out.print((char)len);
        }
//读取到数组 每次读取数组长度的字符,读取到数组,返回值是读取的有效个数,读取到文件末尾返回-1;
 char[]chs=new char[1024];
       int len;
       while ((len=fr.read(chs))!=-1){
           System.out.println(new String(chs,0,len));
       }

java.io.Writer抽象类是表示用于写出字符流的所有类的超类
FileWriter(字符输出流):
构造方法

		File file=new File("d:/io/poetry.txt");
        FileWriter fw=new FileWriter(file);
        方式二:
        FileWriter fw=new FileWriter("d:/io/poetry.txt");

writer():

       //写出一个字符
//        write(int i)
        fw.write(97);//a
        //写出字符数组
        //write(char [] chs)
        fw.write("我爱你".toCharArray());
        //写出字符串
        fw.write("哈哈哈");
        //输出指定字符串
        fw.write("666嘻嘻嘻",0,3);

!!!因为内置缓冲区的原因,FileWriter输出数据必须刷新或者关闭流,把缓冲区的数据强制刷出去,不然文件是接受不到数据的.
flush :// 刷新缓冲区,流对象可以继续使用。
close :// 关闭流,释放系统资源。关闭前会刷新缓冲区。
ResourceBundle:
使用ResourceBundle类读取properties配置文件:

 //构造方法
        ResourceBundle bundle=ResourceBundle.getBundle("a");
        //getString(String key) 获取properties文件里面key的value
        String tid = bundle.getString("tid");
        String name = bundle.getString("name");
        System.out.println(tid+","+name);

InputStreamReader:
将字节流转成字符流的桥梁
InputStreamReader isr=new InputStreamReader(new FileInputStream(“d:/io/a.txt”),“utf-8”);
OutputStreamWriter:
将字符流转成字节流的桥梁
OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream(“d:/io/a.txt”),“utf-8”);
序列化与反序列化

public class Student implements Serializable {
    private static final long serialVersionUID = -2177940198820916898L;
    private String name;
    private int age;

    public Student() {
    }

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

    public String getName() {
        return name;
    }

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

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    }
public static void main(String[] args) throws IOException, ClassNotFoundException {
        Student student=new Student("庞传钰",21);
        //创建序列化流对象必须实现Serializable接口
        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("d:/io/Student.txt"));
        oos.writeObject(student);
        oos.flush();
        //创建反序列化流
        //在反序列化过程中不能操作字节码文件
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream("d:/io/Student.txt"));
        Student s = (Student) ois.readObject();
        System.out.println(s.getName());
        System.out.println(s.getAge());
        oos.close();
    }

晚安各位

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值