本文是基於Linux環境運行,讀者閱讀前須要具有必定Linux知識java
InputStream包含以下三個方法:算法
FileInputStream繼承InputStream,使用FileInputStream讀取文件內容編程
代碼1-1數組
import java.io.FileInputStream; import java.io.IOException; public class FileInputStreamTest { public static void main(String[] args) { if (args == null || args.length == 0) { throw new RuntimeException("請輸入路徑"); } FileInputStream fis = null; try { byte[] bbuf = new byte[1024]; int hasRead = 0; fis = new FileInputStream(args[0]); while ((hasRead = fis.read(bbuf)) > 0) { System.out.print(new String(bbuf, 0, hasRead)); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (fis != null) { fis.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
代碼1-1運行結果:bash
root@lejian:/home/software/.io# cat text Hello Spring Hello Hibernate Hello MyBatis root@lejian:/home/software/.io# java FileInputStreamTest text Hello Spring Hello Hibernate Hello MyBatis
Reader包含以下三個方法:數據結構
FileReader的父類繼承自Reader,使用FileReader讀取文件spa
代碼1-2指針
import java.io.FileReader; import java.io.IOException; public class FileReaderTest { public static void main(String[] args) { if (args == null || args.length == 0) { throw new RuntimeException("請輸入路徑"); } FileReader fr = null; try { fr = new FileReader(args[0]); char[] cbuf = new char[32]; int hasRead = 0; while ((hasRead = fr.read(cbuf)) > 0) { System.out.print(new String(cbuf, 0, hasRead)); } } catch (IOException e) { // TODO: handle exception e.printStackTrace(); } finally { try { if (fr != null) { fr.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
代碼1-2運行結果:blog
root@lejian:/home/software/.io# cat text Java編程思想 算法 編譯原理 root@lejian:/home/software/.io# java FileReaderTest text Java編程思想 算法 編譯原理
代碼1-3繼承
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileOutputStreamTest { public static void main(String[] args) { if (args == null || args.length < 2) { throw new RuntimeException("請輸入兩個路徑"); } FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(args[0]); fos = new FileOutputStream(args[1]); byte[] bbuf = new byte[32]; int hasRead = 0; while ((hasRead = fis.read(bbuf)) > 0) { fos.write(bbuf, 0, hasRead); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (fis != null) { fis.close(); } if (fos != null) { fos.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
OutputStream和Writer:
Writer裏還包含以下兩個方法:
使用FileInputStream來執行輸入,FileOutputStream來執行輸出
代碼1-3運行結果:
root@lejian:/home/software/.io# cat input Java編程思想 算法 編譯原理 root@lejian:/home/software/.io# java FileOutputStreamTest input output root@lejian:/home/software/.io# cat output Java編程思想 算法 編譯原理
使用FileWriter來執行輸出
代碼1-4
import java.io.FileWriter; import java.io.IOException; public class FileWriterTest { public static void main(String[] args) { if (args == null || args.length == 0) { throw new RuntimeException("請輸入路徑"); } FileWriter fw = null; try { fw = new FileWriter(args[0]); fw.write("數據結構與算法\n"); fw.write("Python基礎教程\n"); fw.write("C和指針\n"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (fw != null) { fw.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
代碼1-4運行結果:
root@lejian:/home/software/.io# java FileWriterTest book root@lejian:/home/software/.io# cat book 數據結構與算法 Python基礎教程 C和指針
Java7以後,不少IO資源類都實現了AutoCloseable接口,只要在try語句中聲明的IO流均可以自動關閉,但有一些IO流像FileWriter這樣的輸出流,若是IO流不顯示的關閉,輸出流緩衝區的數據沒法flush到實際的物理節點,由於執行IO流的close()方法前,會先執行flush()方法