Day16_IO流(下)

Day16_IO流(下)

转换流

import java.io.*;

public class Demo08 {
    public static void main(String[] args) throws IOException {
        //将键盘录入的东西输出到硬盘上的文件中去,用效率最高的方式

        //字符---缓冲---一行一行读,这句话返回的是字节流
        InputStream in =System.in;
        //字节流---》字符流:转换流,只能单向转换
        InputStreamReader isr=new InputStreamReader(in);
        //传入字符流
        BufferedReader br=new BufferedReader(isr);
        BufferedWriter bw=new BufferedWriter(new FileWriter("/Users/Desktop/java/test/g.txt"));
        //开始动作
        String str=br.readLine();
        while(str!=null){
            if(str.equals("over")){
                break;
            }
            bw.write(str);
            bw.newLine();
            str=br.readLine();
        }
        bw.close();
        br.close();
        isr.close();
        in.close();
    }
}

运行结果:输入“over"结束运行。新建g.txt,将键盘输入的字符全部写入文件g.txt中(除"over"外)。

数据流

操纵引用数据类型

序列化:程序--》硬盘

反序列化:硬盘--〉程序

Student类(引用数据类型)

import java.io.Serializable;
//Serializable查看源码,发现里面什么方法常量都没有。原因:这是一个标识。
public class Student implements Serializable {
    //序列化版本号serialVersionUID
    private static final long serialVersionUID=1001L;
    private int age;
    private String name;
    //静态变量,transient修饰的变量,不会参与到序列化中。密码保护前一半用transient修饰
    static String school;
    transient String pwd;
    
    //报错:NotSerializableException,因为类Suibian是引用数据类型,没有进行序列化操作
    //Suibian sb =new Suibian();
  
    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;
    }

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

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

Suibian类

public class Suibian {
}
import java.io.*;

public class Demo09 {
    public static void main(String[] args) throws IOException {
        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(new File("/Users/Desktop/java/test/h.txt")));
        Student s=new Student(18,"lili","123456");
        s.school="101";
        oos.writeObject(s);
        oos.close();
    }
}

运行结果:新建h.txt,并将对象s存进文件h.txt中。

import java.io.*;

public class Demo10 {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream(new File("/Users/Desktop/java/test/h.txt")));
        Object o=ois.readObject();
        System.out.println(o);
    }
}

输出

Student{age=18, name='lili', pwd='null', school='null'}

注意!!!

如果一个类中属性是引用数据类型,那么这个引用数据类型也必须要进行序列化操作,否则影响外面这个类的序列化。

文件夹的复制

import java.io.*;

public class Demo11 {
    public static void main(String[] args) throws IOException {
        copydir(new File("/Users/Desktop/java"),new File("/Users/Desktop/java2"));
    }
    public static void copydir(File srcFile,File targetFile) throws IOException {
        //如果不存在目标文件夹,就新建一个目标文件夹
        if(!targetFile.isDirectory()){
            targetFile.mkdir();
        }
        //开始对源文件夹进行遍历,然后复制
        File[] lf=srcFile.listFiles();
        for (File f:lf) {
            if(f.isFile()){
                //遍历出来的是一个文件,操作如下:
                copyFile(new File(srcFile+"/"+f.getName()),new File(targetFile+"/"+f.getName()));
            }else{//遍历出来的是一个目录,操作如下:
                copydir(new File(srcFile+"/"+f.getName()),new File(targetFile+"/"+f.getName()));
            }
        }
    }
    public static void copyFile(File srcFile,File targetFile) throws IOException {
    FileInputStream fis=new FileInputStream(srcFile);
    FileOutputStream fos=new FileOutputStream(targetFile);
    //字节流外面包着缓冲字节流
    BufferedInputStream bis=new BufferedInputStream(fis);
    BufferedOutputStream bos=new BufferedOutputStream(fos);
    //开始动作
    byte[] b=new byte[1024];
    int n=bis.read(b);
        while(n!=-1){
        bos.write(b,0,n);
        n=bis.read(b);
    }
    //关闭流:只关闭高级流就可以,低级流会自动关闭。但是为了保险,在执行一次关闭低级流的语句
        bos.close();
        bis.close();
        fos.close();
        fis.close(); 
    }
}

运行结果:复制了java文件夹。

posted @ 2020-09-02 21:59  XLR  阅读(143)  评论(0编辑  收藏  举报