Alternately merge the words in the A.txt file with the words in the B.txt file into the C.txt file

Source: Internet
Author: User

Original address: Merge the words in the A.txt file with the words in the B.txt file into the C.txt file

The requirements are as follows: Write a program that alternately merges the words in the A.txt file with the words in the B.txt file into the C.txt file a.txt the word in the file is delimited by a carriage return, separated by a carriage return or a space in the B.txt file.

Nonsense not much to say, directly on the code:

package javase.arithmetic; import com.google.common.base.charsets;import  com.google.common.base.joiner;import com.google.common.base.splitter;import  Com.google.common.collect.lists;import com.google.common.io.files; import java.io.file;import  java.io.IOException;import java.util.List; /** * User: Realfighter *  date: 2015/3/10 * time: 18:06 */public class filetest {      /**     *  Write a program   merge the words in the A.txt file with the words in the B.txt file into the C.txt file The words in the  a.txt file are separated by a carriage return, separated by a carriage return or a space in the      * b.txt file.      */    //a.txt                                       //b.txt    /**     i                                            this is a java  program     love                                         my name is Realfighter      u     baby     */     public static void main (String[] args)  throws IOException {         //Read A.txt b.txt content   switch to list         string aPath = filetest.class.getclassloader (). GetResource ("A.txt"). GetPath ();         list alist = files.readlines (New file (Apath),  Charsets.UTF_8);         string bpath = filetest.class.getclassloader (). GetResource ("B.txt"). GetPath ();        list blist =  Files.readlines (New file (Bpath),  charsets.utf_8);         All the words in  list awords = alist;// a.txt          List bwords = lists.newarraylist (Splitter.on (" "). Split (Joiner.on (" "). Join (blist))) ;// b.txt all the words inside the         list bigone = awords.size ( )  >= bwords.size ()  ? aWords : bWords;         list smallOne = awords.size ()  >= bwords.size ()  ? bWords : aWords;          stringbuffer from = new stringbuffer ();         for  (int i = 0; i <  Smallone.size ();  i++)  {             From.append (Bigone.get (i)). Append (" "). Append (Smallone.get (i)). Append (" ");         }        for  (int j =  Smallone.size ();  j < bigone.size ();  j++)  {             from.append (Bigone.get (j)). Append (" ");         }        //  Writing Files          string cpath&nbSp;= filetest.class.getclassloader (). GetResource ("C.txt"). GetPath ();         file file = new file (CPath);         Files.write (from, file, charsets.utf_8);     } }

Source Address: Http://git.oschina.net/realfighter/xx566-diary/blob/master/src/javase/arithmetic/FileTest.java

Alternately merge the words in the A.txt file with the words in the B.txt file into the C.txt file

Related Article

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.