码迷,mamicode.com
首页 > 其他好文 > 详细

编写一个程序,将a.txt文件中的单词与b.txt文件中的单词交替合并到c.txt文件中,a.txt文件中的单词用回车符分隔,b.txt文件中用回车或空格进行分隔

时间:2015-01-27 16:16:42      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

 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文件中用回车或空格进行分隔

标签:

原文地址:http://www.cnblogs.com/xiaoluosun/p/4253246.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!