java io讀取檔案操作程式碼例項
阿新 • • 發佈:2020-01-07
這篇文章主要介紹了java io讀取檔案操作程式碼例項,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
主要分為位元組讀取和字元讀取,位元組讀取可以一個一個讀取和位元組陣列讀取,字元讀取同樣之,字元讀取適合文字讀取,位元組讀取皆可以
這裡直接上程式碼,讀取檔案的9個小demo
package com.io; import org.junit.Test; import java.io.*; public class FileTest { //1、位元組流位元組一個一個讀取 @Test public void test() throws IOException{ InputStream fis = new FileInputStream(new File("E:\project_test\src\com\io\readme.txt")); int len; //按位元組一個一個讀取 while ((len = fis.read())!=-1){ System.out.print((char)len+"t"); }; fis.close(); } //輸出結果h e l l o w o r l d //2、位元組流位元組陣列讀取 @Test public void test1() throws IOException{ InputStream fis = new FileInputStream(new File("E:\project_test\src\com\io\readme.txt")); byte[] bytes = new byte[2]; int len ; //按位元組陣列讀取 bytes儲存的是讀取的資料 while ((len = fis.read(bytes))!=-1){ System.out.print((new String(bytes))+"t"); }; fis.close(); } //輸出結果 he ll ow or ld //3、緩衝位元組流位元組一個一個讀取 @Test public void test2() throws IOException{ BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt"))); int len ; while ((len = bis.read())!=-1){ System.out.print((char)len+"t"); }; bis.close(); } //輸出結果h e l l o w o r l d //4、緩衝位元組流位元組陣列讀取 @Test public void test3() throws IOException{ BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt"))); byte[] bytes = new byte[3]; int len ; //按位元組陣列讀取 bytes儲存的是讀取的資料 while ((len = bis.read(bytes))!=-1){ System.out.print(new String(bytes)+"t"); }; bis.close(); } //輸出結果hel low orl drl //5、字元流一個一個讀取 @Test public void test4() throws IOException{ InputStreamReader isr = new InputStreamReader(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt"))); int len ; //按位元組陣列讀取 bytes儲存的是讀取的資料 while ((len = isr.read())!=-1){ System.out.print((char)len+"t"); }; isr.close(); } //6、字元流字元陣列讀取 @Test public void test5() throws IOException{ InputStreamReader isr = new InputStreamReader(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt"))); char[] chars = new char[3]; int len ; //按位元組陣列讀取 bytes儲存的是讀取的資料 while ((len = isr.read(chars))!=-1){ System.out.print(new String(chars)+"t"); }; isr.close(); } //7、緩衝字元流字元一個一個讀取 @Test public void test6() throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt")))); int len ; //按位元組陣列讀取 bytes儲存的是讀取的資料 while ((len = br.read())!=-1){ System.out.print((char)len+"t"); }; br.close(); } //8、緩衝字元流字元陣列讀取 @Test public void test7() throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt")))); char[] chars = new char[3]; int len ; //按位元組陣列讀取 bytes儲存的是讀取的資料 while ((len = br.read(chars))!=-1){ System.out.print(new String(chars)+"t"); }; br.close(); } //9、緩衝字元流按行讀取 @Test public void test8() throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt")))); //按位元組陣列讀取 bytes儲存的是讀取的資料 String str; while ( (str = br.readLine())!=null){ System.out.print(str+"t"); }; br.close(); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。