按照數據流向的不一樣:輸入流和輸出流數組
按照處理數據的單位不一樣:字節流((非文本文件)視頻、音頻、圖像)、字符流(文本文件)緩存
按照角色的不一樣:節點流和處理流oop
抽象基類 節點流 緩衝流(處理流的一種,能夠提升文本操做的效率)視頻
InputStream FileInputStream BufferedInputStream對象
OutputStream FileOutputStream BufferedOutputStream資源
Reader FileReader BufferedReaderget
Writer FileWriter BufferedWriterit
public class FileInputOutputStream {
// 從硬盤中存在的文件,讀取內容加載到程序中,FileInputStream
// 要讀取的文件必定要存在,不然拋一個異常FileNotFoundException
@Test
public void testFileInputStream1() throws Exception{
// 一、建立一個File類的對象
File file = new File("hello.txt");
// 二、建立一個FileInputStream類的對象
FileInputStream fis = new FileInputStream(file);
// 三、調用FileInputStream中的方法,實現file文件的讀取
// read()方法:能夠讀取文件中的一個字節 當執行到文件結尾時,返回-1
/* int b = fis.read();
// 判斷有沒有讀取到文件結尾
while(b!=-1){
System.out.println((char)b);
b = fis.read();
}*/
int b;
while((b=fis.read())!=-1){
System.out.println((char)b);
}
// 四、關閉相應的流
fis.close();
}
// 使用try-catch-finally處理異常:保證流的關閉必定能夠執行
@Test
public void testFileIntputStream2(){
File file = new File("hello.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
int b;
while((b=fis.read())!=-1){
System.out.println((char)b);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
// 無論是否發生異常,finally中的代碼必定會執行
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 將讀取到的數據填充到字節數組中
@Test
public void testFileInputStream3(){
FileInputStream fis = null;
try {
File file = new File("hello.txt");
// 將file對象做爲FileInputStream的形參傳進來
fis = new FileInputStream(file);
// 讀取的數據要寫入的數組
byte[] b = new byte[5];
// 每次讀入到byte中字節的長度
int len;
while ((len=fis.read(b))!=-1) {
for (int i = 0; i < len; i++) {
System.out.print((char)b[i]);
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// FileOutputStream
@Test
public void testFileOutputStream(){
// 一、建立一個File對象,指定要寫入的文件位置
// 輸出的物理文件也能夠不存在,當執行過程當中,若是不存在,則自動建立,若存在,則將原來的文件覆蓋
File file = new File("hello2.txt");
// 二、建立一個FileOutputStream對象,將file對象做爲形參傳遞給FileOutputStream的構造器。
FileOutputStream fos = null;
try {
fos=new FileOutputStream(file);
// 三、執行寫入操做
fos.write(new String("hello").getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
// 四、關閉輸出流(釋放資源)
if (fos!=null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 從硬盤讀取一個文件並寫入到另外一個位置(文件的複製)
@Test
public void testFileInputOutputStream(){
// 一、提供讀入、寫出的文件
File file1 = new File("hello.txt");
File file2 = new File("hello3.txt");
// 二、提供相應的輸入流和輸出流
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
byte[] b = new byte[20];
int len;
// 三、實現文件的複製
while ((len=fis.read(b))!=-1) {
System.out.println(len);
// 從頭開始寫 寫的長度是len
fos.write(b, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fos!=null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis!=null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// 實現文件複製的方法
public static void copyFile(String src,String dest){
// 一、提供讀入、寫出的文件
File file1 = new File(src);
File file2 = new File(dest);
// 二、提供相應的輸入流和輸出流
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
byte[] b = new byte[20];
int len;
// 三、實現文件的複製
while ((len=fis.read(b))!=-1) {
// 從頭開始寫 寫的長度是len
fos.write(b, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (fos!=null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis!=null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testCopyFile(){
copyFile("1.jpg", "2.jpg");
}
}io
public class TestBuffered {
// 使用BufferedInputStream和BufferedOutputStream實現非文本複製
@Test
public void testBufferedInputOutputStream(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
// 1.提供讀寫的文件
File file1 = new File("1.jpg");
File file2 = new File("3.jpg");
// 2.建立響應的節點流 FileInputStream FileOutputStream
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
// 3.將建立的節點流對象做爲形參傳遞給緩衝流的構造器
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
// 4.具體實現文件複製的操做
byte[] b = new byte[1024];
int len;
while ((len=bis.read(b))!=-1) {
// 寫數據
bos.write(b, 0, len);
// 清除緩存
bos.flush();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
// 5.釋放資源
if (bis!=null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos!=null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testBufferReader(){
BufferedReader br = null;
BufferedWriter bw = null;
try {
File file = new File("oop.txt");
File file1 = new File("oop2.txt");
FileReader fr = new FileReader(file);
FileWriter fw = new FileWriter(file1);
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
/*char[] c = new char[1024];
int len;
while ((len=br.read(c))!=-1) {
String str = new String(c, 0, len);
bw.write(str);
bw.flush();
}*/
String str;
while ((str=br.readLine())!=null) {
bw.write(str);
bw.newLine();// 自動換行
bw.flush();
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (bw!=null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br!=null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}class