編寫一個程序,將a.txt文件中的單詞與b.txt文件中的單詞交替合併到c.txt文件中,a.txt文件中的單詞用回車符分隔,b.txt文件中用回車或空格進行分隔

這種方法好點:java

package cglib;數組

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class StringNumber {
    
    public static void main(String args[]) throws Exception{
       
        FileManager a=new FileManager("D:\\文件\\QC處理\\2016年11月\\a.txt",new char[]{'\n'});
        FileManager b=new FileManager("D:\\文件\\QC處理\\2016年11月\\b.txt",new char[]{' ','\n'});
        FileWriter c=new FileWriter("D:\\文件\\QC處理\\2016年11月\\c.txt");
        String aWord = null;  
        String bWord = null;  
        while ((aWord = a.nextWord()) != null) {  
            c.write(aWord);  
            bWord = b.nextWord();  
            if (bWord != null) {  
                c.write(bWord);  
            }  
        }  
        if (bWord != null) {  
            c.write(bWord);  
        }  
        c.close();  
        System.out.println("finish");  
        }
    }app

class FileManager{  
 
    String[] words = null;  
    int pos = 0;  
    @SuppressWarnings("resource")
    public FileManager(String fileName, char spilt[]) throws Exception {
        File file = new File(fileName);  
        FileReader fr = new FileReader(file);  
        char buf[] = new char[(int) file.length()];  
        int len = fr.read(buf);  
        String bufString = new String(buf, 0, len);  
        StringBuffer temp = new StringBuffer("");  
        temp.append(spilt[0]);  
        if (spilt.length > 1) {  
            int posl = 2;  
            while (posl <= spilt.length) {  
                temp.append("|");  
                temp.append(spilt[posl - 1]);  
                posl++;  
            }  
        }  
        String bs = temp.toString();  
        words = bufString.split(bs);  
    }  
 
    public String nextWord() {  
        if (pos == words.length) {  
            return null;  
        } else {  
            return words[pos++];  
        }  
}
}it

 

 

 

第二種:io

 

package cglib;class

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class StringNumber {
    
    public static void main(String args[]) throws Exception{
        try{
        FileManager a=new FileManager("D:\\文件\\QC處理\\2016年11月\\a.txt",new char[]{'\n'});
        FileManager b=new FileManager("D:\\文件\\QC處理\\2016年11月\\b.txt",new char[]{' ','\n'});
        FileWriter c=new FileWriter("D:\\文件\\QC處理\\2016年11月\\c.txt");
        String aWord= null;  
        String bWord= null;  
         //讀取一個aWord,調用c寫入,讀取一個bWord,調用 c寫入  
        while((aWord= a.nextWord()) !=null ){
            System.out.println("aWord="+aWord);
               c.write(aWord+ "\n");  
               bWord= b.nextWord();
               System.out.println("bWord="+bWord);
               if(bWord!= null){  
                   c.write(bWord+ "\n");
                   
               }
        }  
      //還得考慮a.txt內容讀取完,b.txt還有內容沒弄完
        while((bWord= b.nextWord()) != null){  
               c.write(bWord+ "\n");  
        }     
         
        c.close();
        System.out.println("finish");
        }catch(Exception e){
            e.printStackTrace();
        }
    
        }
    }
    
class FileManager{
     String[] words =null;  
     int pos = 0;  
     //把文件轉換成String類型,而後分割成String[]  
     @SuppressWarnings("resource")
    public FileManager(String filename,char[] seperators) throws Exception{  
              
            File f = new File(filename);  
            FileReader reader = new FileReader(f);  
            //聲明一個char數組緩衝區  
            char[] buf =new char[(int)f.length()]; //char佔用兩個字節  
            //調用reader讀取,放入char數組中  
            int len =reader.read(buf);  
            String results = new String(buf,0,len);  
            //聲明一個regex表達式null,而後進行賦值  
            String regex= null;  
            if(seperators.length>1 ){  
                   regex= "" + seperators[0] + "|" + seperators[1];  
            }else{  
                   regex= "" + seperators[0];  
            }  
            words =results.split(regex);  
     }  
      
     public String nextWord(){  
            if(pos ==words.length)  
                   return null;  
            return words[pos++];  
     }  
 
}
    
   import

相關文章
相關標籤/搜索