How to merge the words in a.txt and b.txt into c.txt alternately, with the words in a.txt separated by Carriage return; The words in b.txt are separated by carriage returns or spaces.

  java, question

Problem: unable to merge words alternately according to the title requirements. After searching [the problem is found in regular expressions], if you change both regular expressions to: regex = “\\n*\\s* “; The problem was solved.

But now, I want to use [character array] to pass parameters to pass regular expressions.
Can you tell me how? ?

Paste the code as follows:

package cn.itcast;
 
 import java.io.File;
 import java.io.FileReader;
 import java.io.FileWriter;
 
 public class MainClass
 bracket
 public static void main(String[] args) throws Exception
 bracket
 FileManager a = new FileManager("a.txt",new char[]{'\n'});
 FileManager b = new FileManager("b.txt",new char[]{'\n','  '});
 
 FileWriter c = new FileWriter("c.txt");
 
 String aWord = null;
 String bWord = null;
 
 while((aWord = a.nextWord()) !  =null )
 bracket
 c.write(aWord);
 bWord = b.nextWord();
 if(bWord !  = null)
 c.write(bWord);
 bracket
 
 while((bWord = b.nextWord()) !  = null){
 c.write(bWord);
 bracket
 c.close();
 bracket
 
 bracket
 
 
 class FileManager
 bracket
 
 String[] words = null;
 int pos = 0;
 public FileManager(String filename,char[] seperators) throws Exception
 bracket
 File f = new File(filename);
 FileReader reader = new FileReader(f);
 char[] buf = new char[(int)f.length()];
 int len = reader.read(buf);
 String results = new String(buf,0,len);
 String regex = null;
 if(seperators.length >1 ){
 Regex = ""plus separator s [0] plus "|" plus separator s [1];
 }else{
 Regex = ""plus separator s [0];
 bracket
 words = results.split(regex);
 bracket
 
 public String nextWord(){
 if(pos == words.length)
 return null;
 Return words[pos plus];
 bracket
 
 bracket

A.txt b.txt merge

$ cat b.txt | tr ‘ ‘ ‘\n’ > tmp
$ paste a.txt tmp | tr ‘\r’ ‘\n’ > c.txt