溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

JAVA.io讀寫文件的方法

發(fā)布時(shí)間:2020-07-18 14:10:08 來(lái)源:億速云 閱讀:144 作者:小豬 欄目:編程語(yǔ)言

這篇文章主要講解了JAVA.io讀寫文件的方法,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

一、Java把這些不同來(lái)源和目標(biāo)的數(shù)據(jù)都統(tǒng)一抽象為數(shù)據(jù)流。

  Java語(yǔ)言的輸入輸出功能是十分強(qiáng)大而靈活的。

  在Java類庫(kù)中,IO部分的內(nèi)容是很龐大的,因?yàn)樗婕暗念I(lǐng)域很廣泛:標(biāo)準(zhǔn)輸入輸出,文件的操作,網(wǎng)絡(luò)上的數(shù)據(jù)流,字符串流,對(duì)象流,zip文件流。

  這里介紹幾種讀寫文件的方式

二、InputStream、OutputStream(字節(jié)流)

//讀取文件(字節(jié)流)
    InputStream in = new FileInputStream("d:\\1.txt");
    //寫入相應(yīng)的文件
    OutputStream out = new FileOutputStream("d:\\2.txt");
    //讀取數(shù)據(jù)
    //一次性取多少字節(jié)
    byte[] bytes = new byte[2048];
    //接受讀取的內(nèi)容(n就代表的相關(guān)數(shù)據(jù),只不過(guò)是數(shù)字的形式)
    int n = -1;
    //循環(huán)取出數(shù)據(jù)
    while ((n = in.read(bytes,0,bytes.length)) != -1) {
      //轉(zhuǎn)換成字符串
      String str = new String(bytes,0,n,"GBK"); #這里可以實(shí)現(xiàn)字節(jié)到字符串的轉(zhuǎn)換,比較實(shí)用
      System.out.println(str);
      //寫入相關(guān)文件
      out.write(bytes, 0, n);
    }
    //關(guān)閉流
    in.close();
    out.close();

三、BufferedInputStream、BufferedOutputStream(緩存字節(jié)流)使用方式和字節(jié)流差不多,但是效率更高(推薦使用)

//讀取文件(緩存字節(jié)流)
    BufferedInputStream in = new BufferedInputStream(new FileInputStream("d:\\1.txt"));
    //寫入相應(yīng)的文件
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("d:\\2.txt"));
    //讀取數(shù)據(jù)
    //一次性取多少字節(jié)
    byte[] bytes = new byte[2048];
    //接受讀取的內(nèi)容(n就代表的相關(guān)數(shù)據(jù),只不過(guò)是數(shù)字的形式)
    int n = -1;
    //循環(huán)取出數(shù)據(jù)
    while ((n = in.read(bytes,0,bytes.length)) != -1) {
      //轉(zhuǎn)換成字符串
      String str = new String(bytes,0,n,"GBK");
      System.out.println(str);
      //寫入相關(guān)文件
      out.write(bytes, 0, n);
    }
    //清楚緩存
    out.flush();
    //關(guān)閉流
    in.close();
    out.close();

四、InputStreamReader、OutputStreamWriter(字節(jié)流,這種方式不建議使用,不能直接字節(jié)長(zhǎng)度讀寫)。使用范圍用做字符轉(zhuǎn)換

//讀取文件(字節(jié)流)
    InputStreamReader in = new InputStreamReader(new FileInputStream("d:\\1.txt"),"GBK");
    //寫入相應(yīng)的文件
    OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("d:\\2.txt"));
    //讀取數(shù)據(jù)
    //循環(huán)取出數(shù)據(jù)
    byte[] bytes = new byte[1024];
    int len = -1;
    while ((len = in.read()) != -1) {
      System.out.println(len);
      //寫入相關(guān)文件
      out.write(len);
    }
    //清楚緩存
    out.flush();
    //關(guān)閉流
    in.close();
    out.close();

五、BufferedReader、BufferedWriter(緩存流,提供readLine方法讀取一行文本)

//讀取文件(字符流)
    BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("d:\\1.txt"),"GBK"));#這里主要是涉及中文
    //BufferedReader in = new BufferedReader(new FileReader("d:\\1.txt")));
    //寫入相應(yīng)的文件
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:\\2.txt"),"GBK"));
    //BufferedWriter out = new BufferedWriter(new FileWriter("d:\\2.txt"));
    //讀取數(shù)據(jù)
    //循環(huán)取出數(shù)據(jù)
    String str = null;
    while ((str = in.readLine()) != null) {
      System.out.println(str);
      //寫入相關(guān)文件
      out.write(str);
      out.newLine();
    }
    
    //清楚緩存
    out.flush();
    //關(guān)閉流
    in.close();
    out.close();

六、Reader、PrintWriter(PrintWriter這個(gè)很好用,在寫數(shù)據(jù)的同事可以格式化)

//讀取文件(字節(jié)流)
    Reader in = new InputStreamReader(new FileInputStream("d:\\1.txt"),"GBK");
    //寫入相應(yīng)的文件
    PrintWriter out = new PrintWriter(new FileWriter("d:\\2.txt"));
    //讀取數(shù)據(jù)
    //循環(huán)取出數(shù)據(jù)
    byte[] bytes = new byte[1024];
    int len = -1;
    while ((len = in.read()) != -1) {
      System.out.println(len);
      //寫入相關(guān)文件
      out.write(len);
    }
    //清楚緩存
    out.flush();
    //關(guān)閉流
    in.close();
    out.close();

七、基本的幾種用法就這么多,當(dāng)然每一個(gè)讀寫的使用都是可以分開的。為了更好的來(lái)使用io。流里面的讀寫,建議使用BufferedInputStream、BufferedOutputStream

看完上述內(nèi)容,是不是對(duì)JAVA.io讀寫文件的方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI