I/O
是input/output
的縮寫,I/O
技術是很是實用的技術,用於處理設備之間的數據傳輸。如:讀寫文件、網絡通訊等。在Java
程序中,對於數據的輸入/輸出操做以:流(stream
)的方式進行。java
流的分類數組
按操做數據單位不一樣分爲:字節流(8 bit),字符流(16 bit)網絡
按數據流的流向不一樣分爲:輸入流,輸出流code
按流的角色的不一樣分爲:節點流,處理流視頻
(抽象基類) | 字節流 | 字符流 |
---|---|---|
輸入流 | inputStream | Reader |
輸出流 | OutputStream | Writer |
分類 | 字節輸入流 | 字節輸出流 | 字符輸入流 | 字符輸出流 |
---|---|---|---|---|
抽象基類 | InputStream | OutputStream | Reader | Writer |
訪問文件 | FileInputStream | FileOutputStream | FileReader | FileWriter |
訪問數組 | ByteArrayInputStream | ByteArrayOutputStream | CharArrayReader | CharArrayWriter |
訪問管道 | PipedInputStream | PipedOutputStream | PipedReader | PipedWriter |
訪問字符串 | StringReader | StringWriter | ||
緩衝流 | BufferedInputStream | BufferedOutputStream | BufferedReader | BufferedWriter |
轉換流 | InputStreamReader | OutputStreamWriter | ||
對象流 | ObjectInputStream | ObjectOutputStream | ||
FilterInputStream | FilterOutputStream | |||
打印流 | PrintStream | printWiter | ||
推回輸入流 | PushbackInputStream | PushbackReader | ||
特殊流 | DataInputStream | DataOutputStream |
對於處理文本文件(
.txt,.java,.c,.cpp
),使用字符流處理。對象對於非文本文件(
.jpg,.mp3,.mp4,.avi,.doc,.ppt,....
),使用字節流處理ip
read()
: 返回讀入的一個字符。若是達到文件末尾,返回-1。
異常處理:爲了保證流資源一旦執行後,可以執行關閉操做,須要使用try-catch-findlly
處理。
讀入的文件必定要存在,不然就會報FileNotfoundException
。資源
public void testFileReader() { FileReader fr = null; try { // 1.實例化File類的對象,指明要操做的文件 File file = new File("hello.txt");// 相較於當前Module // 2.提供具體的流 fr = new FileReader(file); // 3.數據的讀入過程 int data = fr.read(); while(data != -1) { System.out.print((char)data); data = fr.read(); } } catch (IOException e) { e.printStackTrace(); } finally { // 4.流的關閉操做 try { if(fr != null) fr.close(); } catch (IOException e) { e.printStackTrace(); } } }
輸出操做,對應的File能夠不存在的。字符串
File
對應的硬盤中的文件不存在,則在輸出的過程當中自動建立文件。
File
對應的硬盤中的文件存在:input
FileWriter(file,false)
或FileWriter(file)
:對源文件作覆蓋。FileWriter(file,true)
:對源文件內作追加。public void testFileWriter() { FileWriter fileWriter = null; try { // 1.提供File類的對象,指明寫出到的文件 File file = new File("hello1.txt"); // 2.提供FileWriter的對象,用於數據的寫出 // FileWriter fileWriter = new FileWriter(file); // FileWriter fileWriter = new FileWriter(file,true); FileWriter fileWriter = new FileWriter(file,false); // 3.寫出的操做 fileWriter.write("I have a dream!\n"); fileWriter.write("you need to have a dream!\n"); } catch (IOException e) { e.printStackTrace(); } finally { // 4.流資源的關閉操做 if (fileWriter != null) { try { fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } }
字符輸入流與字符輸出流案例
public void testFileReaderFileWriter() { FileReader fileReader = null; FileWriter fileWriter = null; try { // 1.建立File類對象,指明讀入和寫出的文件 File srcFile1 = new File("hello.txt"); File srcFile2 = new File("hello2.txt"); // 2.建立輸入流與輸出流對象 fileReader = new FileReader(srcFile1); fileWriter = new FileWriter(srcFile2); // 3.數據的讀入與寫出操做 char[] cbuf = new char[5]; int len;// 記錄每次讀入到數組中字符的個數 while((len = fileReader.read(cbuf)) != -1) { // 從0開始,每次寫出len個字符 fileWriter.write(cbuf,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { // 4.關閉流資源 try { if (fileWriter != null) fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } try { if (fileReader != null) fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } }
FileInputStream
與FileOutputStream
:使用方法與字符流一致,在處理文本文件是有可能出現亂碼。
public void testFileInputOutputStream() { FileInputStream inputStream = null; FileOutputStream outputStream = null; try { File srcFile = new File("image.png"); File destFile = new File("image2.png"); inputStream = new FileInputStream(srcFile); outputStream = new FileOutputStream(destFile); // 複製的過程 byte[] buffer = new byte[5]; int len; while((len = inputStream.read(buffer)) != -1) { // 從0開始,每次寫出len個字符 outputStream.write(buffer,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (outputStream != null) outputStream.close(); } catch (IOException e) { e.printStackTrace(); } try { if (inputStream != null) inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
做用:提供流的讀取、寫入的速度。
緩衝流資源關閉:先關閉外層流(緩衝流),在關閉內層的流(節點流)。
在關閉外層流(緩衝流)的同時,內層流(節點流)也會自動的進行關閉。全部內層的關閉流能夠省略。
// 非文本文件的複製 public void BufferedStreamTest() { BufferedInputStream bufferedInputStream = null; BufferedOutputStream bufferedOutputStream = null; try { // 1.造文件 File srcFile = new File("image.png"); File destFile = new File("image3.png"); // 2.造流 // 2.1 造節點流 FileInputStream fileInputStream = new FileInputStream(srcFile); FileOutputStream fileOutputStream = new FileOutputStream(destFile); // 2.2 造緩衝流 bufferedInputStream = new BufferedInputStream(fileInputStream); bufferedOutputStream = new BufferedOutputStream(fileOutputStream); // 3.複製的細節:讀取、寫入 byte[] bytes = new byte[10]; int len; while ((len = bufferedInputStream.read(bytes)) != -1) { bufferedOutputStream.write(bytes,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { // 4.資源關閉 // 要求:先關閉外層流,在關閉內層的流 try { if (bufferedOutputStream != null) bufferedOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } try { if (bufferedInputStream != null) bufferedInputStream.close(); } catch (IOException e) { e.printStackTrace(); } // 在關閉外層流的同時,內層流也會自動的進行關閉。全部內層的關閉流能夠省略 // fileOutputStream.close(); // fileInputStream.close(); } }
flush()
:只要是緩衝輸出流都會有flush()
刷新操做。
// 實現文件複製的方法 public void copyFileWithBuffered(String srcPath,String destPath) { BufferedInputStream bufferedInputStream = null; BufferedOutputStream bufferedOutputStream = null; try { // 1.造文件 File srcFile = new File(srcPath); File destFile = new File(destPath); // 2.造流 // 2.1 造節點流 FileInputStream fileInputStream = new FileInputStream(srcFile); FileOutputStream fileOutputStream = new FileOutputStream(destFile); // 2.2 造緩衝流 bufferedInputStream = new BufferedInputStream(fileInputStream); bufferedOutputStream = new BufferedOutputStream(fileOutputStream); // 3.複製的細節:讀取、寫入 byte[] bytes = new byte[1024]; int len; while ((len = bufferedInputStream.read(bytes)) != -1) { bufferedOutputStream.write(bytes,0,len); // bufferedOutputStream.flush();// 刷新緩衝區 } } catch (IOException e) { e.printStackTrace(); } finally { // 4.資源關閉 // 要求:先關閉外層流,在關閉內層的流 try { if (bufferedOutputStream != null) bufferedOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } try { if (bufferedInputStream != null) bufferedInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } public void testCopyFile() { long start = System.currentTimeMillis(); String srcPath = "F:\\視頻.avi"; String destPath = "F:\\視頻2.avi"; copyFileWithBuffered(srcPath,destPath); long end = System.currentTimeMillis(); System.out.println( "複製文件消耗時間:" + (end - start)); }
readLine()
:一次輸出一行,不報行換行符。
newLine()
:提供換行操做。
public void testBufferedReaderBufferedWriter() { BufferedReader bufferedReader = null; BufferedWriter bufferedWriter = null; try { bufferedReader = new BufferedReader(new FileReader(new File("hello.txt"))); bufferedWriter = new BufferedWriter(new FileWriter(new File("hello3.txt"))); String data; while((data = bufferedReader.readLine()) != null) { bufferedWriter.write(data);// data中不包含換行符 bufferedWriter.newLine();// 提供換行操做 } } catch (IOException e) { e.printStackTrace(); } finally { // 關閉資源 try { if (bufferedWriter != null) bufferedWriter.close(); } catch (IOException e) { e.printStackTrace(); } try { if (bufferedReader != null) bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } }
轉換流提供了在字節流和字符流之間的轉換:
InputStreamReader
(屬於字符流):將一個字節的輸入流,轉換爲字符的輸入流。
OutputStreamWriter
(屬於字符流):將一個字符的輸出流,轉換爲字節的輸出流。
public void test2() { InputStreamReader inputStreamReader = null; OutputStreamWriter outputStreamWriter = null; try { File file1 = new File("dbcp.txt"); File file2 = new File("dbcp_gbk.txt"); FileInputStream fileInputStream = new FileInputStream(file1); FileOutputStream fileOutputStream = new FileOutputStream(file2); inputStreamReader = new InputStreamReader(fileInputStream,"UTF-8"); outputStreamWriter = new OutputStreamWriter(fileOutputStream,"GBK"); char[] cbuf = new char[1024]; int len; while((len = inputStreamReader.read(cbuf)) != -1) { outputStreamWriter.write(cbuf,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (outputStreamWriter != null) outputStreamWriter.close(); } catch (IOException e) { e.printStackTrace(); } try { if (inputStreamReader != null) inputStreamReader.close(); } catch (IOException e) { e.printStackTrace(); } } }