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


     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[]{'
    '});  
    10         FileManager b = new FileManager("E:\WorkSpace\SunDemo\data\demo\b.txt",new char[]{'
    ',' '});        
    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 + "
    ");  
    16             bWord = b.nextWord();  
    17             if(bWord != null)  
    18                 c.write(bWord + "
    ");  
    19         }  
    20           
    21         while((bWord = b.nextWord()) != null){  
    22             c.write(bWord + "
    ");  
    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 }  
  • 相关阅读:
    在web项目下注册MySQL数据库驱动失败
    Servlet 调用过程
    请求时参数到后台解码时会出现乱码问题
    Request 部分功能
    dom4j增删改查
    微信消息处理JAXP-sax解析
    微信消息处理JAXP-dom解析
    inputstream与其他格式的转换
    微信消息处理
    将Gridview导出到Excel
  • 原文地址:https://www.cnblogs.com/xiaoluosun/p/4253246.html
Copyright © 2020-2023  润新知