java는 두 파일의 내용을 새 파일에 통합합니다

2659 단어 java병합파일
프로그램을 작성하여 a.txt 파일의 단어와 b.txt 파일의 단어를 c.txt 파일의 a.txt 파일에 교체하여 합친 단어는 리턴 문자로 구분하고, b.txt 파일에는 리턴 또는 공백으로 구분합니다.

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 {
    /**
     * a.txt b.txt c.txt a.txt ,
     * b.txt 。
     */
    //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 {
        // a.txt b.txt 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;// a.txt
        List bWords = Lists.newArrayList(Splitter.on(" ").split(Joiner.on(" ").join(bList)));// 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(" ");
        }
        //
        String cpath = FileTest.class.getClassLoader().getResource("c.txt").getPath();
        File file = new File(cpath);
        Files.write(from, file, Charsets.UTF_8);
    }
}
이상의 코드가 본문의 전체 내용입니다. 여러분이 좋아해 주시기 바랍니다.

좋은 웹페이지 즐겨찾기