java는 두 파일의 내용을 새 파일에 통합합니다
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);
}
}
이상의 코드가 본문의 전체 내용입니다. 여러분이 좋아해 주시기 바랍니다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
38. Java의 Leetcode 솔루션텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.