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