PrintStream{
特點{
}1.提供打印方法,可以對多種數據類型值進行打印,並保持數據的表現形式
2.不拋出IOException
構造函數{
1.字符串路徑
2.File對象
3.字節輸出流
}
}
小練習:將一個文件進行分割再進行合併:{
分割方法,需要指定被分割文件對象
/** * 步驟 * 1.創建輸入流,讀取文件內容 * 2.創建緩衝區, * 3.將讀取內容存入緩衝區 * 4.創建輸出流 * 5.創建文件對象 * 6.將輸出流中的內容寫入文件中 * 7.關閉資源 */ private static void cutFileMethod(File file) throws IOException { InputStream is = new FileInputStream(file); byte[] buf = new byte[1024*1024]; int len = 0; int count = 1; while((len=is.read(buf)) != -1){ File f = new File("f:\\a\\" + count++ + ".txt"); OutputStream os = new FileOutputStream(f); os.write(buf, 0 , len); os.close(); } //創建一個配置文件,包含文件分割的信息 File f = new File("f:\\a\\" + count + ".properties"); Properties p = new Properties(); p.put("filenum", "" + --count); p.put("filename", "" + file.getName()); p.store(new FileOutputStream(f), "分割文件信息"); }
將分割後的文件進行合併
/** * 思路 * 1.獲取配置文件中的內容,讀取文件名,以及分割後的個數 * 2.讀取每個部分, * 3.創建一個輸出流,將讀取的數據寫出 */ private static void joinFileMethod(File file) throws IOException, IOException { // TODO Auto-generated method stub if(!file.exists()) throw new RuntimeException(); File[] f = file.listFiles(new FilterTool(".properties")); if(f.length != 1) throw new RuntimeException("該目錄下沒有properties擴展名的文件或者不唯一"); File f_pro = f[0]; Properties p = new Properties(); p.load(new FileInputStream(f_pro)); String file_name = p.getProperty("filename"); String file_num = p.getProperty("filenum"); File[] file_part = file.listFiles(new FilterTool(".txt")); if(file_part.length != Integer.parseInt(file_num)) throw new RuntimeException("文件個數與配置文件不一致"); List<FileInputStream> list = new ArrayList<FileInputStream>(); for(File f_1 : file_part){ list.add(new FileInputStream(f_1)); } Enumeration<FileInputStream> en = Collections.enumeration(list); SequenceInputStream sis = new SequenceInputStream(en); byte[] buf = new byte[1024]; File f_join = new File(file, file_name); FileOutputStream fos = new FileOutputStream(f_join); int len = 0; while((len =sis.read(buf)) != -1){ fos.write(buf, 0, len); fos.flush(); } fos.close(); sis.close(); }
}
IO一些獨特的流{
ObjectOutputStream{
特點:操作對象的流
ObjectOutputStream:序列化(將對象存入文件)
ObjectInputStream:反序列化(從文件中讀取對象數據)
注意:
1.操作的對象必須序列化,實現Serializable接口(無需覆蓋方法)2.實現接口將對象進行排序3.保證類與對象是同一個版本4.可以自定義序列號
transient:該關鍵字修飾的數據不會被序列化例:ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L
}
RandomAccessFile{
用法:進行隨機訪問文件,創建該對象需要指定文件對象,以及模式(即:“r” “rw” “rws” “rwd”)
特點:
1.該類綁定的文件存在的時候不創建,不存在就創建(普通的輸出流不管文件存在不存在都創建新文件)2.該類的對象技能進行讀的操作,又能寫內容到文件3.內部有一個byte數組,通過指針可以操作數組的元素,同時實現隨機訪問文件4.通過getFilePointer方法獲取指針的位置,通過seek方法指定指針位置5.該對象的本質是將字節輸入流和輸出流進行封裝6.該對象的源或目的只能是文件,不能使目錄
}
PipedStream{
用法:管道流,能夠將管道的輸入輸出流聯通,常和線程一起使用,邊讀邊寫
練習(使用線程進行讀寫操作)
public class PipedStreamDemo { public static void main(String[] args) throws IOException { // 創建一個管道輸入,一個管道輸出 PipedInputStream pis = new PipedInputStream(); PipedOutputStream pos = new PipedOutputStream(); // 將兩個管道聯通 pis.connect(pos); // 創建線程 // 執行兩個管道的任務 new Thread(new Input(pis)).start(); new Thread(new Output(pos)).start(); } } class Input implements Runnable{ PipedInputStream pis = null; Input(PipedInputStream pis){ this.pis = pis; } //該管道的任務,就是進行讀的操作, public void run() { // TODO Auto-generated method stub byte[] buf = new byte[1024]; int len = 0; try { len = pis.read(buf); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(new String(buf, 0, len)); } } class Output implements Runnable{ PipedOutputStream pos = null; Output(PipedOutputStream pos){ this.pos = pos; } //該管道的任務,就是進行寫的操作, public void run() { // TODO Auto-generated method stub try { pos.write("哈哈".getBytes()); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("出錯"); }finally{ try { pos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
}
DataStream{
用法:將一個流對象包裝成能夠自由訪問基本類型數據的流,將一些信息以某種形式寫入文件或者從文件中讀取
注意:構造方法只有一個,必須指定一個流對象,
}