溫馨提示×

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

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

JAVA文件讀寫例題的實(shí)現(xiàn)方法

發(fā)布時(shí)間:2020-06-29 16:24:49 來源:億速云 閱讀:146 作者:清晨 欄目:開發(fā)技術(shù)

這篇文章主要介紹JAVA文件讀寫例題的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

練習(xí)

有這樣的一個(gè)words數(shù)組,數(shù)組中每個(gè)字符串的格式為“詞性:單詞”

String[] words = {"verb:eat","verb:drink","verb:sleep","verb:play","noun:rice","noun:meat","noun:hand","noun:hair"};

根據(jù)單詞性質(zhì)動(dòng)詞verb全部存入verb.txt文件中

根據(jù)單詞性質(zhì)名詞noun全部存入noun.txt文件中

package readandwrite;
/*1.有這樣的一個(gè)words數(shù)組,數(shù)組中每個(gè)字符串的格式為“詞性:單詞”
    String[] words = {"verb:eat","verb:drink","verb:sleep","verb:play","noun:rice","noun:meat","noun:hand","noun:hair"};
    根據(jù)單詞性質(zhì)動(dòng)詞verb全部存入verb.txt文件中
    根據(jù)單詞性質(zhì)名詞noun全部存入noun.txt文件中

*/

import java.io.*;


public class FileReadAndWrite {
  public static void main(String args[]) throws IOException {
    //WORDS數(shù)組
    String[] words = {"verb:eat","verb:drink","verb:sleep","verb:play","noun:rice","noun:meat","noun:hand","noun:hair"};
    FileOutputStream outFile1 = new FileOutputStream("verb.txt");
    FileOutputStream outFile2 = new FileOutputStream("noun.txt");
    OutputStream out1 = new BufferedOutputStream(outFile1);
    OutputStream out2 = new BufferedOutputStream(outFile2);

    for(int i=0;i<words.length;i++){
      if(words[i].startsWith("verb")){
        byte[] bytes1 = words[i].getBytes();
        out1.write(bytes1);
      }
      if(words[i].startsWith("noun")){
        byte[] bytes2 = words[i].getBytes();
        out2.write(bytes2);
      }
    }
    out1.close();
    out2.close();
  }
}

以上是JAVA文件讀寫例題的實(shí)現(xiàn)方法的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(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