Java Algorithm interview questions: Separate them with carriage return or space in the answer file .,

Source: Internet
Author: User

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 ();}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.