文章插圖

文章插圖
JAVA操作文件在經常會使用到 , 本文匯總了部分JAVA操作文件的讀取常用工具類 , 希望可以幫到大家 。直接上代碼 。
一、讀取文件成字節
將文件內容轉為字節 , 需要使用到FileInputStream文件字節輸入流 , 將文件輸入到文件字節輸入流中 , 使用FileInputStream的available()方法獲取與之關聯的文件的字節數 , 然后使用read()方法讀取數據 , 最后記得關閉文件字節流即可 。
//讀取文件成字節數組public static byte[] file2byte(String path){try {FileInputStream in =new FileInputStream(new File(path));byte[] data=http://www.mnbkw.com/jxjc/188684/new byte[in.available()];in.read(data);in.close();return data;} catch (Exception e) {e.printStackTrace();return null;}}二、將字節寫入文件與一中的讀取文件成字節類似 , 字節寫入文件使用FileOutputStream流 , 即可將字節寫入到文件中 。調用FileOutputStream的write()方法 , 寫入數據 , 之后關流 。
//將字節數組寫入文件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經常遇到需要將一個文檔中的文本按行輸出 , 這是我們可以使用BufferedReader 和 InputStreamReader流處理 。具體代碼如下 。
//按行讀取文件成listpublic 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();}}五、從標準輸入中讀入//從標準輸入中讀入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();}【java文件的讀取和寫入 java文件讀入寫出】七、輸出字符串到文件 //輸出字符串到文件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();}}八、讀取文件成數據矩陣//讀取文件成數據矩陣public static ArrayList<Double> file2matrix(String path){ArrayList<Double> alldata=http://www.mnbkw.com/jxjc/188684/new ArrayList();try {DataInputStream in=new DataInputStream(new BufferedInputStream(new FileInputStream(path)));//利用DataInputStream來讀數據while(true){alldata.add(in.readDouble());}} catch (Exception e) {e.printStackTrace();}return alldata;} 總結對文件的操作方式還有很多 , 本文使用的只是一個基礎的參考示例 , 歡迎學習交流 。感謝閱讀 。
- java簡歷如何寫 java開發工程師簡歷怎么寫
- php數組的長度 php使用什么函數可以求得數組的大小
- 數據可視化用什么軟件 數據分析可視化軟件哪些用的多
- 網絡安全模型有哪幾種 常見的網絡安全模型有哪些
- 很喜歡文學是什么梗
- 免費cdn加速服務器 免費的cdn加速服務
- 雪糕刺客是什么意思
- 多媒體應用終端 液晶多媒體終端是什么的
- 網站規劃與設計有哪些內容 網站規劃與設計的主要內容
- 全鴨的做法大全
