Java編程之文件讀寫實例詳解
本文實例講述了Java編程中文件讀寫的方法。分享給大家供大家參考,具體如下:
Java中文件讀寫操作的作用是什么?
回答這個問題時應(yīng)該先想到的是Java只是一門語言,我們的一種使用工具而已,這樣答案就明晰了,就是將外來的各種數(shù)據(jù)寫入到某一個文件中去,用以保存下來;或者從文件中將其數(shù)據(jù)讀取出來,供我們使用。就如下電影過程,從網(wǎng)絡(luò)資源中下載一部電影保存于你電腦中(寫文件),當(dāng)你想看的時候就用播放器打開(讀文件)。
Java中如何對文件進(jìn)行讀寫操作?
先理一理,Java中的流分兩種,字節(jié)流和字符流,其中字節(jié)流的兩個基類是InputStream和OutputStream;字符流的兩個基類是Reader和Writer。所謂文件流,即我們對文件的操作留不開流。由此可知我們要用到某個類必然繼承如上的四個基類之一。Java中一切都是類,一切都是對象。自然會想到文件操作有哪些類:
如下四個直接用到的類:
字節(jié)流中:FileInputStream和FileOutputStream
字符流中:FileReader和FileWriter
找到類就好辦事了。剩下來的就是去找實現(xiàn)方法啦。
兩種選擇方案在這里,這就牽涉到我們?nèi)绾芜x擇合適的文件讀寫方式呢?
選擇條件的區(qū)別:
以字節(jié)為單位讀取文件,常用于讀二進(jìn)制文件,如圖片、聲音、影像等文件。
以字符為單位讀取文件,常用于讀文本,數(shù)字等類型的文件.
至于是否選擇用Buffer來對文件輸入輸出流進(jìn)行封裝,就要看文件的大小,若是大文件的讀寫,則選擇Buffer這個桶來提供文件讀寫效率。
如下是簡單運用實例:
1、運用字節(jié)流對文件進(jìn)行直接讀寫:
注:FileOutputStream(file, true);里面true參數(shù)表示不覆蓋原文件,直接在文件后面追加添加內(nèi)容。
public class FileTest { static File file = new File("d:/test.txt"); public static void main(String[] args) { try { FileOutputStream out = new FileOutputStream(file, true); String s = "Hello,world!\r\n"; out.write(s.getBytes()); out.flush(); out.close(); //FileInputStream in = new FileInputStream(file); //byte [] b = new byte[20]; //in.read(b, 0, b.length); //System.out.println(new String(b)); //in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
2、運用字符流對文件進(jìn)行直接讀寫:
public class File03 { static File file = new File("d:/test.txt"); public static void main(String[] args) { try { FileWriter fw = new FileWriter(file,true); fw.write("Hello,world!\r\n"); fw.flush(); fw.close(); //FileReader fr = new FileReader(file); //int i=0; //String s =""; //while( ( i = fr.read() )!= -1) //{ // s = s +(char)i; //} //System.out.println(s); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
文件讀寫流用Buffer封裝之后的運用:
1、對字節(jié)流封裝后對文件進(jìn)行讀寫:
static File file = new File("d:/test.txt"); public static void main(String[] args) { try { // FileOutputStream out = new FileOutputStream(file, true); // BufferedOutputStream bout = new BufferedOutputStream(out); // String s = "I have a dream!"; // bout.write(s.getBytes()); // bout.flush(); // bout.close(); FileInputStream in = new FileInputStream(file); BufferedInputStream bin = new BufferedInputStream(in); byte[] b = new byte[15]; bin.read(b); bin.close(); System.out.println(new String(b)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
2、對字符流封裝后對文件進(jìn)行讀寫:
public class File03 { static File file = new File("d:/test.txt"); public static void main(String[] args) { try { // FileWriter fw = new FileWriter(file, true); // BufferedWriter bw = new BufferedWriter(fw); // String nextLine = System.getProperty("line.separator"); // bw.write("Hello,world!" + nextLine); // bw.flush(); // bw.close(); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); int i = 0; String s = ""; String temp = null; while((temp=br.readLine())!=null) { s = s+temp; } System.out.println(s); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
希望本文所述對大家Java程序設(shè)計有所幫助。
相關(guān)文章
DoytoQuery中關(guān)于N+1查詢問題解決方案詳解
這篇文章主要為大家介紹了DoytoQuery中關(guān)于N+1查詢問題解決方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12基于Restful接口調(diào)用方法總結(jié)(超詳細(xì))
下面小編就為大家?guī)硪黄赗estful接口調(diào)用方法總結(jié)(超詳細(xì))。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08Spring基于Aop實現(xiàn)事務(wù)管理流程詳細(xì)講解
這篇文章主要介紹了Spring基于Aop實現(xiàn)事務(wù)管理流程,事務(wù)管理對于企業(yè)應(yīng)用來說是至關(guān)重要的,即使出現(xiàn)異常情況,它也可以保證數(shù)據(jù)的一致性,感興趣想要詳細(xì)了解可以參考下文2023-05-05springboot?maven?plugin報紅的解決辦法
本文主要介紹了springboot?maven?plugin報紅的解決辦法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07