一、了解
在程序中所有的數據都是以流的方式進行傳輸或保存的,程序需要數據的時候要使用輸入流讀取數據,而當程序需要將一些數據保存起來的時候,就要使用輸出流完成
字節流與字符流,兩類都分為輸入流和輸出流。字節流中輸出數據主要是OutputStream,輸入流的是InputStream,在字符流中輸出主要使用Writer類完成,輸入流主要使用Reader完成(這四個都是抽象類)
所有文件的存儲都是字節的存儲,在磁盤上保留的并不是文件的字符而是先把字符編碼成字節,在存儲這些字節到磁盤中。在讀取文件時,也是一個字節一個字節地讀取,以形成字節序列。
二、文件操作類:File
在整個java.io包之中,file類是唯一的一個與文件本身操作有關的類,所謂文件本身指的是:文件的創建、刪除、重命名、取得文件大小、修改日期等。
import java.io.File;
public class TestDemo {
public static void main(String[] args) throws Exception {
File file = new File("D:\\demo.txt"); // 文件的路徑
if (file.exists()) { // 文件存在
file.delete(); // 刪除文件
} else { // 文件不存在
file.createNewFile(); // 創建新文件
}
}
}
//一些文件操作的方法
public File getParentFile();
public boolean mkdirs();
public String getName();
public boolean isDirectory();
public boolean isFile();
public long length() ;//以字節為單位返回的
public boolean renameTo(File dest);
public File[] listFiles();//列出目錄內容
三、字節流
private static void demo_read() throws IOException {
//1.創建一個讀取流對象。和制定文件關聯
FileInputStream fis = new FileInputStream("bytedemo1.txt");
//不適合用于讀取大文件數據,會發生內存溢出
//或者在讀取文件時,先讀取文件的大小
//建議使用該種讀取數據的方式
byte[] buf = new byte[1024];
int len = 0;
while ((len = fis.read(buf)) != -1){
System.out.println(new String(buf,0,len));
}
fis.close();
}
private static void demo_writer() throws IOException {
//1.創建字節輸出對象。用于操作文件
FileOutputStream fos = new FileOutputStream("bytedemo1.txt");
//2.寫數據。直接寫入目的地中
fos.write("asjflask1234654987".getBytes());
fos.close();//關閉資源動作要完成.
}
四、字符流
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("buf.txt");
BufferedReader bufr = new BufferedReader(fr);
//涉及到緩沖區的時候使用String方法讀取
String line =null;
while ((line = bufr.readLine()) != null){
System.out.println(line);
}
demo();
bufr.close();
}
public static void main(String[] args) throws IOException{
FileWriter fw = new FileWriter("buf.txt");
//為了提高寫入的效率。 使用字符流的緩沖區
//創建了一個字符寫入流的緩沖區對象,并和指定要被緩沖的流對象相關聯
BufferedWriter bufw = new BufferedWriter(fw);
//使用緩沖區的寫入方法將數據先寫入緩沖區中。
bufw.write("abcdef"+LINE_SPERATOR+"hahahah");
//使用緩沖區的刷新方法將數據刷入目的地
bufw.flush();
//關閉緩沖區,其實關閉的是緩沖區的流對象
bufw.close();//一定要關,關閉資源
fw.close();
}
練習:copy一個文件
private static void copy_2() throws IOException {
//如果不設置第二個參數,澤輸入流的緩沖區大小為2048字節,輸出流的的緩沖區 大小設置為512字節
FileInputStream fis = new FileInputStream("H:\\圖/123.avi");
BufferedInputStream bufis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream("H:\\圖/456.avi");
BufferedOutputStream bufos = new BufferedOutputStream(fos);
//用了緩沖區就不用數組了
// byte[] buf = new byte[1024];
int ch = 0;
//n FileInputStream.read(byte[] b)
//從此輸入流中將最多 b.length 個字節的數據讀入一個 byte 數組中
while ((ch = bufis.read()) != -1){
bufos.write(ch);
Bufos.flush();
}
fis.close();
fos.close();
}
private static void copy_1() throws IOException {
FileInputStream fis = new FileInputStream("H:\\圖\\123.avi");
FileOutputStream fos = new FileOutputStream("H:\\圖\\456.avi");
byte[] buf = new byte[1024];
int len = 0;
//n FileInputStream.read(byte[] b)
//從此輸入流中將最多 b.length 個字節的數據讀入一個 byte 數組中
while ((len = fis.read(buf)) != -1){
fos.write(buf,0,len);
}
fis.close();
fos.close();
}
1.輸入字節流InputStreamIO 中輸入字節流的繼承圖可見上圖,可以看出:
InputStream 是所有的輸入字節流的父類,它是一個抽象類。
ByteArrayInputStream、StringBufferInputStream、FileInputStream 是三種基本的介質流,它們分別從Byte 數組、StringBuffer、和本地文件中讀取數據。PipedInputStream 是從與其它線程共用的管道中讀取數據,與Piped 相關的知識后續單獨介紹。
ObjectInputStream 和所有FilterInputStream 的子類都是裝飾流(裝飾器模式的主角)。
2.輸出字節流OutputStream
IO 中輸出字節流的繼承圖可見上圖,可以看出:
OutputStream 是所有的輸出字節流的父類,它是一個抽象類。
ByteArrayOutputStream、FileOutputStream 是兩種基本的介質流,它們分別向Byte 數組、和本地文件中寫入數據。PipedOutputStream 是向與其它線程共用的管道中寫入數據,
ObjectOutputStream 和所有FilterOutputStream 的子類都是裝飾流。
4.字符輸入流Reader
在上面的繼承關系圖中可以看出:
Reader 是所有的輸入字符流的父類,它是一個抽象類。
CharReader、StringReader 是兩種基本的介質流,它們分別將Char 數組、String中讀取數據。PipedReader 是從與其它線程共用的管道中讀取數據。
BufferedReader 很明顯就是一個裝飾器,它和其子類負責裝飾其它Reader 對象。
FilterReader 是所有自定義具體裝飾流的父類,其子類PushbackReader 對Reader 對象進行裝飾,會增加一個行號。
InputStreamReader 是一個連接字節流和字符流的橋梁,它將字節流轉變為字符流。FileReader 可以說是一個達到此功能、常用的工具類,在其源代碼中明顯使用了將FileInputStream 轉變為Reader 的方法。我們可以從這個類中得到一定的技巧。Reader 中各個類的用途和使用方法基本和InputStream 中的類使用一致。后面會有Reader 與InputStream 的對應關系。
5.字符輸出流Writer
在上面的關系圖中可以看出:
Writer 是所有的輸出字符流的父類,它是一個抽象類。
CharArrayWriter、StringWriter 是兩種基本的介質流,它們分別向Char 數組、String 中寫入數據。PipedWriter 是向與其它線程共用的管道中寫入數據,
BufferedWriter 是一個裝飾器為Writer 提供緩沖功能。
PrintWriter 和PrintStream 極其類似,功能和使用也非常相似。
OutputStreamWriter 是OutputStream 到Writer 轉換的橋梁,它的子類FileWriter 其實就是一個實現此功能的具體類(具體可以研究一SourceCode)。功能和使用和OutputStream 極其類似,后面會有它們的對應圖。