JAVA文件讀取常用工具類(8種)
JAVA操作文件在經(jīng)常會使用到,本文匯總了部分JAVA操作文件的讀取常用工具類,希望可以幫到大家。直接上代碼。
一、讀取文件成字節(jié)
將文件內(nèi)容轉(zhuǎn)為字節(jié),需要使用到FileInputStream文件字節(jié)輸入流,將文件輸入到文件字節(jié)輸入流中,使用FileInputStream的available()方法獲取與之關(guān)聯(lián)的文件的字節(jié)數(shù),然后使用read()方法讀取數(shù)據(jù),最后記得關(guān)閉文件字節(jié)流即可。
//讀取文件成字節(jié)數(shù)組 public static byte[] file2byte(String path){ try { FileInputStream in =new FileInputStream(new File(path)); byte[] data=new byte[in.available()]; in.read(data); in.close(); return data; } catch (Exception e) { e.printStackTrace(); return null; } }
二、將字節(jié)寫入文件
與一中的讀取文件成字節(jié)類似,字節(jié)寫入文件使用FileOutputStream流,即可將字節(jié)寫入到文件中。調(diào)用FileOutputStream的write()方法,寫入數(shù)據(jù),之后關(guān)流。
//將字節(jié)數(shù)組寫入文件 public static void byte2file(String path,byte[] data) { try { FileOutputStream outputStream =new FileOutputStream(new File(path)); outputStream.write(data); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } }
三、按行讀取文件成list
經(jīng)常遇到需要將一個文檔中的文本按行輸出,這是我們可以使用BufferedReader 和 InputStreamReader流處理。具體代碼如下。
//按行讀取文件成list public static ArrayList<String> file2list(String path,String encoder) { ArrayList<String> alline=new ArrayList<String>(); try { BufferedReader in =new BufferedReader(new InputStreamReader(new FileInputStream(path),encoder)); String str=new String(); while ((str=in.readLine())!=null) { alline.add(str); } in.close(); } catch (Exception e) { e.printStackTrace(); } return alline; }
四、輸出list到文件
//輸出list到文件 public static void list2file(String path,ArrayList<String> data,String encoder) { try { BufferedWriter out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),encoder)); for (String str:data) { out.write(str); out.newLine(); } out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } }
五、從標(biāo)準(zhǔn)輸入中讀入
//從標(biāo)準(zhǔn)輸入中讀入 public static String system2str() throws IOException{ BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in)); return stdin.readLine(); }
六、讀取文件成字符串
//讀取文件成字符串 public static String file2str(String path,String encoder){ StringBuilder sb=new StringBuilder(); try { BufferedReader in =new BufferedReader(new InputStreamReader(new FileInputStream(path),encoder)); String str=new String(); while ((str=in.readLine())!=null) { sb.append(str); } in.close(); } catch (Exception e) { e.printStackTrace(); } return sb.toString(); }
七、輸出字符串到文件
//輸出字符串到文件 public static void str2file(String path,String data,String encoder){ try { BufferedWriter out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),encoder)); out.write(data); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } }
八、讀取文件成數(shù)據(jù)矩陣
//讀取文件成數(shù)據(jù)矩陣 public static ArrayList<Double> file2matrix(String path){ ArrayList<Double> alldata=new ArrayList<Double>(); try { DataInputStream in=new DataInputStream(new BufferedInputStream(new FileInputStream(path))); //利用DataInputStream來讀數(shù)據(jù) while(true) { alldata.add(in.readDouble()); } } catch (Exception e) { e.printStackTrace(); } return alldata; }
總結(jié)
對文件的操作方式還有很多,本文使用的只是一個基礎(chǔ)的參考示例,到此這篇關(guān)于JAVA文件讀取常用工具類(8種)的文章就介紹到這了,更多相關(guān)JAVA文件讀取內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JAVA基礎(chǔ)之基本數(shù)據(jù)類型全面解析
下面小編就為大家?guī)硪黄狫AVA基礎(chǔ)之基本數(shù)據(jù)類型全面解析。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-07-07在Java Spring框架中使用的設(shè)計模式有哪些
面試中常會被問道Spring框架使用了哪些設(shè)計模式?關(guān)于這個問題本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09Java讀寫鎖ReadWriteLock原理與應(yīng)用場景詳解
這篇文章主要介紹了Java讀寫鎖ReadWriteLock原理與應(yīng)用場景詳解,讀寫狀態(tài)的設(shè)計,寫鎖的獲取與釋放,鎖降級需要的朋友可以參考下2023-02-02SpringBoot結(jié)合Swagger2自動生成api文檔的方法
這篇文章主要介紹了SpringBoot結(jié)合Swagger2自動生成api文檔的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05