首页 新闻 会员 周边

用流合并两个文件(比如两个word),只能将第一个word的内容保存到目标文件,该怎么做

0
悬赏园豆:50 [待解决问题]

例如:

用如下代码

public void Demo(string file1, string file2)
{
bs1 = File.ReadAllBytes(file1);
bs2 = File.ReadAllBytes(file2);
}
public void Together(string file)
{
FileStream fs = File.Create(file);
fs.Write(bs1, 0, bs1.Length);
fs.Write(bs2, 0, bs2.Length);
fs.Close();
}

Paul_ms的主页 Paul_ms | 初学一级 | 园豆:108
提问于:2017-05-26 13:51
< >
分享
所有回答(4)
0

谁告诉你可以这么搞的?

谁说过2个文件前后合起来就是1个连续的文件了....

吴瑞祥 | 园豆:29449 (高人七级) | 2017-05-26 13:52

应该怎样呢  求赐教

支持(0) 反对(0) Paul_ms | 园豆:108 (初学一级) | 2017-05-26 13:55

@Paul_ms: 不知道你要做什么.没法赐教..

支持(0) 反对(0) 吴瑞祥 | 园豆:29449 (高人七级) | 2017-05-26 13:55

@吴瑞祥: 想把两个word文件合并成一个

支持(0) 反对(0) Paul_ms | 园豆:108 (初学一级) | 2017-05-26 13:57

(⊙o⊙)…,我这样做过. 文件太大了, 就先切割成小的文件, 然后, 上传成功后, 后台合并.O(∩_∩)O哈哈~

 

支持(0) 反对(0) [0] | 园豆:1257 (小虾三级) | 2017-05-27 16:26
0

public static void main(String args[])throws IOException{
FileReader Fr1=new FileReader(("c:\\文件1.txt"));
FileReader Fr2=new FileReader(("c:\\文件2.txt")); 
BufferedReader Br1=new BufferedReader(Fr1);
BufferedReader Br2=new BufferedReader(Fr2);
BufferedWriter Bw=new BufferedWriter (new FileWriter("c:\\合并后的文件.txt"));
String S1=Br1.readLine();
String S2=Br2.readLine(); 
System.out.println(S1+S2);
while(S1!=null){
Bw.write(S1); 
S1=Br1.readLine();

}
while(S2!=null){
Bw.write(S2); 
S2=Br2.readLine();

}
Fr1.close();
Fr2.close();
Br1.close();
Br2.close();
Bw.close();
System.out.println("文件合并完毕!");

}
}

 

风行天下12 | 园豆:3867 (老鸟四级) | 2017-05-26 15:52
0

@Test //从2个文件中逐行读入数据,并存到一个.html文件 public void t2(){ BufferedReader br =null; BufferedReader br2 = null; BufferedWriter bw = null; try { br= new BufferedReader(new FileReader("aaa.txt")); br2 = new BufferedReader(new FileReader("dbcp.txt")); bw = new BufferedWriter(new FileWriter("kj.html")); String str ; while((str=br.readLine())!=null){ bw.write(str); bw.newLine(); bw.flush(); } while((str=br2.readLine())!=null){ bw.write(str); bw.newLine(); bw.flush(); } } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { br.close(); } catch (IOException e) { e.printStackTrace(); } try { br2.close(); } catch (IOException e) { e.printStackTrace(); } try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } }

匆匆先生 | 园豆:202 (菜鸟二级) | 2017-05-28 08:59
0

手打。

 

import java.io.File;
import java.io.FileInputStream;
import java.io.*;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;

/**
 * @Time 2017年5月30日19:18:36
 * @author caiyongji
 * @Description doc文件合并
 */
public class DocFileConsolidation {
    private String[] filePaths;
    private String fileOutPath;
    public DocFileConsolidation(String fileOutPath, String... filePaths) {
        this.fileOutPath = fileOutPath;
        this.filePaths = filePaths;
    }

    /**
     * 合并
     */
    public void consolidation() {
        File file = null;
        WordExtractor extractor = null;
        try {
            String contents = new String();
            for (String path : filePaths) {
                file = new File(path);
                FileInputStream fis = new FileInputStream(file.getAbsolutePath());
                HWPFDocument document = new HWPFDocument(fis);
                extractor = new WordExtractor(document);
                String[] fileData = extractor.getParagraphText();
                for (int i = 0; i < fileData.length; i++) {
                    if (fileData[i] != null)
                        contents += fileData[i];
                }
                fis.close();
            }
            FileOutputStream fos = new FileOutputStream(new File(fileOutPath));
            fos.write(contents.getBytes());
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        DocFileConsolidation consolidation = new DocFileConsolidation("c:\\out.doc","c:\\1.doc","c:\\2.doc");
        consolidation.consolidation();
    }
}
CaiYongji | 园豆:1267 (小虾三级) | 2017-05-30 19:31
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册