Java作业,求助各位大佬,急急!! 5

将两个文本文件的内容复制到另一个文件中,格式采用:source1+source2(其中source1和source2为源文件名,target为目标文件名)... 将两个文本文件的内容复制到另一个文件中,格式采用:
source1+source2 (其中source1和source2为源文件名,target为目标文件名)
展开
 我来答
荤奇0Hg
2019-12-31 · TA获得超过308个赞
知道小有建树答主
回答量:246
采纳率:66%
帮助的人:63.9万
展开全部

简单写了一个

package cn.os;

import java.io.*;

public class MergedDocuments {
    public static void main(String[] args) throws IOException {
        String ff = "C:\\Users\\Administrator\\Desktop\\DianCai\\src\\cn\\os\\";
        File file = new File(ff+"target");
        File file1 = new File(ff+"source1");
        File file2 = new File(ff+"source2");
        String str=MergedDocuments.getText(file1,file2);
        System.out.println(str);
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        bw.write(str);
        bw.close();


    }
    public static String getText(File... file) throws IOException {
        StringBuilder result = new StringBuilder();
        String str;
        for(File f : file){
            BufferedReader br = new BufferedReader(new FileReader(f));

            while((str = br.readLine()) !=null){
                result.append(System.lineSeparator()+str);
            }
            br.close();
        }


        return result.toString();
    }
}


希望能帮到你

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式