您好,登錄后才能下訂單哦!
這篇文章主要介紹JAVA文件讀寫例題的實現方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
練習
有這樣的一個words數組,數組中每個字符串的格式為“詞性:單詞”
String[] words = {"verb:eat","verb:drink","verb:sleep","verb:play","noun:rice","noun:meat","noun:hand","noun:hair"};
根據單詞性質動詞verb全部存入verb.txt文件中
根據單詞性質名詞noun全部存入noun.txt文件中
package readandwrite; /*1.有這樣的一個words數組,數組中每個字符串的格式為“詞性:單詞” String[] words = {"verb:eat","verb:drink","verb:sleep","verb:play","noun:rice","noun:meat","noun:hand","noun:hair"}; 根據單詞性質動詞verb全部存入verb.txt文件中 根據單詞性質名詞noun全部存入noun.txt文件中 */ import java.io.*; public class FileReadAndWrite { public static void main(String args[]) throws IOException { //WORDS數組 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文件讀寫例題的實現方法的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。