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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190 |
package
com.stella; import java.io.Closeable; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import
java.io.SequenceInputStream; import
java.util.ArrayList; import
java.util.Collections; import
java.util.Properties; /** * 切割与合并文件 * @author Administrator * */ public
class SplitAndCombineFile { public
static void main(String[] args) throws
IOException { File file = new
File( "E:\\多生产者多消费者.wmv" ); //File parentFile = getDiretory(file); //initPropertiesFile(parentFile); //splitFile(file, "rar"); combineFiles(file); } /** * 获取配置文件 * @return */ public
static File getPropertiesFile(File file){ //遍历文件目录找配置文件 File parentFile = getDiretory(file); File[] files = parentFile.listFiles(); for (File f : files){ if (f.isFile() && fileSuffixFilter(f, "properties" )){ return
f; } } return
null ; } /** * 程序启动时加载配置文件 * @param fileDir配置文件 * @throws IOException */ public
static void initPropertiesFile(File file) throws
IOException{ //思路 //1.查看此配置文件是否存在,如果存在删除,如果不存在创建,然后加载配置信息 if (file != null
&& !file.exists()){ file.createNewFile(); } else { //如果文件存在,则删除该文件,重新创建一个 file.delete(); initPropertiesFile(file); } } /** * 获取存切割了的文件和配置文件的目录 * @param file原文件的目录 * @return */ public
static File getDiretory(File file){ //思路 //1根据原文文件的目录找出它的父目录即是我们所需的文件目录 return
file.getAbsoluteFile().getParentFile(); } /** * 文件后缀名过滤器 * @param file * @return */ public
static boolean fileSuffixFilter(File file, final
String suffix){ return
new FileFilter() { @Override public
boolean accept(File f) { return
f.getName().endsWith(suffix); } }.accept(file); } /** * 合并文件 * @param filesDir 传入原文件 */ public
static void combineFiles(File file){ //思路 //1.根据指定目录找到配置文件,查找要合并的碎片文件的后缀名,以及合并以后的后缀名 ArrayList<FileInputStream> fileList = new
ArrayList<FileInputStream>(); File propertiesFile = getPropertiesFile(file); if (propertiesFile == null ){ throw
new RuntimeException( "配置文件找不到!" ); } Properties properties = new
Properties(); SequenceInputStream sInputStream = null ; FileOutputStream fileOutputStream = null ; try
{ properties.load( new
FileInputStream(propertiesFile)); String splitSuffix = properties.getProperty( "splitFileSuffix" ); File[] files = file.getParentFile().listFiles(); for (File f : files){ if (f.isFile() && fileSuffixFilter(f, splitSuffix)){ fileList.add( new
FileInputStream(f)); } } sInputStream = new
SequenceInputStream(Collections.enumeration(fileList)); fileOutputStream = new
FileOutputStream( new
File(file.getParentFile().getName() + "\\copy."
+ properties.getProperty( "fileSuffix" ))); byte [] bytes = new
byte [ 1024 * 1024 * 10 ]; int
num = 0 ; while ((num = sInputStream.read(bytes)) != - 1 ){ fileOutputStream.write(bytes, 0 , num); } } catch
(FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch
(IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { closeStream(sInputStream,fileOutputStream); } //2.创建ArrayList集合,将碎片文件装入集合 //3.创建SequenceInputStream,用Collections.enumeration(集合) } /** * 切割文件 * @param filePath 要进行切割的文件 * @param splitFileSuffix 切割后文件的后缀名 */ public
static void splitFile(File filePath, String splitFileSuffix){ //思路 //1.先对filePath进行处理,检查它是不是一个文件,如果是文件,则进行切割,如果不是抛出异常 FileOutputStream fileOutput = null ; FileInputStream fileInput = null ; if (!filePath.isFile()){ throw
new RuntimeException( "这不是一个文件!" ); } try
{ fileInput = new
FileInputStream(filePath); byte [] bytes = new
byte [ 1024 * 1024 ]; int
byteNum = 0 ; int
splitFileNum = 1 ; File parentFile = getDiretory(filePath); String parentFilePath = parentFile.getAbsolutePath(); while ((byteNum = fileInput.read(bytes)) != - 1 ){ //这里把切割以后的文件存到和原文件相同的文件夹 fileOutput = new
FileOutputStream(parentFilePath + "\\"
+ splitFileNum ++ + "."
+ splitFileSuffix); fileOutput.write(bytes, 0 , byteNum); } //3.更新文件的配置信息 Properties properties = new
Properties(); properties.setProperty( "splitFileSuffix" , splitFileSuffix); String fileName = filePath.getName(); properties.setProperty( "fileSuffix" , fileName.substring(fileName.lastIndexOf( "." ) + 1 )); //这里减1是因为上面是++最后多加了一次 properties.setProperty( "splitFileNum" ,String.valueOf(splitFileNum - 1 )); //将properties保存到配置文件里 properties.store( new
FileOutputStream( new
File(parentFilePath + "\\"
+ "splitfile.properties" )), "addpropertiesconfig" ); } catch
(FileNotFoundException e) { e.printStackTrace(); throw
new RuntimeException( "文件找不到!" ); } catch
(IOException e) { e.printStackTrace(); throw
new RuntimeException( "流异常!" ); } finally { //关闭所有打开的流 closeStream(fileOutput,fileInput); } } /** * 关闭打开的流 * @param closeables 不定参列表 */ public
static void closeStream(Closeable...closeables){ for (Closeable closeable : closeables){ try
{ if (closeable != null ){ closeable.close(); } } catch
(IOException e) { e.printStackTrace(); throw
new RuntimeException( "流关闭异常!" ); } } } } |
原文:http://www.cnblogs.com/lxricecream/p/3602363.html