1. 程式人生 > 實用技巧 >Day16_IO流(下)

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資料夾。