Java
字節(jié)流: 可以處理一切文件花椭,包括二進(jìn)制臭家,音頻疲陕,視頻,doc等等钉赁。
字符流: 只能處理純文本蹄殃,全部為可見字符。.txt .html
1. 字節(jié)流
1. 文件的讀取
public void Input() {
// 1. 建立聯(lián)系 File對象
File src = new File("E:/xp/test/a.txt");
// 2. 選擇流
InputStream is = null; // 提升作用域
try {
is = new FileInputStream(src);
// 3. 操作 不斷讀取你踩,緩沖數(shù)組
byte[] car = new byte[1024];
int len = 0; // 實(shí)際讀取的長度
// 循環(huán)讀取诅岩,還回實(shí)際讀取的長度讳苦,空的話還回-1
while ((len = is.read(car)) != -1) {
// 輸出 字節(jié)數(shù)組轉(zhuǎn)成字符串
String info = new String(car,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件不存在!");
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件讀取失敗!");
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件流關(guān)閉失敗!");
}
}
}
}
2. 文件的寫出
public void Output() {
// 1. 建立聯(lián)系 File對象 目的地
File dest = new File("E:/xp/test/a.txt");
// 2. 選擇流,文件輸出流 OutputStream吩谦,F(xiàn)ileOutputStream
OutputStream os = null;
try {
// true,以追加方式寫出文件
os = new FileOutputStream(dest,true);
// 操作
String str = "WM is a good man!";
// 字符串轉(zhuǎn)字節(jié)數(shù)組
byte[] data = str.getBytes();
os.write(data,0,data.length);
os.flush(); // 強(qiáng)制刷新出去
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("文件未找到!");
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件寫出失敗!");
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("文件寫出失敗!");
}
}
}
}
3. 文件的拷貝
/**
* 文件的拷貝
* @throws IOException
*/
public void copyFile() throws IOException {
// 1. 建立聯(lián)系 源文件+目的文件
File src = new File("E:/xp/test/1.jpg");
File dest = new File("E:/xp/test/2.jpg");
// 2. 選擇流
InputStream is = new FileInputStream(src);
OutputStream os = new FileOutputStream(dest);
byte[] car = new byte[1024];
int len = 0;
// 讀取
while ((len = is.read(car)) != -1) {
// 寫出
os.write(car,0,len);
}
os.flush(); // 強(qiáng)制刷出
os.close(); // 后出現(xiàn)的先關(guān)閉
is.close();
}
2. 字符流
1. 純文本讀取
public void reader() {
// 創(chuàng)建源
File file = new File("E:/xp/test/a.txt");
// 選擇流
Reader reader = null;
try {
reader = new FileReader(file);
// 用于讀取時(shí)存放的
char[] car = new char[1024];
int len = 0;
while ((len = (reader.read(car))) != -1) {
// 字符數(shù)組轉(zhuǎn)換為字符串
String string = new String(car,0,len);
System.out.println(string);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("源文件不存在");
} catch (IOException e) {
e.printStackTrace();
}finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2. 純文本的寫入
public void write() {
// 創(chuàng)建源
File file = new File("E:/xp/test/b.txt");
// 選擇流
Writer writer = null;
try {
// true: 追加文件而不是覆蓋文件鸳谜,默認(rèn)是false
writer = new FileWriter(file, true);
// 寫出
// \r\n: 換行
String msg = "鋤禾日當(dāng)午\r\n汗滴禾下土\r\n";
writer.write(msg);
writer.append("誰知盤中餐");
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}