編寫一個程式,將a.txt檔案中的單詞與b.txt檔案中的單詞交替合并到c.txt檔案中,a.txt檔案中的單詞用斷行符號符分隔,b.txt檔案中用斷行符號或空格進行分隔

來源:互聯網
上載者:User

標籤:

 1 package sundemo2; 2  3 import java.io.File;   4 import java.io.FileReader;   5 import java.io.FileWriter;   6    7 public class MainClass{   8     public static void main(String[] args) throws Exception{   9         FileManager a = new FileManager("E:\\WorkSpace\\SunDemo\\data\\demo\\a.txt",new char[]{‘\n‘});  10         FileManager b = new FileManager("E:\\WorkSpace\\SunDemo\\data\\demo\\b.txt",new char[]{‘\n‘,‘ ‘});        11         FileWriter c = new FileWriter("E:\\WorkSpace\\SunDemo\\data\\demo\\c.txt");  12         String aWord = null;  13         String bWord = null;  14         while((aWord = a.nextWord()) !=null ){  15             c.write(aWord + "\n");  16             bWord = b.nextWord();  17             if(bWord != null)  18                 c.write(bWord + "\n");  19         }  20           21         while((bWord = b.nextWord()) != null){  22             c.write(bWord + "\n");  23         }     24         c.close();  25     }       26 }  27   28 class FileManager{   29     String[] words = null;  30     int pos = 0;  31     public FileManager(String filename,char[] seperators) throws Exception{  32         File f = new File(filename);  33         FileReader reader = new FileReader(f); 34         char[] buf = new char[(int)f.length()];  35         int len = reader.read(buf);  36         String results = new String(buf,0,len);  37         String regex = null;  38         if(seperators.length > 1 ){  39             regex = "" + seperators[0] + "|" + seperators[1];  40         }else{  41             regex = "" + seperators[0];  42         }  43         words = results.split(regex);  44     }  45       46     public String nextWord(){  47         if(pos == words.length)  48             return null;  49         return words[pos++];  50     }  51 }  

 

編寫一個程式,將a.txt檔案中的單詞與b.txt檔案中的單詞交替合并到c.txt檔案中,a.txt檔案中的單詞用斷行符號符分隔,b.txt檔案中用斷行符號或空格進行分隔

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.