本文实例为大家分享了Java实现多个文档合并输出到一个文档的具体代码,供大家参考,具体内容如下
方法:Java NIO
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
package First; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.channels.FileChannel; import java.nio.channels.WritableByteChannel; public class Test { public static void main(String params[]) throws Exception { String[] iF = new String[] { "E:/test1.txt" , "E:/test2.txt" , "E:/test3.txt" , "E:/test4.txt" }; String oF = "E:/out.txt" ; FileOutputStream output = new FileOutputStream( new File(oF)); WritableByteChannel targetChannel = output.getChannel(); for ( int i = 0 ; i<iF.length; i++){ FileInputStream input = new FileInputStream(iF[i]); FileChannel inputChannel = input.getChannel(); inputChannel.transferTo( 0 , inputChannel.size(), targetChannel); inputChannel.close(); input.close(); } targetChannel.close(); output.close(); System.out.println( "All jobs done..." ); } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/Donnnnnn/p/7727782.html