例如:
用如下代码
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();
}
谁告诉你可以这么搞的?
谁说过2个文件前后合起来就是1个连续的文件了....
应该怎样呢 求赐教
@Paul_ms: 不知道你要做什么.没法赐教..
@吴瑞祥: 想把两个word文件合并成一个
(⊙o⊙)…,我这样做过. 文件太大了, 就先切割成小的文件, 然后, 上传成功后, 后台合并.O(∩_∩)O哈哈~
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("文件合并完毕!");
}
}
@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(); } } }
手打。
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(); } }