Java常用IO流詳解
阿新 • • 發佈:2018-06-17
cat exce getpath tst IV trac AC output har
一、流的分類:
- 按照數據流向的不同:輸入流 輸出流
- 按照處理數據的單位的不同:字節流 字符流(處理的文本文件)
- 按照角色的不同:節點流(直接作用於文件的) 處理流
二、IO的體系
- 抽象基類 節點流(文件流) 緩沖流(處理流的一種)
- InputStream FileInputStream BufferedInputStream
- OutputStream FileOutputStream BufferedOutputStream
- Reader FileReader BufferedReader
- Writer FileWriter BufferedWriter
三、實例
基類InputStream、OutputStream 字節流既可以用來處理媒體數據也可以用來處理文本數據
1. FileInputStream和FileOutputStream
package com.yyx.test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;public class FileInputStreamOutputStreamDemo { public static void main(String[] args) { testFileInputStreamOne(); System.out.println("================="); testFileInputStreamTwo(); System.out.println("================="); testFileOutputStreamOne(); System.out.println("================="); testInputStreamOutputStreamOne(); System.out.println("================="); testInputStreamOutputStreamTwo(); } /** * 讀取文本文件 */ public static void testFileInputStreamOne() { String pathname = "F:" + File.separator + "source.txt"; File file = new File(pathname); FileInputStream fis = null; try { fis = new FileInputStream(file); // 讀取到的數據要寫入的數組 byte[] b = new byte[5]; // 每次讀入到byte中的字節的長度 int len; while ((len = fis.read(b)) != -1) { String str = new String(b, 0, len); System.out.print(str); } System.out.println(); } catch (Exception e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 讀取文本文件 */ public static void testFileInputStreamTwo() { String pathname = "F:" + File.separator + "source.txt"; File file = new File(pathname); FileInputStream fis = null; try { fis = new FileInputStream(file); int b; /* * read():讀取文件的一個字節。當執行到文件結尾時,返回-1 此方法如文件內容有中文名出現亂碼 */ while ((b = fis.read()) != -1) { System.out.print((char) b); } System.out.println(); } catch (Exception e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 寫入文本文件 */ public static void testFileOutputStreamOne() { String pathname = "F:" + File.separator + "target.txt"; File file = new File(pathname); FileOutputStream fos = null; try { fos = new FileOutputStream(file); String str = "*中國--China*"; fos.write(str.getBytes()); System.out.println("寫入成功"); fos.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 復制文本文件 */ public static void testInputStreamOutputStreamOne() { // 此處兩個文件都已存在 String sourcePathName = "F:" + File.separator + "source.txt"; String targetPathName = "F:" + File.separator + "target.txt"; File sourceFile = new File(sourcePathName); File targetFile = new File(targetPathName); FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { fileInputStream = new FileInputStream(sourceFile); fileOutputStream = new FileOutputStream(targetFile); // 3.實現文件的復制 byte[] b = new byte[1024]; int len; while ((len = fileInputStream.read(b)) != -1) { fileOutputStream.write(b, 0, len); } fileOutputStream.flush(); System.out.println("復制文本成功"); } catch (Exception e) { e.printStackTrace(); } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 復制圖片文件 */ public static void testInputStreamOutputStreamTwo() { // 此處兩個文件都已存在 String sourcePathName = "F:" + File.separator + "source.jpg"; String targetPathName = "F:" + File.separator + "target.jpg"; File sourceFile = new File(sourcePathName); File targetFile = new File(targetPathName); FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { fileInputStream = new FileInputStream(sourceFile); fileOutputStream = new FileOutputStream(targetFile); // 3.實現文件的復制 byte[] b = new byte[1024]; int len; while ((len = fileInputStream.read(b)) != -1) { fileOutputStream.write(b, 0, len); } System.out.println("復制圖片成功"); fileOutputStream.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
2. BufferedInputStream和BufferedOutputStream
package com.yyx.test; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileInputStreamOutputStreamDemo { public static void main(String[] args) { // 此處兩個文件都已存在 String sourcePathName = "F:" + File.separator + "source.jpg"; String targetPathName = "F:" + File.separator + "target.jpg"; File sourceFile = new File(sourcePathName); File targetFile = new File(targetPathName); FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; BufferedInputStream bufferedInputStream = null; BufferedOutputStream bufferedOutputStream = null; try { fileInputStream = new FileInputStream(sourceFile); fileOutputStream = new FileOutputStream(targetFile); bufferedInputStream = new BufferedInputStream(fileInputStream); bufferedOutputStream = new BufferedOutputStream(fileOutputStream); // 3.實現文件的復制 byte[] b = new byte[1024]; int len; while ((len = bufferedInputStream.read(b)) != -1) { bufferedOutputStream.write(b, 0, len); } System.out.println("復制圖片成功"); bufferedOutputStream.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (bufferedInputStream != null) { try { bufferedInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (bufferedOutputStream != null) { try { bufferedOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
基類Reader、Writer 字符流只用來處理文本數據
1. FileReader和FileWriter
package com.yyx.test; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FileReaderWriterDemo { public static void main(String[] args) { testReaderOne(); System.out.println("================="); testReaderTwo(); System.out.println("================="); testWriter(); System.out.println("================="); testReaderWriter(); } /** * 推薦:使用字節組讀取 */ public static void testReaderOne() { // 方法中文件已經存在 String pathname = "F:" + File.separator + "source.txt"; File file = new File(pathname); FileReader fr = null; try { fr = new FileReader(file); char[] buf = new char[6]; int num = 0; while ((num = fr.read(buf)) != -1) { String str = new String(buf, 0, num); System.out.print(str); } System.out.println(); } catch (Exception e) { e.printStackTrace(); } } /** * 使用單個字母讀取 */ public static void testReaderTwo() { // 方法中文件已經存在 String pathname = "F:" + File.separator + "source.txt"; File file = new File(pathname); FileReader fr = null; try { fr = new FileReader(file); int ch; while ((ch = fr.read()) != -1) { System.out.print((char) ch); } System.out.println(); } catch (Exception e) { e.printStackTrace(); } } public static void testWriter() { String pathname = "F:" + File.separator + "target.txt"; File file = new File(pathname); FileWriter fw = null; try { fw = new FileWriter(file); String str = "*中國--China*"; fw.write(str); System.out.println("寫入成功"); /* * flush和close的區別:flush刷新後可以繼續輸入,close刷新後不能繼續輸入 */ fw.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (fw != null) { try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 復制文件 */ public static void testReaderWriter() { // 此處兩個文件都已存在 String sourcePathName = "F:" + File.separator + "source.txt"; String targetPathName = "F:" + File.separator + "target.txt"; File sourceFile=new File(sourcePathName); File targetFile=new File(targetPathName); FileReader fileReader = null; FileWriter fileWriter = null; try { fileReader = new FileReader(sourceFile); // true表示追加寫入,默認是false fileWriter = new FileWriter(targetFile, true); char[] buf = new char[1024]; // 將文件讀取到buf數組中 int num = 0; while ((num = fileReader.read(buf)) != -1) { fileWriter.write(new String(buf, 0, num)); } System.out.println("復制成功"); fileWriter.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (fileReader != null) { try { fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileWriter != null) { try { fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
2. BufferedReader和BufferedWriter
- BufferedWriter:將文本寫入字符輸出流,緩沖各個字符,從而提供單個字符、數組和字符串的高效寫入
- BufferedReader:從字符輸入流中讀取文本,緩沖各個字符,從而實現字符、數組和行的高效讀取
package com.yyx.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FileReaderWriterDemo { public static void main(String[] args) { // 此處兩個文件都已存在 String sourcePathName = "F:" + File.separator + "source.txt"; String targetPathName = "F:" + File.separator + "target.txt"; File sourceFile=new File(sourcePathName); File targetFile=new File(targetPathName); FileReader fileReader = null; FileWriter fileWriter = null; BufferedReader bufferedReader = null; BufferedWriter bufferedWriter = null; try { fileReader = new FileReader(sourceFile); // true表示追加寫入,默認是false fileWriter = new FileWriter(targetFile, true); bufferedReader = new BufferedReader(fileReader); bufferedWriter = new BufferedWriter(fileWriter); char[] buf = new char[1024]; // 將文件讀取到buf數組中 int num = 0; while ((num = bufferedReader.read(buf)) != -1) { bufferedWriter.write(new String(buf, 0, num)); } System.out.println("復制成功"); bufferedWriter.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } if (bufferedWriter != null) { try { bufferedWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
Java常用IO流詳解