JAVA文件讀寫例題實(shí)現(xiàn)過程解析
練習(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(); }}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP動(dòng)態(tài)網(wǎng)頁制作技術(shù)經(jīng)驗(yàn)分享2. jsp實(shí)現(xiàn)登錄驗(yàn)證的過濾器3. Xml簡介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理4. jsp文件下載功能實(shí)現(xiàn)代碼5. 如何在jsp界面中插入圖片6. JSP之表單提交get和post的區(qū)別詳解及實(shí)例7. 詳解瀏覽器的緩存機(jī)制8. vue3+ts+elementPLus實(shí)現(xiàn)v-preview指令9. .Net加密神器Eazfuscator.NET?2023.2?最新版使用教程10. phpstudy apache開啟ssi使用詳解
