Java 輸入輸出流,Java InputStream FileInputStream OutputStream FileOutputStream


Java 輸入輸出流,Java InputStream FileInputStream OutputStream FileOutputStream

 

================================

©Copyright 蕃薯耀 2021-04-20

https://www.cnblogs.com/fanshuyao/

 

一、字節流和文件流使用,實現文件(適用所有文件)復制

InputStream、FileInputStream :獲取源文件

OutputStream、FileOutputStream:輸出目標文件

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Stream1FileStream {

    
    public static void fileInputStream() throws IOException {
        InputStream is = new FileInputStream("c:/img/0.png");
        OutputStream os = new FileOutputStream("c:/img/1.png");
        
        byte[] buffer = new byte[1024];
        int length;
        while((length = is.read(buffer)) != -1) {
            os.write(buffer, 0, length);
        }
        
        os.close();
        is.close();
        System.out.println("執行完成。");
    }
    
    public static void main(String[] args) throws IOException {
        fileInputStream();
    }
}

 

二、字節流和文件流使用,實現文件(文本文件)復制,無中文亂碼問題

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Stream1FileStreamTxt {

    
    public static void fileInputStream() throws IOException {
        
        //字節流讀取文件不會有中文亂碼的問題
        InputStream is = new FileInputStream("c:/img/0.txt");
        OutputStream os = new FileOutputStream("c:/img/1-2.txt");
        
        byte[] buffer = new byte[20];
        int length;
        while((length = is.read(buffer)) != -1) {
            //System.out.println(new String(buffer, "GBK"));//這里直接輸出會有中文亂碼的問題(因為每次讀取的字節數組存在將漢字分開的情況,這樣就出現亂碼,漢字不被分開就不會有亂碼),但生成文件是沒有中文亂碼問題的
            os.write(buffer, 0, length);
        }
        
        os.close();
        is.close();
        System.out.println("執行完成。");
    }
    
    public static void main(String[] args) throws IOException {
        fileInputStream();
    }
}

 

三、ByteArrayOutputStream字節數組使用

ByteArrayOutputStream的作用就是將多次讀取到的所有字節都緩存起來,最后一起輸入,能解決中文被截斷后出現亂碼的問題

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Stream1FileStreamTxtByteArray {

    
    public static void fileInputStream() throws IOException {
        
        //字節流讀取文件不會有中文亂碼的問題
        InputStream is = new FileInputStream("c:/img/0.txt");//0.txt為GBK編碼
        OutputStream os = new FileOutputStream("c:/img/1-3.txt");
        
        //將字節存儲在ByteArrayOutputStream中,解決中文漢字截斷后在控制台輸出存在中文亂碼的問題
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        
        byte[] buffer = new byte[20];
        int length;
        while((length = is.read(buffer)) != -1) {
            os.write(buffer, 0, length);
            byteArrayOutputStream.write(buffer, 0, length);
        }
        
        //System.out.println(byteArrayOutputStream.toString("GBK"));
        System.out.println(new String(byteArrayOutputStream.toByteArray(), "GBK"));//處理中文亂碼,GBK編碼設置,取決於文件的編碼,如果不是UTF-8,就必須設置,不然會有亂碼
        
        byteArrayOutputStream.close();
        
        os.close();
        is.close();
        System.out.println("執行完成。");
    }
    
    
    public static void fileInputStreamUtf8() throws IOException {
        
        //字節流讀取文件不會有中文亂碼的問題,但文件的編碼一定要是UTF-8,不然在輸出時就要指定相應的編碼
        InputStream is = new FileInputStream("c:/img/utf8.txt");//utf8.txt為UTF-8編碼
        OutputStream os = new FileOutputStream("c:/img/utf8-2.txt");
        
        //將字節存儲在ByteArrayOutputStream中,解決中文漢字截斷后在控制台輸出存在中文亂碼的問題
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        
        byte[] buffer = new byte[20];
        int length;
        while((length = is.read(buffer)) != -1) {
            os.write(buffer, 0, length);
            byteArrayOutputStream.write(buffer, 0, length);
        }
        
        System.out.println(byteArrayOutputStream.toString());
        
        byteArrayOutputStream.close();
        
        os.close();
        is.close();
        System.out.println("執行完成。");
    }
    
    public static void main(String[] args) throws IOException {
        //fileInputStream();
        fileInputStreamUtf8();
    }
}

 

四、字符流

字符流就是用來讀取文件內容的,如txt,但這個一般都會有中文亂碼

Reader、FileReader:輸入

Writer、FileWriter:輸出

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class Stream2FileReader {

    
    public static void fileInputStream() throws IOException {
        
        Reader r = new FileReader("c:/img/0.txt");//FileReader:直接使用會有中文亂碼
        Writer w = new FileWriter("c:/img/2.txt");
        
        char[] buffer = new char[1024];
        int length = 0;
        while((length = r.read(buffer)) != -1) {
            System.out.println(new String(buffer));
            w.write(buffer, 0, length);
        }
        
        r.close();
        w.close();
        System.out.println("執行完成。");
    }
    
    public static void main(String[] args) throws IOException {
        fileInputStream();
    }
}

 

