歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Java基礎之IO流

Java基礎之IO流

日期:2017/3/1 9:07:15   编辑:Linux編程

補充JavaSE的一些細節部分

關於IO流的一些總結

首先要介紹的是File類,File類用於對文件和目錄的一些操作

1.創建文件CreateNewFile()

2.對文件的信息的獲取getName(),getAbsolutePath()

3.判斷是否是文件isFile()

4.遍歷整個目錄下的文件 File[] listFile(),返回一個元素為文件的對象數組,可以使用方法來遍歷數組


然後引入了流的概念

以內存為參考系,以方向可以分為輸入流(讀),輸出流(寫)

以流的內容來劃分,可以分為字節流和字符流

上圖四個類都是抽象類,由抽象類可以向下派生出多個子類

字節流是將各種數據通過字節(byte)來傳輸,比如視頻,圖片都可以轉換為二進制,字符流是將數據通過字符(char)來傳輸,一般是文本文件

輸入方法都有read()方法,用於讀取文件數據,輸出方法都有writer()方法,用於將數據寫入文件


文件輸入輸出(實現類)

1.FileInputStream,FileInputStream (文件字節類)

2.FileReader,FileWriter (文件字符類)

//文件寫操作
static void write(){

    FileOutputStream out = new FileOutputStream("src\\test.txt");
    String str = "HelloIOStream";
    byte[] b = str.getbyte();   
    out.write(b);
    out.close();
}   

//文件讀操作
static void read(){
    FileOutputStream in = new FileInputStream("src\\test.txt");
    int temp;
    while((temp = in.read()) != -1){
        System.out.print((char)temp);
    }
    in.close();
    
}

轉換流(實現類)

1.InputStreamReader (將InputStream類型轉換為Reader類型)

2.OutputStreamWriter (將OutputStream類型轉換為Writer類型)

這裡補充一下標准輸入輸出流

1.標准輸入流System.in(InputStream類型)

//直接從控制台讀取
BufferedReader in= new BufferedReader(new InputStreamReader(System.in));
    String temp = null;
    while((temp = in.readLine())!=null){
                 System.out.println(temp);
    }

2.標准輸出流System.out

//直接寫在控制台中
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    out.write("HelloWorld");
    out.flush();
    out.close();

緩沖輸入輸出流(實現類)

1.BufferedInputStream,BufferedOutputStream(緩沖字節流)

BufferedInputStream in = new BufferedInputStream(new FileInputStream(src\\test.txt));

2.BufferedReader,BufferedWriter(緩沖字符流)

緩沖流只不過是對文件流的封裝,擴展了功能,可以將多個字節/字符同時讀/寫,大大提高效率

//FileInputStream作為參數   
BufferedInputStream in = new BufferedInputStream(new FileInputStream(src\\test.txt));

在BufferedReader和BufferedWriter中分別新增了方法readLine()和newLine()

readLine()方法讀取一行,返回字符串,如果沒有下一行返回null

newLine()方法在寫入文件的時候添加換行

打印流(實現類)

1.PrintOutputStream

2.PrintWriter

System.out.println();中System是類,in是其中的字段,它是OutputStream類型

所以可調用其中的print(),println()方法

對象序列化流

首先來談談什麼叫做序列化,我們可以將一些普通的數據類型通過數據流存入文件,也可以將對象的狀態持久化的保存在文件之中,而這個過程就稱之為序列化,反之從文件之中獲取對象稱之為反序列化

1.ObjectInputStream,ObjectOutputStream 對象字節流

2.ObjectReader,ObjectWriter 對象字符流

  • 構造一個對象並繼承Serializable接口,不進行序列化的字段加transient

    class Student implements Serializable{
    Student(int i , String n, int a){
            id = i;
            name = n;
            age = a;
    }
            int id;
            String name;
            transient int age;
    }
  • 將對象作為參數引入流中

    //將對象寫入到流中
    static void write(){
            Student s1 = new Student(1,"lilili",18);
            Student s2 = new Student(2,"asdasd",23);
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("src\\test.txt"));
            out.writeObject(s1);
            out.writeObject(s2);
            out.flush(); //刷新流
            out.close();
    }
    
    //將對象從流中讀取出來
    static void read()
    {
            ObjectInputStream in = new ObjectInputStream(new FileOutputStream(""src\\test.txt));
            Student temp;
            while((temp =(Student)in.readObject())!= null){
                System.out.println(temp.id+temp.name+temp.age);
        }
    }

Copyright © Linux教程網 All Rights Reserved