《Java基礎知識》Java IO流詳解


Java IO概念

1. 用於設備之間的數據傳輸。

2. Java 將操作數據流的功能封裝到了IO包中。

3. 數據流流向分:輸入流和輸出流,操作對象為文件。

4. 流按照操作數據分:字節流(通用)和字符流。

5. 將計算機語言:二進制數據轉換成文件顯示到電腦上。

 

IO包:繼承關系圖:

 

字符流:

Reader :讀取字符流,方法見API。

Writer :寫入字符流,方法見API。

案例(Writer ):

import java.io.*;

public class var {
    public static void main(String[] agrs){
        Writer writer = null;
        try{
            writer = new FileWriter("Demo.txt");
// writer = new FileWriter("Demo.txt",true); 文件續寫功能,否則會覆蓋。 writer.write(
"今天天氣真好!"); writer.flush(); } catch (IOException e) { e.printStackTrace(); }finally { if(writer != null){ try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } } } }

運行結果:

 

案例(Reader):

文件數據:

import java.io.*;

public class var {
    public static void main(String[] agrs){
        Reader reader = null;
        try{
            reader = new FileReader("Demo.txt"); 
            char[] arr = new char[5];
            int red = reader.read(arr); //red是裝到數組的長度。
            while(red != -1 ){
                System.out.println(new String(arr,0,red));
                red = reader.read(arr);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

運行結果:

 

實現一個完整的文件復制。

import java.io.*;

public class var {
    public static void main(String[] agrs){
        Reader reader = null;
        Writer writer = null;
        try{
            reader = new FileReader("Demo.txt");
            writer = new FileWriter("Dome1.txt"); //
            char[] arr = new char[5];
            int red = 0; //red是裝到數組的長度。
            while((red = reader.read(arr)) != -1 ){
                writer.write(new String(arr,0,red));
                writer.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(writer != null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

運行結果:

 

優化文件復制案例(緩沖流):

import java.io.*;

public class var {
    public static void main(String[] agrs){
        BufferedReader bufferedReader = null; //緩沖讀取流
        BufferedWriter bufferedWriter = null; //緩沖寫入流
        try{
            bufferedReader = new BufferedReader(new FileReader("Demo.txt"));
            bufferedWriter = new BufferedWriter(new FileWriter("Dome2.txt"));
            String str ;
            while((str = bufferedReader.readLine()) != null ){
                bufferedWriter.write(str);
                bufferedWriter.newLine();   //換行。
                bufferedWriter.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(bufferedReader != null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bufferedWriter != null){
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

運行結果:

 

上述IO無法處理視頻,圖片等一些文件,拷貝出來的文件也無法打開,由此我們引出字節流。

字節流:

OutputSteam:寫入字節流,方法見API。

InputSteam: 讀取字節流,方法見API。

案例(OutputSteam):

public class var {
    public static void main(String[] agrs){
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream("demo.txt");
            fileOutputStream.write("今天天氣真好,我們去玩吧!".getBytes());
            //不需要刷新。
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fileOutputStream != null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

運行結果:

 

 案例(InputSteam):

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class PublicTest {
    public static void main(String[] args) {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("demo.txt");
            int ch =0;
            byte[] arr = new byte[fileInputStream.available()];  //正式寫代碼不要使用fileInputStream.available(),因為文件比較大的時候內存會不夠用。
            while((ch = fileInputStream.read(arr)) != -1){
                System.out.println(new String(arr,0,ch));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

運行結果:

案例:復制一個視頻文件

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class PublicTest {
    public static void main(String[] args) {
        FileOutputStream fileOutputStream = null;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("video.avi");
            fileOutputStream = new FileOutputStream("video1.avi");
            int ch =0;
            byte[] arr = new byte[fileInputStream.available()];  //正式寫代碼不要使用fileInputStream.available(),因為文件比較大的時候內存會不夠用。
            while((ch = fileInputStream.read(arr)) != -1){
                fileOutputStream.write(arr);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fileOutputStream != null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

運行結果:

 

轉換流(鍵盤輸入和控制台輸出作為案例):

import java.io.*;

public class var {
    public static void main(String[] agrs){
        OutputStream out = System.out;  //控制台打印
        OutputStreamWriter outputStreamWriter = null;
        outputStreamWriter = new OutputStreamWriter(out);  // outputStreamWriter = new OutputStreamWriter(out,"GBK");  可以指定寫的編碼
        BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);

        InputStream in = System.in;   //鍵盤輸入流。
        InputStreamReader inputStreamReader = new InputStreamReader(in); // InputStreamReader inputStreamReader = new InputStreamReader(in,"BGK"); 同樣可以指定編碼
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        try {
            String line = null;
            while((line = bufferedReader.readLine()) != null){
                if("exit".equals(line)){
                    break;
                }
                bufferedWriter.write(line);
                bufferedWriter.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(bufferedWriter != null){
                    bufferedWriter.close();
                }
                if(bufferedReader != null){
                    bufferedReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

運行結果:

IO流先講到這里,Java IO流還有很多有意思的類和方法見API。

參考:https://www.cnblogs.com/runningTurtle/p/7088125.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM