java中如何将两个文件合并到另一个文件

一个文件里写1,2,3,4,5 ,另一个文件里写6,7,8,9,10,怎么样在第三个文件里打印出1+6,2+7,3+8,4+9,5+10?在线等,急,,谢谢啦

java可以使用FileChannel快速高效地将多个文件合并到一起,以下是详细代码:

    import static java.lang.System.out;  
    import java.io.FileInputStream;  
    import java.io.FileOutputStream;  
    import java.io.IOException;  
    import java.nio.ByteBuffer;  
    import java.nio.channels.FileChannel;  
    import java.util.Arrays;  

    public class test {  
        public static final int BUFSIZE = 1024 * 8;  
        public static void mergeFiles(String outFile, String[] files) {  
            FileChannel outChannel = null;  
            out.println("Merge " + Arrays.toString(files) + " into " + outFile);  
            try {  
                outChannel = new FileOutputStream(outFile).getChannel();  
                for(String f : files){  
                    FileChannel fc = new FileInputStream(f).getChannel();   
                    ByteBuffer bb = ByteBuffer.allocate(BUFSIZE);  
                    while(fc.read(bb) != -1){  
                        bb.flip();  
                        outChannel.write(bb);  
                        bb.clear();  
                    }  
                    fc.close();  
                }  
                out.println("Merged!! ");  
            } catch (IOException ioe) {  
                ioe.printStackTrace();  
            } finally {  
                try {if (outChannel != null) {outChannel.close();}} catch (IOException ignore) {}  
            }  
        }  
        public static void main(String[] args) {  
            mergeFiles("D:/output.txt", new String[]{"D:/in_1.txt", "D:/in_2.txt", "D:/in_3.txt"});  
        }  
    }
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-08-31
你好,代码如下:
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class FilesInOneFile {

public static String read(File inFile) throws IOException {
FileReader fr = new FileReader(inFile) ;
char[] ch = new char[1024] ;
int len = fr.read(ch);
fr.close();
return new String(ch, 0, len) ;
}

public static void write(String str, File outFile) throws IOException {
FileWriter fw = new FileWriter(outFile) ;
fw.write(str);
fw.close();
}

public static void main(String[] args) throws IOException {
File inFile1 = new File("d:" + File.separator + "1.txt") ;
File inFile2 = new File("d:" + File.separator + "2.txt") ;
File outFile = new File("d:" + File.separator + "3.txt") ;
String str1 = read(inFile1) ;
String str2 = read(inFile2) ;
String[] strs1 = str1.split(",") ;
String[] strs2 = str2.split(",") ;
StringBuffer sb = new StringBuffer() ;
for (int i=0; i<strs1.length; i++) {
sb.append(strs1[i]);
sb.append("+") ;
sb.append(strs2[i]) ;
if (i != strs1.length-1) {
sb.append(",") ;
}
}
write(sb.toString(),outFile) ;
}
}追问

谢谢啦,我是java初学者,见谅哈,我写的地址找不到啊?
public static void main(String[] args) throws IOException {
File inFile1 = new File("f:" + File.separator + "wenben1.txt") ;
File inFile2 = new File("f:" + File.separator + "wenben2.txt") ;
File outFile = new File("f:" + File.separator + "wenben3.txt") ;

追答

你必须先创建好文件。

本回答被网友采纳
第2个回答  推荐于2017-11-25
你的描述感觉有点不清,貌似这是从两个文件中取出值再做进一步操作?追问

恩,不要意思啦,是先从2个文本里按序取出,再在第三个文本中打印出它们的相加之和

追答

哦,而且这两个文本里的数据应该是一一对应的吧,代码就不给你写了,应该很简单,思路:
FileReader 读出第一文件里的数据保存到一个数组里,读出第二个文件里的数据保存到另外一个数组里(我的理解是这两个数据里保存的数据应该是一一对应的,且这两个数据大小一样长),然后对其中一个数据进行循环,在循环里取出第一个数据的数据加上第二个数据的数据(位置一样),然后将结果可以先保存到另外一个数组,也可以直接就将结果写到第3个文本里哦。

追问

能对能存,就是怎么用BufferedinputStream与BufferedOuputStream怎么写,把它们之和打印出来???菜菜鸟啊 ,先谢谢你啦

追答

害我又自觉了一下流,代码如下
public static void main(String args[]) {
try {
FileReader fra = new FileReader("D:\\temp\\a.txt");
FileReader frb = new FileReader("D:\\temp\\b.txt");
char[] a = new char[1024];
int num = fra.read(a);
String avalue = new String(a,0,num);
char[] b = new char[1024];
int num2 = frb.read(b);
String bvalue = new String(b,0,num2);
String[] ashuzu = avalue.split(",");
String[] bshuzu = bvalue.split(",");
String s = "";
for(int i = 0;i<ashuzu.length;i++){
s += (Integer.parseInt(ashuzu[i])+Integer.parseInt(bshuzu[i]))+",";
}
FileWriter fw = new FileWriter("D:\\temp\\c.txt");
fw.write(s, 0, s.length()-1);
fw.close();
} catch (Exception e) {
e.printStackTrace();
}
}

其中,a.txt文件内容为1,2,3,4,5
b.txt文件内容为6,7,8,9,10
得到的c.txt文件内容为7,9,11,13,15

本回答被提问者采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网