用java語言實現兩個文件的拼接與上一篇用java實現兩個文件的異或使用的方法都一樣,都是fileinputstream()與fileoutputstream()方法,兩個相同大小的文件a,b,把文件b拼接在文件a之后,輸出的文件名為outfile具體代碼詳見下述內容:
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
import java.io.file; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; public class append { static int count= 0 ; static int countb= 0 ; public static void main(string args[]) throws ioexception { fileinputstream filea = new fileinputstream( "d:\\javaxor\\a" ); fileinputstream fileb = new fileinputstream( "d:\\javaxor\\b" ); file outfile= new file( "d:\\javaxor\\outfile" ); int filesizea=filea.available(); //計算文件的大小 int filesizeb=fileb.available(); fileoutputstream fos= new fileoutputstream(outfile); int hasreada = 0 ; int hasreadb= 0 ; byte [] bufa= new byte [ 1024 ]; byte [] bufc= new byte [ 1024 ]; byte [] buf_yua= new byte [filesizea% 1024 ]; byte [] buf_yub= new byte [filesizeb% 1024 ]; while ( (hasreada=filea.read(bufa) )> 0 ) { if (count<filesizea-filesizea% 1024 ) { for ( int i= 0 ;i<bufa.length && count<filesizea-filesizea% 1024 ;i++) { bufc[i]=( byte )(bufa[i] & 0xff ); count++; } fos.write(bufc); } else if (count>=filesizea-filesizea% 1024 && count<filesizea) { for ( int j= 0 ; count>=filesizea-filesizea% 1024 && count<filesizea ;j++) { buf_yua[j]=( byte )(bufa[j] & 0xff ); count++; } fos.write(buf_yua); } } while ( (hasreadb=fileb.read(bufa) )> 0 ) { if (countb<filesizeb-filesizeb% 1024 ) { for ( int i= 0 ;i<bufa.length && countb<filesizeb-filesizeb% 1024 ;i++) { bufc[i]=( byte )(bufa[i] & 0xff ); countb++; } fos.write(bufc); } else if (countb>=filesizeb-filesizeb% 1024 && countb<filesizeb) { for ( int j= 0 ; countb>=filesizeb-filesizeb% 1024 && countb<filesizeb ;j++) { buf_yub[j]=( byte )(bufa[j] & 0xff ); countb++; } fos.write(buf_yub); } } } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/u013555975/article/details/50294047