java 中的流_Java中的流(IO
Java中的流(IO流.
java.io包中定義了多個流類型(類或抽象類)來實現 輸入 / 輸出功能,可以從不同的角度對其進行分類:
按單位可分為: 字節流???????? (一個字節一個字節的讀取) 字符流???????? (一個字符一個字符的讀取『一個字符是兩個字節』)
按功能不同可以分為: 節點流????? (數據的傳輸) 處理流????? (數據的處理)
按方向可以分為: 輸入流???????? (相對于程序來說的) 輸出流???????? (相對于程序來說的)
以下 輸出輸出流 圖中深色的為節點流,淺色為處理流。 InputStream 繼承自該類的流都是用于向程序中輸入數據,且數據的單位為字節(8bit);
基本方法:
OutputStream 繼承自該類的流是用于程序中輸入數據,且數據的單位為字節(8bit);
基本方法:
Reader 繼承自該類的流都是用于向程序中輸入數據,且數據的單位為字符(16bit);
常用方法:
Writer 繼承自該類的流都是用于程序中輸入數據,且數據的單位為字符(16bit);
常用方法:
緩沖流: 緩沖流要“套接”在相應的節點流之上,對讀寫的數據提供了緩沖的功能,提高了讀寫的效率,可以降低硬盤的讀寫次數,同時增加了一些新的方法。 SDK提供了四種緩存流,其常用的構造方法為:
轉換流(轉換編碼):
Print流 用于輸出
在使用中如下: new InputStreamReader(System.in);//監聽讀取鍵盤的輸入 PrintWriter log = new PrintWriter(輸入流); 另外實例: ??? PrintStream ps = null; ??? try { ????? FileOutputStream fos = ????????????? new FileOutputStream(“d:\\bak\\log.dat”); ????? ps = new PrintStream(fos); ??? } catch (IOException e) { ????? e.printStackTrace(); ??? } ??? if(ps != null){ ????? System.setOut(ps); ??? }
Object流 該類的Object流是保存對象用的。 注意代碼: ??????????? FileOutputStream fos = new FileOutputStream(file); ??????????? BufferedOutputStream bos = new BufferedOutputStream(fos); 才可進行寫的操作。 要保存對象,必須要求該對象的實體類是可序列化的『Serializable』。 transient關鍵字 在序列化的實體類中,使用transient關鍵字修飾的屬性,被修飾的屬性被視作透明不存在的。 實現Externalizable接口是自己寫序列化,保存對象的輸出流方法。
具體如下圖所示:
示例代碼: ??? public void InputStreamTest() { ??????? /** ???????? * 創建文件夾以及文件 ???????? */ ??? ??? String sep = File.separator; ??????? String path = “E:” + sep + “MyTest”; ??????? String fileName = “document.txt”; ??????? File file = new File(path, fileName); ??????? if (file.exists()) { ??????????? System.out.println(“文件路徑:”+file.getAbsolutePath()); ??????????? System.out.println(“文件大小:”+file.length()); ??????? } else { ??????????? file.getParentFile().mkdirs(); ??????????? try { ??????????????? file.createNewFile(); ??????????? } catch (IOException e) { ??????????? ??? // TODO Auto-generated catch block ??????????????? e.printStackTrace(); ??????????? } ??????? }
/** ??????? * 字節讀取文件 ???????? */ ??????? try { ??????????? FileInputStream fis = new FileInputStream(file); ??????????? BufferedInputStream bis = new BufferedInputStream(fis);??? //這些Buffered**的都是處理流(緩沖) ??????????? int b ; ??????????? while ((b=bis.read())!=-1) { ??????????????? System.out.print((char)b); ??????????? } ??????????? bis.close(); ??? ??????? System.out.println(“\n\n”); ??????? } catch (FileNotFoundException e) { ??????????? // TODO Auto-generated catch block ??????????? System.out.println(“未找到文件”); ??????????? e.printStackTrace(); ??????? } catch (IOException e) { ??????????? // TODO Auto-generated catch block ??? ??????? e.printStackTrace(); ??????? }
/** ???????? * 字符讀取文件 ???????? */ ??????? try { ??????????? FileReader fileReader = new FileReader(file); ??????????? BufferedReader br = new BufferedReader(fileReader); ??????????? int b ; ??????????? while ((b=br.read())!=-1) {???? //使用br.readLine();可讀一行數據 ??????????????? System.out.print((char)b); ??????????? } ??????????? br.close(); ??? ??? } catch (FileNotFoundException e) { ??????????? // TODO Auto-generated catch block ??????????? e.printStackTrace(); ??????? } catch (IOException e) { ??????????? // TODO Auto-generated catch block ??????????? e.printStackTrace(); ??????? }
/** ???????? * 字節輸入文件 ??? ???? */ ??????? try { ??????????? FileOutputStream fos = new FileOutputStream(file); ??????????? BufferedOutputStream bos = new BufferedOutputStream(fos); ??????????? String str = new String(“所有的中文測試”); ??????????? byte[] c=str.getBytes(); ??????????? bos.write(c); ??????????? bos.flush(); ??????????? bos.close(); ??????? } catch (FileNotFoundException e) { ??????????? // TODO Auto-generated catch block ??????????? e.printStackTrace(); ??????? } catch (IOException e) { ??????????? // TODO Auto-generated catch block ??????????? e.printStackTrace(); ??????? } ??????? ??????? /** ???????? * 字符輸入文件 ???????? */ ??????? try { ??????????? FileWriter fw = new FileWriter(file,true);//寫上true代表在原有的字符串后面繼續寫入 ??????????? BufferedWriter bw = new BufferedWriter(fw); ??????????? String str = “受了點傷”; ??????????? bw.write(str.toCharArray()); ??????????? bw.flush();????? //將緩存中的數據完全打印 ??????????? bw.close(); ??????? } catch (IOException e) { ??????????? // TODO Auto-generated catch block ??????????? System.out.println(“文件讀寫錯誤”); ??????????? e.printStackTrace(); ??????? } ??????? ??????? /** ???????? * Data數據流測試,這個子類是將對象保存到了“*.txt”文件中,在文件中顯示亂碼 ???????? * 因為是將對象保存到了? txt? 文件中,所以無法正常顯示。 ???????? */ ??????? try { ??????????? FileOutputStream fos = new FileOutputStream(file); ??????????? BufferedOutputStream bos = new BufferedOutputStream(fos); ??????????? DataOutputStream dos = new DataOutputStream(bos); ??????????? double dou = Math.random(); ??????????? System.out.println(dou); ??????????? dos.writeDouble(dou); ??????????? dos.flush(); ??????????? dos.close(); ??????? } catch (FileNotFoundException e1) { ??????????? // TODO Auto-generated catch block ??????????? e1.printStackTrace(); ??????? } catch (IOException e) { ??????????? // TODO Auto-generated catch block ??????????? e.printStackTrace(); ??????? } ??????? ??????? /** ??????? * Data數據流在程序之間的傳遞 ??????? */ ??????? try { ??????????? ByteArrayOutputStream baos = new ByteArrayOutputStream(); ??????????? DataOutputStream dos = new DataOutputStream(baos); ??????????? dos.writeDouble(Math.random()); ??????????? dos.writeBoolean(true); ??? ??????? ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ??????????? System.out.println(bais.available()); ??????????? DataInputStream dis = new DataInputStream(bais); ??????????? System.out.println(dis.readDouble()); ??????????? System.out.println(dis.readBoolean()); ??????????? dos.close(); ??????? ??? dis.close(); ??????? } catch (FileNotFoundException e) { ??????????? // TODO Auto-generated catch block ??????????? e.printStackTrace(); ??? ??? } catch (IOException e) { ??????????? // TODO Auto-generated catch block ??????????? e.printStackTrace(); ??????? } ??????? ??????? /保存對象///stu對象必須實現了Serializable接口,也就是必須可序列化 ??? ??? public void forSaveObject(Student stu){ ??????? try { ??????????? FileOutputStream fos = new FileOutputStream(“E:/dd.dox”); ??????????? ObjectOutputStream oos = new ObjectOutputStream(fos); ??????????? oos.writeObject(stu); ??????????? oos.flush(); ??????????? oos.close(); ??????? } catch (FileNotFoundException e) { ??????????? // TODO Auto-generated catch block ??????????? e.printStackTrace(); ??????? } catch (IOException e) { ??????????? // TODO Auto-generated catch block ??? ??????? e.printStackTrace(); ??????? } ??? }
}
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的java 中的流_Java中的流(IO的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 包 过程 job_mysql
- 下一篇: java 向父类_Java基础——面向对