1. 程式人生 > 實用技巧 >Java-19 檔案操作

Java-19 檔案操作

1.File及構造方法

  • FIle類的概述:檔案和目錄路徑的抽象表示形式。

  • 構造方法:

    public File(String pathname)
    public File(String oarent,String child)
    public File(File parent,String child)
    
  • 示例

    import java.io.File;
    
    public class FileDemo1 {
    	public static void main(String[] args) {
    		// 方式1:
    		File f1 = new File("J:\\java大資料\\java基礎\\day16\\fileDemo\\123.txt");
    		// 方式2
    		File f2 = new File("J:\\java大資料\\java基礎\\day16", "fileDemo\\123.txt");
    		// 方式3
    		File f3 = new File("J:\\java大資料\\java基礎\\\\day16");
    		File f4 = new File(f3, "fileDemo\\123.txt");
    	}
    }
    
    

2.File建立刪除重新命名

  • 方法

    public boolean createNewFile()   建立檔案
    public boolean mkdir()           建立單層資料夾
    public boolean mkdirs()			建立多層資料夾
    public boolean delete()          刪除檔案或資料夾
    public boolean renameTo(File desc) 檔案重新命名
    public boolean isDirectory()     判斷是否是資料夾
    public boolean isFile()			判斷是否是檔案
    public boolean exists()			是否存在
    public boolean canRead()		是否可讀
    public boolean canWrite()		是否可寫
    public boolean isHidden()		是否隱藏
    
  • 建立檔案

    import java.io.File;
    import java.io.IOException;
    
    public class FileDemo2 {
    	public static void main(String[] args) {
    		// 建立檔案時,要保證資料夾必須存在的
    		File f1 = new File("J:\\java大資料\\java基礎\\day16\\fileDemo\\123.txt");
    		try {
    			System.out.println(f1.createNewFile());
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    
    
  • 建立單層資料夾

File f2 = new File("J:\\java大資料\\java基礎\\day16\\fileDemo2");
f2.mkdir();
  • 建立多層資料夾

    File f2 = new File("J:\\java大資料\\java基礎\\day16\\fileDemo2");
    f2.mkdirs();
    
  • 刪除(資料夾中有檔案無法刪除)

    File f2 = new File("J:\\java大資料\\java基礎\\day16\\fileDemo2");
    f2.delete();
    
  • 檔案重新命名(其實相當於檔案移動)

    File f1 = new File("J:\\java大資料\\java基礎\\day16\\fileDemo\\123.txt");
    f1.renameTo(new File("J:\\java大資料\\java基礎\\day16\\fileDemo\\abc.txt"));
    

3.檔案獲取功能

public String getAbsolutePath()	獲取絕對路徑
public String getPath()	獲取相對路徑
public String getName() 獲取檔名
public long length()	獲取檔案位元組數
public long lastMondified() 最後修改時間(毫秒)
  • 示例
import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;

public class FileDemo3 {
	public static void main(String[] args) throws IOException {
		File f = new File("J:\\java大資料\\java基礎\\day16\\fileDemo\\abc.txt");
		File f2 = new File("cba.txt"); // 相對路徑:eclipse預設工作空間
		f2.createNewFile();
		// 絕對路徑
		System.out.println(f.getAbsolutePath());
		System.out.println(f2.getAbsolutePath());
		// 相對路徑
		System.out.println(f.getPath());
		System.out.println(f2.getPath());
		// 檔名
		System.out.println(f.getName());
		// 檔案位元組數
		System.out.println(f.length());
		// 最後修改時間
		System.out.println(f.lastModified());
		Date d = new Date(f.lastModified());
		String str = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d);
		System.out.println(str);
	}
}

4.File中其他用法

public String[] list()	獲取所有子檔名稱
public File[] listFiles()	獲取所有子檔案物件
  • 示例
import java.io.File;

public class FileDemo4 {
	public static void main(String[] args) {
		File f = new File("d:/");
		String[] arr = f.list();
		for (String s : arr) {
			// 列印所有子檔名稱
			System.out.println(s);
		}
		File[] listFile = f.listFiles();
		for (File file : listFile) {
			// 列印所有子檔案路徑
			System.out.println(file.getAbsolutePath());
			// 列印所有子檔案長度
			System.out.println(file.length());
		}
	}
}

  • 列印一個資料夾所有.jpg檔案
import java.io.File;

public class FileDemo5 {
	public static void main(String[] args) {
		findFiles("D:\\meinvimg", ".jpg");
	}
	public static void findFiles(String path, String suffix) {
		// 1.把路徑封裝成檔案
		File f = new File(path);
		// 2.得到所有子檔案
		File[] listFiles = f.listFiles();
		// 3.遍歷陣列,判斷每一檔案是否是指定字尾結尾
		for (File file : listFiles) {
			String name = file.getName();
			if (name.toLowerCase().endsWith(suffix.toLowerCase()) & file.isFile()) {
				System.out.println(file.getAbsolutePath());
			}
		}
	}
}

5.檔案過濾器

  • File[] listFiles(FilenameFilter filter)

import java.io.File;
import java.io.FilenameFilter;


public class FileDemo6 {
	public static void main(String[] args) {
		findFiles("D:\\meinvimg", ".jpg");
	}
	public static void findFiles(String path, String suffix) {
		// 1.把路徑封裝成檔案
		File f = new File(path);
		// 通過過濾器得到子檔案
		// 介面通過匿名函式實現方法過載
		File[] listFiles = f.listFiles(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				// dir 表示上層資料夾
				// name 表示子檔名稱
				File ff = new File(dir, name);
				if (name.toLowerCase().endsWith(suffix.toLowerCase()) & ff.isFile()) {
					return true;
				}
				return false;
			}
		});
	}
}

