java io讀取文件操作代碼實(shí)例
這篇文章主要介紹了java io讀取文件操作代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
主要分為字節(jié)讀取和字符讀取,字節(jié)讀取可以一個(gè)一個(gè)讀取和字節(jié)數(shù)組讀取,字符讀取同樣之,字符讀取適合文本讀取,字節(jié)讀取皆可以
這里直接上代碼,讀取文件的9個(gè)小demo
package com.io; import org.junit.Test; import java.io.*; public class FileTest { //1、字節(jié)流字節(jié)一個(gè)一個(gè)讀取 @Test public void test() throws IOException{ InputStream fis = new FileInputStream(new File("E:\project_test\src\com\io\readme.txt")); int len; //按字節(jié)一個(gè)一個(gè)讀取 while ((len = fis.read())!=-1){ System.out.print((char)len+"t"); }; fis.close(); } //輸出結(jié)果h e l l o w o r l d //2、字節(jié)流字節(jié)數(shù)組讀取 @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 ; //按字節(jié)數(shù)組讀取 bytes存儲(chǔ)的是讀取的數(shù)據(jù) while ((len = fis.read(bytes))!=-1){ System.out.print((new String(bytes))+"t"); }; fis.close(); } //輸出結(jié)果 he ll ow or ld //3、緩沖字節(jié)流字節(jié)一個(gè)一個(gè)讀取 @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(); } //輸出結(jié)果h e l l o w o r l d //4、緩沖字節(jié)流字節(jié)數(shù)組讀取 @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 ; //按字節(jié)數(shù)組讀取 bytes存儲(chǔ)的是讀取的數(shù)據(jù) while ((len = bis.read(bytes))!=-1){ System.out.print(new String(bytes)+"t"); }; bis.close(); } //輸出結(jié)果hel low orl drl //5、字符流一個(gè)一個(gè)讀取 @Test public void test4() throws IOException{ InputStreamReader isr = new InputStreamReader(new FileInputStream(new File("E:\project_test\src\com\io\readme.txt"))); int len ; //按字節(jié)數(shù)組讀取 bytes存儲(chǔ)的是讀取的數(shù)據(jù) while ((len = isr.read())!=-1){ System.out.print((char)len+"t"); }; isr.close(); } //6、字符流字符數(shù)組讀取 @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 ; //按字節(jié)數(shù)組讀取 bytes存儲(chǔ)的是讀取的數(shù)據(jù) while ((len = isr.read(chars))!=-1){ System.out.print(new String(chars)+"t"); }; isr.close(); } //7、緩沖字符流字符一個(gè)一個(gè)讀取 @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 ; //按字節(jié)數(shù)組讀取 bytes存儲(chǔ)的是讀取的數(shù)據(jù) while ((len = br.read())!=-1){ System.out.print((char)len+"t"); }; br.close(); } //8、緩沖字符流字符數(shù)組讀取 @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 ; //按字節(jié)數(shù)組讀取 bytes存儲(chǔ)的是讀取的數(shù)據(jù) 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")))); //按字節(jié)數(shù)組讀取 bytes存儲(chǔ)的是讀取的數(shù)據(jù) String str; while ( (str = br.readLine())!=null){ System.out.print(str+"t"); }; br.close(); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot?實(shí)現(xiàn)全局異常處理的示例代碼
本文主要介紹了SpringBoot實(shí)現(xiàn)全局異常處理,全局異常處理器的使用可以顯著提高Spring Boot項(xiàng)目的代碼質(zhì)量和可維護(hù)性,減少冗余代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06Java inputstream和outputstream使用詳解
這篇文章主要介紹了Java inputstream和outputstream使用詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08利用Sharding-Jdbc組件實(shí)現(xiàn)分表
這篇文章主要為大家詳細(xì)介紹了利用Sharding-Jdbc組件實(shí)現(xiàn)分表,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07SpringBoot 在測(cè)試時(shí)如何指定包的掃描范圍
這篇文章主要介紹了SpringBoot 在測(cè)試時(shí)如何指定包的掃描范圍,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11接口隔離原則_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了接口隔離原則,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08SpringCloud如何創(chuàng)建一個(gè)服務(wù)提供者provider
這篇文章主要介紹了SpringCloud如何創(chuàng)建一個(gè)服務(wù)提供者provider,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07maven打包插件的使用(maven-compiler-plugin、maven-dependency-plugin、m
本文主要介紹了maven打包插件的使用(maven-compiler-plugin、maven-dependency-plugin、maven-jar-plugin、maven-resources-plugin),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Springboot應(yīng)用中線程池配置詳細(xì)教程(最新2021版)
這篇文章主要介紹了Springboot應(yīng)用中線程池配置教程(2021版),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03