Write a program to merge the words in a.txt file alternately with the words in b.txt file into c.txt file, the words in a.txt file are separated by carriage return, and the words in b.txt file are separated by carriage return or spaces to separate.

import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.InputStreamReader;

import java.io.PrintWriter;

public class A {  

            public static void main(String[] args) throws Exception {  

                  FileManage a = new FileManage("src/main/resources/a.txt");   

                  FileManage b = new FileManage("src/main/resources/b.txt");  

                 PrintWriter pw=new PrintWriter("src/main/resources/c.txt");  

                 String aWord = null;         

                 String bWord = null;        

               while((aWord = a.nextWord()) !=null ){     

                        pw.write(aWord + "\n");            

                       bWord = b.nextWord();         

                         if(bWord != null)     

                         pw.write(bWord + "\n");        

              }       

             while((bWord = b.nextWord()) != null){      

                     pw.write(bWord + "\n");     

             }             pw.close();   

 }

}

class FileManage {  

            String[] arrwords = null;

            int pos = 0;

            public FileManage(String filepath) throws Exception {   

            FileInputStream fis = new FileInputStream(filepath);  

            InputStreamReader in = new InputStreamReader(fis);   

           BufferedReader read = new BufferedReader(in);  

            String lines = null;

             StringBuffer sb = new StringBuffer();

           while ((lines = read.readLine()) != null) {   

                 sb.append(lines).append(" ");

           }  

          arrwords = sb.toString().split(" ");

             if(fis!=null){  

             fis.close();  

             }  

            if(in!=null){   

            in.close();  

           }      

           if(read!=null){

             read.close();

       }  

   }

 public String nextWord() {  

 if (pos == arrwords.length)

   return null;  

 return arrwords[pos++];

 } }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324851335&siteId=291194637