Java的輸入與輸出流InputStream/OutputStream


先看InputStream和FileInputStream的結構

操作輸入流的步驟:

  1. 創建源
  2. 選擇流
  3. 操作
  4. 釋放源

 

代碼示例:

import org.testng.annotations.Test;
import java.io.*;

public class FileDemo {

    @Test
    public void fileTest() {

        //1.創建源
        File file = new File("jerry.txt");

        //2.選擇流
        InputStream in = null;

        //3.操作
        try {
            in = new FileInputStream(file);
            int temp;
            while (true) {

                if (!((temp = in.read()) != -1)) break;
                //read()返回是字符的編碼,我們要讀取的字符是英文,所以轉義成char
                System.out.print((char) temp);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
            //4.關閉流
            //注意:close()要卸載finally里面。
            //如果不寫在finally里面,而是寫在前面catch里面,當出現異常時,close就不會被執行
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

用public int read(byte[] b)方法

這個方法每次會讀取指定長度的字符。

長度有字符數組byte[]b決定。

import org.testng.annotations.Test;

import java.io.*;

public class FileDemo {

    @Test
    public void fileTest() {

        //1.創建源
        File file = new File("jerry.txt");

        //2.選擇流
        InputStream in = null;

        //3.操作
        try {
            in = new FileInputStream(file);
            int temp;
            byte[] b=new byte[1024];
            int len=-1;//len代表每次讀取到的字符的長度,也就是上面定義的2014,讀到末尾返回-1
            while ((len=in.read(b))!=-1){
                //需要用String類來把讀取到的字符數組解碼
                String str=new String(b,0,len);
                System.out.println(len);
                System.out.println(str);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //4.關閉流
                //注意:close()要卸載finally里面。
                //如果不寫在finally里面,而是寫在前面catch里面,當出現異常時,close就不會被執行
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

FileInputStram/FileoutputStream:

是字節流,通過字節的方式讀取/輸出所有的文件類型,比如:圖像、視頻、文字等等。

FileReader/FileWriter:

全字符請考慮FileReader/FileWriter

 

FileOutputStream

import org.testng.annotations.Test;

import java.io.*;

public class FileDemo {

    @Test
    public void fileTest() {

        //1.創建源
        File file = new File("jerry.txt");

        //2.選擇流
        OutputStream out = null;

        //3.操作
        try {
            out = new FileOutputStream(file);
            String msg = "天生麗質難自棄";
         //因為write方法的參數是一個字節數組,所以需要把字符串轉換成字節數組
            byte[] b = msg.getBytes();
            out.write(b);//把整個字節數組全部寫進文件
            out.write(b,3,6);//從字節數組第三個開始,寫6個字節到文件
            out.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //4.關閉流
                //注意:close()要卸載finally里面。
                //如果不寫在finally里面,而是寫在前面catch里面,當出現異常時,close就不會被執行
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

FileOutputStream(File file),這個構造方法創建的輸出流,每寫一次就會把原有內容覆蓋

FileOutputStream(File file, boolean append),這個構造方法創建的輸出流,不會覆蓋原有內容,而是在原有內容后面追加。


免責聲明!

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



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