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(); }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python實(shí)現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫水平條形圖案例2. Java 基于UDP協(xié)議實(shí)現(xiàn)消息發(fā)送3. Python編寫nmap掃描工具4. php5.6不能擴(kuò)展redis.so的解決方法5. python 爬取嗶哩嗶哩up主信息和投稿視頻6. 關(guān)于HTML5的img標(biāo)簽7. python 如何停止一個(gè)死循環(huán)的線程8. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效9. ASP.NET MVC前臺(tái)動(dòng)態(tài)添加文本框并在后臺(tái)使用FormCollection接收值10. PHP獲取時(shí)間戳等相關(guān)函數(shù)匯總
