JAVA文件讀寫(xiě)例題實(shí)現(xiàn)過(guò)程解析
練習(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(); }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP.NET MVC實(shí)現(xiàn)城市或車(chē)型三級(jí)聯(lián)動(dòng)2. IntelliJ IDEA設(shè)置條件斷點(diǎn)的方法步驟3. SSM框架JSP使用Layui實(shí)現(xiàn)layer彈出層效果4. IntelliJ IDEA導(dǎo)入jar包的方法5. Python利用百度地圖獲取兩地距離(附demo)6. 一篇文章弄清楚Ajax請(qǐng)求的五個(gè)步驟7. java基于spring boot本地上傳圖片示例解析8. ASP基礎(chǔ)入門(mén)第八篇(ASP內(nèi)建對(duì)象Application和Session)9. UTF8轉(zhuǎn)成GB2312亂碼問(wèn)題解決方案10. 刪除docker里建立容器的操作方法
