Write a program to alternately merge words in a.txt file with words in b.txt file into c.txt file with words in a.txt file with carriage return characters, and carriage return or spaces in b.txt file Separate.
The code copy is as follows:
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 to alternately merge words in a.txt file with words in b.txt file into c.txt file with words in a.txt file with carriage return characters.
* b.txt file is separated by carriage return or space.
*/
//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 the content in a.txt b.txt and convert it 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);
List aWords = aList;// All words in a.txt
List bWords = Lists.newArrayList(Splitter.on(" ").split(Joiner.on(" ").join(bList));// All words in b.txt
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(" ");
}
// Write to the file
String cpath = FileTest.class.getClassLoader().getResource("c.txt").getPath();
File file = new File(cpath);
Files.write(from, file, Charsets.UTF_8);
}
}
The above code is the entire content of this article, I hope you like it.