JAVA文件讀寫例題實現(xiàn)過程解析
練習
有這樣的一個words數(shù)組,數(shù)組中每個字符串的格式為“詞性:單詞”
String[] words = {'verb:eat','verb:drink','verb:sleep','verb:play','noun:rice','noun:meat','noun:hand','noun:hair'};
根據(jù)單詞性質(zhì)動詞verb全部存入verb.txt文件中
根據(jù)單詞性質(zhì)名詞noun全部存入noun.txt文件中
package readandwrite;/*1.有這樣的一個words數(shù)組,數(shù)組中每個字符串的格式為“詞性:單詞” String[] words = {'verb:eat','verb:drink','verb:sleep','verb:play','noun:rice','noun:meat','noun:hand','noun:hair'}; 根據(jù)單詞性質(zhì)動詞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(); }}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 理解PHP5中static和const關(guān)鍵字2. Java如何基于反射機制獲取不同的類3. Android table布局開發(fā)實現(xiàn)簡單計算器4. IntelliJ IDEA安裝插件的方法步驟5. python 代碼實現(xiàn)k-means聚類分析的思路(不使用現(xiàn)成聚類庫)6. php模擬實現(xiàn)斗地主發(fā)牌7. asp判斷某個文件是否存在的函數(shù)8. Android 在 res/layout 文件夾 下創(chuàng)建一個 子文件夾實例9. PHP安全-命令注入10. Python random庫使用方法及異常處理方案

網(wǎng)公網(wǎng)安備