過濾器方式效能更好一些

6遞迴

  • 遞迴:方法定義中呼叫方法本身的現象。

  • 遞迴注意事項:

    要有出口,否則就是死遞迴
    次數不能太多,否則就記憶體溢位
    構造方法不能遞迴使用
    
  • 示例:

    public class digui {
    	public static void main(String[] args) {
    		int n = Recursion(100);
    		System.out.println(n);
    	}
    	public static int Recursion(int num) {
    		if (num == 1) {
    			return 1;
    		}
    		return num + Recursion(num-1);
    	}
    }
    
  • 斐波那契

    public class RecursionDemo1 {
    	public static void main(String[] args) {
    		System.out.println(bronRabbit(10));
    	}
    	public static int bronRabbit(int month) {
    		if(month ==1 || month ==2) {
    			return 1;
    		}else {
    			return bronRabbit(month-1) + bronRabbit(month-2);
    		}
    	}
    }
    
    
  • 猴子吃桃:小猴子第一天摘下若干桃子,立刻吃了一半,又多吃一個,第二天早上又將剩下的桃子吃了一半,又多吃一個。依此類推,到第10天就剩下1個桃子,問,第一天一共有多少個桃子?

    	public static int eatPeach(int day) {
    		if (day == 10) {
    			return 1;
    		}else {
    			return (eatPeach(day+1) + 1) *2;
    		}
    	}
    
  • 遞迴檔案查詢

    public class digui2 {
    	public static void main(String[] args) {
    		findAllFiles("d:/", ".jpg");
    	}
    	public static void findAllFiles(String path, String suffix) {
    		File f = new File(path);
    		if (f.isFile()) {
    			if (f.getName().toLowerCase().endsWith(suffix.toLowerCase())) {
    				System.out.println(f.getAbsolutePath());
    			}
    		}else {
    			File[] listFiles = f.listFiles();
    			if (listFiles != null && listFiles.length>0) {
    				for (File file : listFiles) {
    					findAllFiles(file.getAbsolutePath(),suffix);
    				}
    			}
    		}
    	}
    }
    
    
    • 遞迴刪除
    	public static void deletefindAllFiles(String path, String suffix) {
    		File f = new File(path);
    		if (f.isFile()) {
    			f.delete();
    		}else {
    			File[] listFiles = f.listFiles();
    			if (listFiles != null && listFiles.length>0) {
    				for (File file : listFiles) {
    					findAllFiles(file.getAbsolutePath(),suffix);
    				}
    			}
    		}
    		// 刪除自己
    		f.delete();
    	}
    }
    
    

IO流

1.檔案本質

  • 儲存都是二進位制資料,我們能夠看到資料的具體形態,都是因為各自的軟體我們都做了解碼工作。

2.位元組和字元區別

  • 位元組是儲存容量基本單位,1位元組=8個二進位制位。
  • 字元是指字母、數字、漢字和各種符號。
  • 一個字元在計算機中若干個位元組的二進位制數表示

計算機中所有的資料能可以使用位元組表示,但是隻有純文字檔案才能使用字元表示。

3.讀寫和輸入輸出的關係

  • 輸入:讀
  • 輸出:寫

