IO流一
字節(jié)流
FileInputStream
FileInputStream(File file)
通過打開一個到實際文件的連接來創(chuàng)建一個 FileInputStream,該文件通過文件系統(tǒng)中的 File 對象 file 指定玩郊。
FileInputStream(String name)
通過打開一個到實際文件的連接來創(chuàng)建一個 FileInputStream肢执,該文件通過文件系統(tǒng)中的路徑名 name 指定。
int read()
從此輸入流中讀取一個數(shù)據字節(jié)译红。
int read(byte[] b)
從此輸入流中將最多 b.length 個字節(jié)的數(shù)據讀入一個 byte 數(shù)組中预茄。
int read(byte[] b, int off, int len)
從此輸入流中將最多 len 個字節(jié)的數(shù)據讀入一個 byte 數(shù)組中。
off - 目標數(shù)組 b 中的起始偏移量。
len - 讀取的最大字節(jié)數(shù)耻陕。
//逐個字節(jié)讀入
public static void main(String[] args) throws IOException {
InputStream in = new FileInputStream("a.txt");
int i = 0;
while((i = in.read()) != -1) {
System.out.println((char)i);
}
in.close();
}
//帶有字節(jié)緩沖流的輸入流
public static void main(String[] args) throws IOException {
InputStream in = new FileInputStream("a.txt");
byte[] b = new byte[1024 * 8];
int len = 0;
while((len = in.read(b)) != -1) {
System.out.println(new String(b,0,len));
}
in.close();
}
FileOutputStream
當指向的文件不遜在世拙徽,會自動創(chuàng)建一個,但是創(chuàng)建不了多級目錄诗宣。
FileOutputStream(File file)
創(chuàng)建一個向指定 File 對象表示的文件中寫入數(shù)據的文件輸出流膘怕。
FileOutputStream(File file, boolean append)
創(chuàng)建一個向指定 File 對象表示的文件中寫入數(shù)據的文件輸出流。boolean表示是否追加
FileOutputStream(String name)
創(chuàng)建一個向具有指定名稱的文件中寫入數(shù)據的輸出文件流召庞。
FileOutputStream(String name, boolean append)
創(chuàng)建一個向具有指定 name 的文件中寫入數(shù)據的輸出文件流岛心。boolean表示是否追加
void write(byte[] b)
將 b.length 個字節(jié)從指定 byte 數(shù)組寫入此文件輸出流中。
void write(byte[] b, int off, int len)
將指定 byte 數(shù)組中從偏移量 off 開始的 len 個字節(jié)寫入此文件輸出流篮灼。
void write(int b)
將指定字節(jié)寫入此文件輸出流忘古。
private static void writeTxtFile(String path) throws IOException {
// 1:打開文件輸出流,流的目的地是指定的文件
FileOutputStream fos = new FileOutputStream(path);
// 2:通過流向文件寫數(shù)據
fos.write('j');
fos.write('a');
fos.write('v');
fos.write('a');
// 3:用完流后關閉流
fos.close();
}
注意:使用write(int b)方法诅诱,雖然接收的是int類型參數(shù)髓堪,但是write 的常規(guī)協(xié)定是:向輸出流寫入一個字節(jié)。要寫入的字節(jié)是參數(shù) b 的八個低位娘荡。b 的 24 個高位將被忽略旦袋。
private static void writeTxtFile(String path) throws IOException {
// 1:打開文件輸出流,流的目的地是指定的文件
FileOutputStream fos = new FileOutputStream(path);
// 2:通過流向文件寫數(shù)據
byte[] byt = "java".getBytes();
fos.write(byt);
// 3:用完流后關閉流
fos.close();
}
仔細查看a.txt文本文件發(fā)現(xiàn)上述程序每運行一次它改,老的內容就會被覆蓋掉。
那么如何不覆蓋已有信息商乎,能夠往a.txt里追加信息呢央拖。
查看API文檔,發(fā)現(xiàn)FileOutputStream類中的構造方法中有一個構造可以實現(xiàn)追加的功能FileOutputStream(File file, boolean append)
第二個參數(shù)鹉戚,append - 如果為 true鲜戒,則將字節(jié)寫入文件末尾處,而不是寫入文件開始處
private static void writeTxtFile(String path) throws IOException {
// 1:打開文件輸出流抹凳,流的目的地是指定的文件
FileOutputStream fos = new FileOutputStream(path,true);
// 2:通過流向文件寫數(shù)據
byte[] byt = "java".getBytes();
fos.write(byt);
// 3:用完流后關閉流
fos.close();
}
文件拷貝
public static void copyFile2(String srcPath, String destPath)
throws IOException {
// 打開輸入流遏餐,輸出流
FileInputStream fis = new FileInputStream(srcPath);
FileOutputStream fos = new FileOutputStream(destPath,true);
// 讀取和寫入信息
int len = 0;
// 使用字節(jié)數(shù)組,當做緩沖區(qū)
byte[] byt = new byte[1024];
while ((len = fis.read(byt)) != -1) {
fos.write(byt);
}
// 關閉流
fis.close();
fos.close();
}
字節(jié)流的異常處理
public static void copyFile(String srcPath, String destPath) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(srcPath);
fos = new FileOutputStream(destPath);
byte[] byt = new byte[1024 * 1024];
int len = 0;
while ((len = fis.read(byt)) != -1) {
fos.write(byt, 0, len);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
throw new RuntimeException(e);//拋出 catch后的代碼不會執(zhí)行
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
在最后的close代碼中可能會有問題赢底,兩個close失都,如果第一個close方法出現(xiàn)了異常,并拋出了運行時異常幸冻,那么程序還是停止了粹庞。下面的close方法就沒有執(zhí)行到。
那么為了保證close的執(zhí)行洽损,將第二個放到fianlly中即可庞溜。
public static void copyFile(String srcPath, String destPath) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(srcPath);
fos = new FileOutputStream(destPath);
byte[] byt = new byte[1024 * 1024];
int len = 0;
while ((len = fis.read(byt)) != -1) {
fos.write(byt, 0, len);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}