Java讀寫文件方法總結(jié)(推薦)
Java的讀寫文件方法在工作中相信有很多的用處的,本人在之前包括現(xiàn)在都在使用Java的讀寫文件方法來處理數(shù)據(jù)方面的輸入輸出,確實(shí)很方便。奈何我的記性實(shí)在是叫人著急,很多時(shí)候既然都會(huì)想不起來怎么寫了,不過我的Java代碼量也實(shí)在是少的可憐,所以應(yīng)該多多練習(xí)。這里做一個(gè)總結(jié),集中在一起方面今后查看。
Java讀文件
package 天才白癡夢(mèng); import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.RandomAccessFile; import java.io.Reader; public class JavaIO { /** * 采用的是操作系統(tǒng)底層默認(rèn)的編碼方式,GBK等,非UTF8 * */ /** * 以字節(jié)為單位讀取文件內(nèi)容,常用于讀取二進(jìn)制文件,比如圖片、影像、聲音等文件 * */ public static void readFileByBytes(String filename) { File file=new File(filename); FileInputStream in=null; try { System.out.println("以字節(jié)為單位讀取文件,一次讀一個(gè)字節(jié): "); in=new FileInputStream(file); int temp=0; while ((temp=in.read()) != -1) { System.out.println(temp); } in.close(); } catch (IOException e) { e.printStackTrace(); return ; } try { System.out.println("以字節(jié)為單位讀取文件,一次讀多個(gè)字節(jié): "); byte[] temp=new byte[100]; int byteread=0; in=new FileInputStream(file); JavaIO.showAvailableBytes(in); while ((byteread=in.read(temp)) != -1) { System.out.write(temp,0,byteread); } } catch (Exception e1) { e1.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e1) { } } } } /** * 以字符為單位讀取文件,常用于讀文本,數(shù)字等類型的文件 * */ public static void readFileByChar(String filename) { File file=new File(filename); Reader reader=null; try { System.out.println("以字符為單位讀取文件內(nèi)容,一次一個(gè)字節(jié):"); //InputStreamReader類:是字節(jié)向字符轉(zhuǎn)換的橋梁 reader=new InputStreamReader(new FileInputStream(file)); int temp; while ((temp=reader.read()) != -1) { if (((char)temp) != '\r') { System.out.println((char)temp); } } reader.close(); } catch (Exception e) { e.printStackTrace(); } try { System.out.println("以字符為單位讀取文件內(nèi)容,一次讀多個(gè)字節(jié): "); char[] temp=new char[30]; int charread=0; reader=new InputStreamReader(new FileInputStream(filename)); while ((charread=reader.read(temp)) != -1) { if ((charread == temp.length) && (temp[temp.length-1]!='\r')) { System.out.println(temp); } else { for (int i=0; i<charread; i++) { if (temp[i] == '\r') { break; } else { System.out.println(temp[i]); } } } } } catch (Exception e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { } } } } /** * 以行為單位讀取文件,常用于讀面向行的格式化文件 * */ public static void readFileByLine(String filename) { File file=new File(filename); BufferedReader reader=null; try { System.out.println("以行為單位讀取文件內(nèi)容,一次讀一整行: "); reader=new BufferedReader(new FileReader(file)); String temp=null; int line=1; while ((temp=reader.readLine()) != null) { System.out.println("line " + line + ": " + temp); line++; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { } } } } /** * 隨機(jī)讀取文件內(nèi)容 * */ public static void readFileByRandomAccess(String filename) { RandomAccessFile randomfile=null; try { System.out.println("隨機(jī)讀取一段文件內(nèi)容"); randomfile=new RandomAccessFile(filename,"r"); long fileLength=randomfile.length(); int beginIndex=(fileLength > 4 ? 4 : 0); randomfile.seek(beginIndex); byte[] bytes=new byte[10]; int byteread=0; while ((byteread=randomfile.read(bytes)) != -1) { System.out.write(bytes,0,byteread); } } catch (IOException e) { e.printStackTrace(); } finally { if (randomfile != null) { try { randomfile.close(); } catch (IOException e) { } } } } private static void showAvailableBytes(InputStream in) { try { System.out.println("當(dāng)前字節(jié)輸入流中的字節(jié)數(shù)為:" + in.available()); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { String filename="E:\\BaiYiShaoNian.txt"; JavaIO.readFileByBytes(filename); JavaIO.readFileByChar(filename); JavaIO.readFileByLine(filename); JavaIO.readFileByRandomAccess(filename); } }
Java寫文件
package 天才白癡夢(mèng); import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStreamWriter; public class JavaIO2 { public static void main(String[] args) throws IOException { String Path="E:\\天才白癡夢(mèng)\\JAVA"; File file=new File("E:\\天才白癡夢(mèng)\\JAVA","BaiYiShaoNian.txt"); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } /** * Java寫入文件的三種方法 * */ FileOutputStream fos=null; BufferedWriter bw=null; FileWriter fw=null; int value=1000; try { fos=new FileOutputStream(new File(Path+"fos.txt")); long begin=System.currentTimeMillis(); for (int i=1; i<=value; i++) { fos.write(5); } long end=System.currentTimeMillis(); System.out.println("TheCostTime of FileOutputStream is : " + (end-begin)); fos.close(); bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(Path+"br.txt")),"UTF8")); begin=System.currentTimeMillis(); for (int i=1; i<=value; i++) { bw.write(5); bw.newLine(); } bw.close(); end=System.currentTimeMillis(); System.out.println("TheCostTime of BufferedWriter is : " + (end-begin)); fw=new FileWriter(Path+"fw.txt"); begin=System.currentTimeMillis(); for (int i=1; i<=value; i++) { fw.write(5); } fw.close(); end=System.currentTimeMillis(); System.out.println("TheCostTime of FileWriter is : " + (end-begin)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { fos.close(); //FileOutputStream bw.close(); //BufferedWriter fw.close(); //FileWriter } catch (Exception e) { e.printStackTrace(); } } } }
以上這篇Java讀寫文件方法總結(jié)(推薦)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
深入探究一下Java中不同的線程間數(shù)據(jù)通信方式
這篇文章主要來和大家一起深入探究一下Java中不同的線程間數(shù)據(jù)通信方式,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2023-04-04SpringDataJpa的@Query注解報(bào)錯(cuò)的解決
這篇文章主要介紹了SpringDataJpa的@Query注解報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Java toString方法重寫工具之ToStringBuilder案例詳解
這篇文章主要介紹了Java toString方法重寫工具之ToStringBuilder案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08springboot解決java.lang.ArrayStoreException異常
這篇文章介紹了springboot解決java.lang.ArrayStoreException異常的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12Java應(yīng)用啟動(dòng)停止重啟Shell腳本模板server.sh
這篇文章主要為大家介紹了Java應(yīng)用啟動(dòng)、停止、重啟Shell腳本模板server.sh,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08java Nio使用NioSocket客戶端與服務(wù)端交互實(shí)現(xiàn)方式
這篇文章主要介紹了java Nio使用 NioSocket 客戶端與服務(wù)端交互實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06使用jdk1.8實(shí)現(xiàn)將list根據(jù)指定的值去分組的操作
這篇文章主要介紹了使用jdk1.8實(shí)現(xiàn)將list根據(jù)指定的值去分組的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10java實(shí)現(xiàn)計(jì)算器加法小程序(圖形化界面)
這篇文章主要介紹了Java實(shí)現(xiàn)圖形化界面的計(jì)算器加法小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05