4.IO流分類

  • 流向劃分

    輸入流
    輸出流
    
  • 處理單位

    位元組流:以位元組為單位進行處理
    字元流:以字元為單位進行處理
    
  • 兩兩組合得到四個基類

    位元組輸入流:InputStream
    位元組輸出流:OutPutStream
    字元輸入流:Reader
    字元輸入流:Write
    
  • 以InputStream結尾,都是屬於位元組輸入流

  • 以OutputStream結尾,都是屬於位元組輸出流

  • 以Readder 結尾的,都是屬於字元輸入流

  • 以Writer結尾的,都是屬於字元輸出流

4.FileOutputStream/FileInputStream用法

4.1FileOutputStream位元組輸出流/FileInputStream位元組輸入流

  • FileOutputStream構造方法

    FileOutputStream(File file)   檔案物件
    FileOutoutStream(String path)  路徑
    FileOutoutStream(String path, boolean append) append預設為false,表示是否追加寫入
    
  • write方法

    public void write(int b)寫入一個位元組
    public void write(byte[] b)寫入一個byte陣列
    public void write(byte[] b, int off, int len)寫入一個數組,從off開始,len個長度
    
  • 示例

    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class FileOutputStreamDemo {
    	public static void main(String[] args) {
    		FileOutputStream fos  = null;
    		try {
    			// 建立一個檔案位元組輸出流的物件
    			fos = new FileOutputStream("J:\\java大資料\\java基礎\\day16\\a.txt");
    			// 寫入一個位元組
    			fos.write(23);
                 // 寫入一個byte陣列
    			fos.write("你好啊".getBytes());
    			fos.write("你好啊".getBytes(),0,2);
    
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} finally {
    			// 關閉
    			try {
    				if (fos != null) {
    					fos.close();
    				}
    			} catch (IOException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
    		
    	}
    }
    
    
  • 自動關流書寫方式,寫在try 括號後面

    import java.io.FileOutputStream;
    
    public class FileOutputStreamDemo2 {
    	public static void main(String[] args) {
    		try(
    				FileOutputStream fos = new FileOutputStream("J:\\java大資料\\java基礎\\day16\\a.txt",true);
    				) {
    				fos.write("hello".getBytes());
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
    
  • FileInputStream構造方法

    FileInputStream(File file)   檔案物件
    FileInputStream(String path)  路徑
    
  • read方法

    public int read()  一次只能讀一個位元組
    public int read([]byte b)  一次能讀一個數組個位元組
    
  • 示例:

    import java.io.FileInputStream;
    
    public class FileInputStreamDemo {
    	public static void main(String[] args) {
    		try (
    				FileInputStream fis = new FileInputStream("J:\\java大資料\\java基礎\\day16\\a.txt");
    				){
    			
    			
    			// 一次讀一個位元組
    			int a = fis.read();
    			int b = fis.read(); 
    			int c = fis.read();
    			byte[] bs = {(byte)a, (byte)b, (byte)c};
    			String s = new String(bs);
    			System.out.println(s);// 大
    			System.out.println((char)a);
    			// 一次讀多個位元組
    			byte[] bbs = new byte[1024];
    			// 讀取真正讀回來有效長度
    			int len = fis.read(bbs);
    			String ss = new String(bbs,0,len);
    			System.out.println(ss);
    			
    			
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
    
    

    如果沒有內容可讀,返回-1

  • 迴圈讀取檔案:

    import java.io.FileInputStream;
    
    public class FileInputStreamDemo2 {
    	public static void main(String[] args) {
    		try(
    				FileInputStream fis = new FileInputStream("J:\\java大資料\\java基礎\\day16\\a.txt");
    				) {
    				byte[] bs =new byte[1024];
    //				int len = fis.read(bs);
    //				System.out.println(new String(bs,0,len));
    				int len;
    				// 
    				while((len=fis.read(bs))!=-1) {
    					System.out.println(new String(bs,0,len));
    				}	
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
    

4.2檔案拷貝工具類封裝

/*
	 * @param srcPath:原始檔路徑
	 * @param destPath:目標檔案路經
	 * */
	public static void copyFile(String srcPath, String destPath) {
		long start = System.currentTimeMillis();
		try(
					FileInputStream fis = new FileInputStream(srcPath);
					FileOutputStream fos = new FileOutputStream(destPath);
				) {
			byte[] bs = new byte[1024];
			int len;
			while((len=fis.read())!=-1) {
				fos.write(bs,0,len);
			}
			long end = System.currentTimeMillis();
			System.out.println("檔案拷貝成功,耗時:" + (end - start) + "毫秒");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

4.3緩衝位元組的用法

  • 使用位元組流每次從檔案中進行讀寫時候,都需要和檔案進行大量io互動,與磁碟互動的效率其實比較低的。所以為了降低與磁碟互動次數,可以使用緩衝位元組流,緩衝位元組流先將資料放到緩衝區,我們直接和緩衝區做互動,可以提高效率。

  • 緩衝位元組流輸出流

    BufferedOutputStream(OutputStream)
    flush:緩衝輸出流中特有的方法,將緩衝區中內容寫到檔案中
    close:會先呼叫flush方法,再關流
    
  • 緩衝位元組輸入流

    BufferedInputStream(InputStream)
    BufferedInputStream(InputStream, int size) size:緩衝區大小,預設8K
    
  • 緩衝輸出流示例

    public class BufferedOutputStreamDemo {
    	public static void main(String[] args) {
    		// 使用BufferedOutputStream包裝OutputStream
    		try(
    				BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("J:\\java大資料\\java基礎\\day16\\c.txt"));
    				) {
    			bos.write(97);
    			bos.write("你好".getBytes());
    			// 刷入檔案中,當執行關閉檔案時,也會自動將資料刷入檔案
    			bos.flush();
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
    
  • 緩衝流輸入:

    import java.io.BufferedInputStream;
    import java.io.FileInputStream;
    
    public class BufferedInputStreamDemo {
    	public static void main(String[] args) {
    		
    		try(
    				// 8192為緩衝區預設大小,也可以自定義大小
    				BufferedInputStream bis = new BufferedInputStream(new FileInputStream("J:\\java大資料\\java基礎\\day16\\c.txt"),8192);
    				) {
    			byte[] bs = new byte[1024];
    			int len;
    			while((len=bis.read(bs))!=-1) {
    				System.out.println(new String(bs,0,len));
    			}
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
    

4.4 轉換流

  • 字元流可以認為位元組流與編碼表的結合。

  • OutputStreamWriter 字元輸出流

    public OutputStreamWriter(OutputStream out)
    public OutputStreamWriter(OutputStream out, String charsetName)
    
  • 方法:

    public void write(int c)
    public void write(char[] cbuf)
    public void write(char[] cbuf, int off, int len)
    public void write(String str)
    public void write(String str,int off,int len)
    
  • 示例:

    import java.io.FileOutputStream;
    import java.io.OutputStreamWriter;
    
    public class OutStreamWriteDemo {
    	public static void main(String[] args) {
    		
    		try(
    				OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("J:\\java大資料\\java基礎\\day16\\d.txt", true),"gbk");
    				) {
    			osw.write(97);
    			char[] chs = {'你', '好'};
    			osw.write(chs,0,1);
    			osw.write("你好啊");
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
    
    
  • InputStreamReader 字元輸入流

    public InputStreamReader(InputStream in)
    public InputStreamReader(InputStream in, String charsetName)
    
  • 方法:

    public int read()
    public int read(char[] cbuf)
    
  • 示例

    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    
    public class InputStreamReaderDemo {
    	public static void main(String[] args) {
    		
    		try(
    				InputStreamReader isr = new InputStreamReader(new FileInputStream("J:\\java大資料\\java基礎\\day16\\c.txt"),"gbk");
    				) {
    			char[] chs = new char[1024];
    			int len;
    			while((len=isr.read(chs))!=-1) {
    				System.out.println(new String(chs,0,len));
    			}
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
    
    
  • 轉換流拷貝 只能拷貝文字檔案

    public static void copyFileByInputStreamReaderAndOutputStreamWriter(String srcPath,String destPath) {
    		long start = System.currentTimeMillis();
    		try(
    				InputStreamReader isr =new InputStreamReader(new FileInputStream(srcPath));
    				OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(destPath));
    				) {
    			char[] chs = new char[1024];
    			int len;
    			while((len=isr.read())!=-1) {
    				osw.write(chs,0,len);
    			}
    			long end = System.currentTimeMillis();
    			System.out.println("檔案拷貝成功,耗時:" + (end - start));
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    

4.5 簡化流

  • 轉換流的名字較長,而我們常見的操作都是按照本地預設編碼實現的,所以為了簡化我們書寫,轉換流提供對應子類。

    FIleWriter
    FileReader
    
  • 寫入

    import java.io.FileWriter;
    import java.io.IOException;
    
    public class FileWriterDemo {
    	public static void main(String[] args) {
    		try(
    			// 預設編碼,無法指定編碼,可以設定是否追加
    			FileWriter fw = new FileWriter("d:/ccc.txt");
    				) {
    			fw.write("你好啊");
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
    
    
  • 讀取

    import java.io.FileReader;
    
    public class FileReaderDemo {
    	public static void main(String[] args) {
    		try(
    			FileReader fr = new FileReader("d:/ccc.txt");
    				) {
    			char[] chs = new char[1024];
    			int len;
    			while((len=fr.read(chs)) != -1) {
    				System.out.println(new String(chs,0,len));
    			}
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
    

4.6緩衝位元組流

	public static void copyFileByBufferedReaderAndBufferedWriter(String srcPath,String destPath) {
		long start = System.currentTimeMillis();
		try(
				BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(srcPath)));
				BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destPath)));
				) {
			char[] chs = new char[1024];
			int len;
			while((len=br.read(chs))!=1) {
				bw.write(chs,0,len);
			}
			long end = System.currentTimeMillis();
			System.out.println("檔案拷貝成功(緩衝字元流),耗時:" + (end - start));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

4.7 序列化和物件流

  • Java序列化是指把Java物件轉換成為位元組序列的過程,反序列化是指把位元組序列回覆為Java物件的過程。當兩個Java程序進行通訊時,傳送方需要把這個Java物件轉換成為位元組序列,然後在網路上傳送,另一方面,接收方需要從位元組序列中回覆Java物件。

  • 序列化:把物件轉成二進位制

  • 反序列化:把二進位制轉換成物件

  • 持久化:把記憶體資料儲存到硬碟上(一般指資料庫)

  • 實現序列化步驟

    1.讓類實現Serializable介面
    2.使用ObjectOutPutStream 寫資料,呼叫writeObject
    3.使用ObjectInputStream 讀資料,呼叫readObject
    
  • 寫入

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    
    
    
    public class ObjectOutputStreamDemo {
    	public static void main(String[] args) {
    		try(
    				ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("J:\\java大資料\\java基礎\\day16\\mast.dll"));
    				) {
    			Person p = new Person("小明",15);
    			oos.writeObject(p);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
    
    
  • 讀出

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    
    public class ObjectInptStreamDemo {
    	public static void main(String[] args) {
    		try (
    				ObjectInputStream ois = new ObjectInputStream(new FileInputStream("J:\\java大資料\\java基礎\\day16\\mast.dll"));
    				) {
    			Object o = ois.readObject();
    			System.out.println(o instanceof Person);
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    }
    
    

    注意:當物件修改Person類時,執行讀入會報錯:java.io.InvalidClassException錯誤,是由於模板改變造成。因為在執行寫入操作時候會生成一個版本號:serialVersionUID,修改Person物件造成版本號不一致。解決方式:

    1.重新執行寫入,在讀

    2.生成序列化id:滑鼠懸浮Person類,點選 Add default serial versionID或Add generated serial version ID

  • 練習1:把ArrayList集合中字串資料儲存到文字檔案(每條資料佔一行),從文字檔案中讀取資料(每一行為一個字串資料)到集合中,並遍歷集合。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class lianxi1 {
	public static void main(String[] args) {
		// 建立String   List
		List<String> list = new ArrayList<>();
		// 往集合中新增元素
		list.add("javase");
		list.add("javaee");
		list.add("linux");
		list.add("hadoop");
		list.add("scala");
		list.add("spark");
		// 建立緩衝字元輸出流
		try(
				// 建立緩衝字元輸出流
				BufferedWriter bw = new BufferedWriter(new FileWriter("J:\\java大資料\\java基礎\\day16\\g.txt"));
				BufferedReader br = new BufferedReader(new FileReader("J:\\java大資料\\java基礎\\day16\\g.txt"));
				) {
			// 遍歷集合獲取每個元素
			ListIterator<String> it = list.listIterator();
			while (it.hasNext()) {
				String s = it.next();
				// 把每條資料寫到檔案中,並換行
				bw.write(s);
				// 換行
				bw.newLine();
			}
			bw.close();// 先寫後讀,先關流
			
			List<String> newList = new ArrayList<>();
			// 讀取每一條資料
			String line;
			while((line=br.readLine())!=null) {
				System.out.println(line);
				// 把讀取資料儲存到集合
				newList.add(line);
			}
			// 遍歷集合列印
			for(int i=0;i<newList.size();i++) {
				System.out.println(newList.get(i));
			}

			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

4.8總結

FileInputStream/FileOutputStream	檔案位元組流
BufferedInputStream/BufferedOutputStream	緩衝位元組流
InputStreamReader/OutputStreamWriter	轉換流 把位元組流轉成字元流 可指定編碼
FileReader/FileWriter	簡化流	不能指定編碼
BufferedReader/BufferedWriter	緩衝字元流	newLine/readLine
ObjectInputStream/ObjectOutputStream	物件流  序列化、反序列化	Serialiazable writeObject/readObject