Java Algorithm interview questions: Separate them with carriage return or space in the answer file .,
Package com. swift; import java. io. bufferedReader; import java. io. bufferedWriter; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. inputStreamReader; import java. io. outputStreamWriter; import java. util. arrayList; import java. util. list; public class IO_jiaoti {public static void main (String [] args) throws Exception {/** compile a program to merge the single words in the.txt file with those in the B .txt file into the c.txt file, and the words in the.txt File Separate them with carriage return characters, and separate them with carriage return or space in B. * txt files. */BufferedReader br1 = new BufferedReader (new InputStreamReader (new FileInputStream ("e: \ neck \ a.txt"), "gb2312 ")); bufferedReader br2 = new BufferedReader (new InputStreamReader (new FileInputStream ("e: \ neck \ B .txt"), "gb2312 ")); bufferedWriter bw = new BufferedWriter (new OutputStreamWriter (new FileOutputStream ("e: \ neck \ c.txt"), "gb2312 ")); list <String> list1 = new ArrayList <String> (); List <String> list2 = new ArrayList <String> (); List <String> list3 = new ArrayList <String> (); String str1; while (str1 = br1.readLine ())! = Null) {list1.add (str1);} StringBuffer sb = new StringBuffer (); int len; char [] buf = new char [1024]; while (len = br2.read (buf ))! =-1) {sb. append (buf, 0, len);} String [] bstr = sb. toString (). split ("\ s"); for (String str: bstr) {System. out. println (str); if (str. matches ("[a-zA-Z] {1,}") {list2.add (str) ;}} if (list1.size () <= list2.size ()) {for (int I = 0; I <list1.size (); I ++) {list3.add (list1.get (I); list3.add (list2.get (I ));} for (int I = list1.size (); I <list2.size (); I ++) {list3.add (list2.get (I) ;}} else {for (int I = 0; I <list2.size (); I ++) {list3.add (list1.get (I); list3.add (list2.get (I);} for (int I = list2.size (); I <list1.size (); I ++) {list3.add (list1.get (I) ;}}for (String str: list3) {System. out. print (str + ""); bw. write (str); bw. write ("\ r \ n"); bw. flush ();}}}