標籤:test imp 位元組數組 char main 字元 void span file
package cn.buaa;import java.io.File;import java.io.FileInputStream;import java.io.FileReader;import java.io.InputStream;import java.io.Reader;public class Hello { public static void main(String[] args) throws Exception { //字元輸入資料流 inputReaderStram(); //位元組輸入資料流 charReaderStream(); } public static void inputReaderStram() throws Exception{ //1:使用File 類建立一個要操作的檔案路徑 File file = new File("D:" + File.separator + "demo" + File.separator +"test.txt"); if(file.exists()){ //2:執行個體化InputStream類對象 InputStream input = new FileInputStream(file); //3:開闢一個位元組數組用於資料的讀取 byte data[] = new byte[1024]; //4:讀取資料 input.read(data); //將資料讀取到位元組數組中 System.out.println("讀取資料: " + new String(data)); input.close(); } } public static void charReaderStream() throws Exception{ //1:使用File 類建立一個要操作的檔案路徑 File file = new File("D:" + File.separator + "demo" + File.separator +"test.txt"); if(file.exists()){ //2:執行個體化Reader類對象 Reader in = new FileReader(file); //3:開闢一個字元數組用於資料的讀取 char[] data = new char[1024]; int len = in.read(data); System.out.println(new String(data)); in.close(); } } }
Java讀取本地檔案(輸入資料流)