五、InputStreamReader:將字節流轉換成字符流

將字節流轉換成字符流,轉換時,可以聲明文件編碼,解決中文亂碼問題

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Stream3FileInputStreamReader {

    
    public static void fileInputStream() throws IOException {
        
        //使用轉換流InputStreamReader解決中文亂碼問題。InputStreamReader:將字節流inputStream轉換成字符流Reader
        InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("c:/img/0.txt"), "GB2312");//InputStreamReader中的GB2312為具體文件的編碼,默認是UTF-8的編碼,不一致就會出現中文亂碼
        
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("c:/img/3.txt"), "UTF-8");//生成新的文件,使用UTF-8編碼
        
        char[] buffer = new char[1024];
        int length = 0;
        while((length = inputStreamReader.read(buffer)) != -1) {
            //System.out.println(new String(buffer));
            outputStreamWriter.write(buffer, 0, length);
        }
        
        outputStreamWriter.close();
        inputStreamReader.close();
        System.out.println("執行完成。");
    }
    
    public static void main(String[] args) throws IOException {
        fileInputStream();
    }
}

 

六、字節緩沖流

BufferedInputStream、BufferedOutputStream:加快文件輸入輸出,小文件影響不大,但大文件影響很明顯。

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Stream4FileStreamBufferStream {

    
    public static void fileInputStream() throws IOException {
        
        long start = System.currentTimeMillis();
        
        InputStream is = new FileInputStream("c:/img/00.mp4");
        OutputStream os = new FileOutputStream("c:/img/11.mp4");
        
        //緩沖流,加快文件輸入輸出
        BufferedInputStream bufferedInputStream = new BufferedInputStream(is);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(os);
        
        
        byte[] buffer = new byte[1024];
        int length;
        while((length = bufferedInputStream.read(buffer)) != -1) {
            bufferedOutputStream.write(buffer, 0, length);
        }
        
        bufferedOutputStream.close();
        bufferedInputStream.close();
        os.close();
        is.close();
        
        long end = System.currentTimeMillis();
        System.out.println("end - start=" + (end - start));//307 12767 14004
        System.out.println("執行完成。");
    }
    
    public static void fileInputStreamBuffer() throws IOException {
        long start = System.currentTimeMillis();
        
        InputStream is = new FileInputStream("c:/img/00.mp4");
        OutputStream os = new FileOutputStream("c:/img/22.mp4");
        
        //緩沖流,加快文件輸入輸出
        BufferedInputStream bufferedInputStream = new BufferedInputStream(is);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(os);
        
        
        byte[] buffer = new byte[1024];
        int length;
        while((length = bufferedInputStream.read(buffer)) != -1) {
            bufferedOutputStream.write(buffer, 0, length);
        }
        
        bufferedOutputStream.close();
        bufferedInputStream.close();
        os.close();
        is.close();
        
        long end = System.currentTimeMillis(); 
        System.out.println("end - start=" + (end - start));//276 4307 7262 7130
        System.out.println("執行完成。");
    }
    
    
    
    public static void main(String[] args) throws IOException {
        //fileInputStream();
        fileInputStreamBuffer();
    }
}

 

七、字符緩存流

BufferedReader、BufferedWriter

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Stream5BufferedReader {

    
    public static void fileInputStream() throws IOException {
        
        //使用轉換流InputStreamReader解決中文亂碼問題。InputStreamReader:將字節流inputStream轉換成字符流Reader
        InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("c:/img/0.txt"), "GB2312");//InputStreamReader中的GB2312為文件的編碼,默認是UTF-8的編碼,不一致就會出現中文亂碼
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("c:/img/5.txt"), "UTF-8");//生成新的文件,使用UTF-8編碼
        
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
        
        char[] buffer = new char[1024];
        int length = 0;
        while((length = bufferedReader.read(buffer)) != -1) {
            //System.out.println(new String(buffer));
            bufferedWriter.write(buffer, 0, length);
        }
        
        bufferedWriter.close();
        bufferedReader.close();
        outputStreamWriter.close();
        inputStreamReader.close();
        System.out.println("執行完成。");
    }
    
    public static void main(String[] args) throws IOException {
        fileInputStream();
    }
}

 

八、Java 數據流

DataInputStream、DataOutputStream

用於數據存儲,類似map的鍵值對,而且讀寫順序要一致。

省略。

.

九、Java 對象流

ObjectInputStream、ObjectOutputStream

用於在網絡中實現對象的傳輸,對象需要實現序列化接口。

省略。

 

注:

示例代碼只是往外拋異常,沒有使用Try…Catch…Finally,是不對的,只是為了演示代碼整潔,實際是要使用Try…Catch…Finally來關閉流。

 

(時間寶貴,分享不易,捐贈回饋,^_^)

 

================================

©Copyright 蕃薯耀 2021-04-20

https://www.cnblogs.com/fanshuyao/

 


免責聲明!

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



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