zoukankan      html  css  js  c++  java
  • 编写一个程序,将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 }  
  • 相关阅读:
    Analysis Services features supported by SQL Server editions
    Azure DevOps to Azure AppServices
    Power BI For Competition
    Win10开机“提示语音”以及”随机播放音乐”
    Azure DevOps
    Allow Only Ajax Requests For An Action In ASP.NET Core
    Mobile CI/CD 101
    Configure SSL for SharePoint 2013
    AWS Step Function Serverless Applications
    Cordova Upload Images using File Transfer Plugin and .Net core WebAPI
  • 原文地址:https://www.cnblogs.com/xiaoluosun/p/4253246.html
Copyright © 2011-2022 走看看