文件字節流、字符流、緩沖字節流、緩沖字符流、數據流


1、字節流(通常與緩沖字節流一起使用,提升效率。具體參考3)
FileOutputStream 輸出流,寫出內容到文件
FileInputStream 輸入流,從文件讀入內容到緩沖區
public class Demo {
    public static void main(String[] args) {
        File f = new File("D:/word.txt");//指定路徑、文件(夾)名
        try {//異常處理
            FileOutputStream out = new FileOutputStream(f, true);//默認不追加,而是覆蓋。
            String str = "與眾不同";
            byte b[] = str.getBytes();//字符串轉字節
            out.write(b);
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }
        try {
            FileInputStream in = new FileInputStream(f);
            byte b2[] = new byte[200];//設定緩沖區大小,200字節
            int length = in.read(b2);//讀入文本到緩沖區,返回文本字節數。
            System.out.println("文件內容是:" + new String(b2, 0, length));//0,length可以去除空格(未使用緩沖區)
        } catch (java.io.IOException e) {
            e.printStackTrace();
        }
    }
}

 2、字符流(通常與緩沖字符流一起使用,提升效率。具體參考4)。直接對字符進行處理,無需字符/字節轉換。

FileWriter
FileReader
public class Demo {
    public static void main(String[] args) {
        File f = new File("D:/word.txt");
        FileWriter fw = null;
        try {
            fw = new FileWriter(f, true);//默認覆蓋
            String str = "為所欲為";
            fw.write(str);//寫入文本
        } catch (IOException e) {
            e.printStackTrace();
        } finally {//關閉流
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        FileReader fr = null;
        try {
            fr = new FileReader(f);
            char ch[] = new char[1024];//緩沖區。由於是字符輸入輸出流,所以返回字符型
            int length = fr.read(ch);//讀取文本到緩沖區。返回文本長度
            System.out.println("文件內容是:" + new String(ch, 0, length));//消除空格(多余緩沖區)
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 3、緩沖字節流

BufferedOutputStream

BufferedInputStream

緩沖字節流可以提高效率。字節流可以認為是一個貨物一個貨物地運輸,而緩沖字節流可以把很多貨物存放到貨車上(緩存),一起運送。

使用緩沖字節輸出流時,推薦多使用flush。刷新可以不必等貨車裝滿就可以輸送。

public class Demo {
    public static void main(String[] args) {
        File f = new File("D:/word.txt");
        FileOutputStream out = null;
        BufferedOutputStream bOut = null;
        try {
            out = new FileOutputStream(f, true);//默認覆蓋
            bOut = new BufferedOutputStream(out);
            String str = "為所欲為";
            byte b[] = str.getBytes();//字符轉字節
            bOut.write(b);//寫入字節
            //使用緩沖字節輸出流時,推薦多使用flush
            bOut.flush();//刷新
        } catch (IOException e) {
            e.printStackTrace();
        } finally {//關閉流
            if (bOut != null) {
                try {
                    bOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        FileInputStream in = null;
        BufferedInputStream bIn = null;
        try {
            in = new FileInputStream(f);
            bIn = new BufferedInputStream(in);
            byte b[] = new byte[1024];
            try {
                int length=bIn.read(b);
                System.out.println("文件內容是:"+new String(b,0,length));
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (bIn != null) {
                try {
                    bIn.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 4、緩沖字符流(除了read/write功能,還有按行處理的功能。如newLine,readLine。按行處理,不需要指定緩沖區大小)

BufferedWriter

BufferedReader

public class Demo {
    public static void main(String[] args) {
        File f = new File("word.txt");
        FileWriter fw = null;
        BufferedWriter bw = null;
        try {
            fw = new FileWriter(f);//字符流
            bw = new BufferedWriter(fw);//緩沖字符流,提升效率
            String str1 = "這是第一行";
            String str2 = "這是第二行";
            bw.write(str1);
            bw.newLine();//創建新行
            bw.write(str2);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();//注意關閉順序,先關閉后創建的
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fw != null) {
                try {
                    fw.close();//關閉最初創建的
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(f);//字符流
            br = new BufferedReader(fr);//緩沖字符流,可以按行讀取
            String tmp = null;
            while ((tmp = br.readLine()) != null) {//br.readLine()只能讀取第一行,while遍歷讀取所有行
                System.out.println(tmp);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fr != null) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 5、數據流

DataOutputStream

DataInputStream

public class Demo {
    public static void main(String[] args) {
        File f = new File("word.txt");
        FileOutputStream out = null;
        DataOutputStream dos = null;
        try {
            out = new FileOutputStream(f);
            dos = new DataOutputStream(out);
            //word.txt中部分內容亂碼
            dos.writeUTF("寫入字符串數據");
            dos.writeInt(520);//整型
            dos.writeDouble(3.14f);
            dos.writeBoolean(true);//布爾型
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        FileInputStream in = null;
        DataInputStream dis = null;
        try {
            in = new FileInputStream(f);
            dis = new DataInputStream(in);
            //注意讀取順序,與寫入順序一致,否則錯誤。
            System.out.println(dis.readUTF());
            System.out.println(dis.readInt());
            System.out.println(dis.readDouble());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (dis != null) {
                try {
                    dis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

 
       


免責聲明!

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



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