Java文件的拆分与合并.docx
《Java文件的拆分与合并.docx》由会员分享,可在线阅读,更多相关《Java文件的拆分与合并.docx(37页珍藏版)》请在冰豆网上搜索。
Java文件的拆分与合并
一:
按文件行数拆分
Java代码
1.package com.yesky.apachelog.util.file;
2.
3.import java.io.BufferedReader;
4.import java.io.BufferedWriter;
5.import java.io.File;
6.import java.io.FileNotFoundException;
7.import java.io.FileReader;
8.import java.io.FileWriter;
9.import java.io.IOException;
10.import java.util.ArrayList;
11.import java.util.Iterator;
12.import java.util.List;
13.import java.util.StringTokenizer;
14.
15.import com.yesky.apachelog.util.regex.ApacheRegexString;
16.
17./**
18. * 按行读取文件,并按行数对文件进行拆分
19. */
20.public class SeparatorByLine {
21. List FilePathArr = new ArrayList();
22. String FileName = null;// 原文件名
23. long FileSize = 0;// 原文件的大小
24.
25. public SeparatorByLine() {
26. }
27.
28. /**
29. *
30. * @param fileAndPath
31. * 原文件名及路径
32. */
33. private void getFileAttribute(String fileAndPath)// 取得原文件的属性
34. {
35. File file = new File(fileAndPath);
36. FileName = file.getName();
37. FileSize = file.length();
38. }
39.
40. /**
41. *
42. * @param fileAndPath
43. * 原文件及完整路径
44. * @param currentBlock
45. * 当前块的序号
46. * @return 现在拆分后块的文件名
47. */
48. private String generateSeparatorFileName(String fileAndPath,
49. int currentBlock)// 生成折分后的文件名,以便于将来合并
50. {
51. return fileAndPath + ".part" + currentBlock;
52. }
53.
54. /**
55. * 按行写文件
56. *
57. * @param fileSeparateName:
拆分的文件名及路径
58. * @param tempLine:
一行的内容
59. * @return
60. */
61. private boolean writeFileByLine(String fileSeparateName,
62. List tempList)// 往硬盘写文件
63. {
64. BufferedWriter writer = null;
65. try {
66. writer = new BufferedWriter(new FileWriter(fileSeparateName, true), 10 * 1024 * 1024);
67. Iterator it = tempList.iterator();
68. while (it.hasNext()) {
69. String s = (String) it.next();
70. writer.append(s);
71. writer.newLine();
72. }
73. writer.flush();
74. writer.close();
75. } catch (IOException e) {
76. e.printStackTrace();
77. return false;
78.
79. } finally {
80. if (writer !
= null)
81. try {
82. writer.close();
83. } catch (IOException e) {
84. // TODO Auto-generated catch block
85. e.printStackTrace();
86. }
87. }
88.
89. return true;
90.
91. }
92.
93. /**
94. * 拆分文件,
95. *
96. * @param fileAndPath
97. * @param currentPath
98. * @param linNum:
行数
99. * @return:
路径,
100. */
101. public List separatorFileByLine(String fileAndPath,
102. String currentPath, long linNum)// 折分文件主函数
103. {
104.
105. List list = new ArrayList();
106. getFileAttribute(fileAndPath);// 将文件的名及大小属性取出来
107. if (null !
= currentPath && !
("").equals(currentPath)) {
108. this.createFolder(currentPath);
109. }
110.
111. BufferedReader reader = null;
112. try {
113. reader = new BufferedReader(new FileReader(fileAndPath));
114. } catch (FileNotFoundException e) {
115. // TODO Auto-generated catch block
116. e.printStackTrace();
117. }
118. String temp = "";
119. int partName = 1;
120. String FileCurrentNameAndPath = null;
121. try {
122. while ((temp = reader.readLine()) !
= null) {
123.
124. if (currentPath == null || ("").equals(currentPath))
125. FileCurrentNameAndPath = generateSeparatorFileName(
126. fileAndPath, partName);
127. else
128. FileCurrentNameAndPath = generateSeparatorFileName(
129. currentPath + FileName, partName);
130. temp = temp.replaceAll("\"", "'");
131. String regexStr=ApacheRegexString.regexString(temp);
132. if (regexStr !
= null) {
133. list.add(regexStr);
134. }
135.
136. if (list.size() > 0) {
137. if (list.size() % linNum == 0) {
138. writeFileByLine(FileCurrentNameAndPath, list);
139. FilePathArr.add(FileCurrentNameAndPath);
140. partName++;
141. list.clear();
142. }
143. }
144. }
145. if (list !
= null && list.size() > 0) {
146. writeFileByLine(FileCurrentNameAndPath, list);
147. FilePathArr.add(FileCurrentNameAndPath);
148. list.clear();
149. }
150. reader.close();
151. } catch (IOException e) {
152. e.printStackTrace();
153. } finally {
154. if (reader !
= null)
155. try {
156. reader.close();
157. } catch (IOException e) {
158. // TODO Auto-generated catch block
159. e.printStackTrace();
160. }
161. }
162.
163. return FilePathArr;
164. }
165.
166. /**
167. * 新建目录
168. *
169. * @param folderPath
170. * 目录
171. * @return 返回目录创建后的路径
172. */
173. public void createFolder(String folderPath) {
174. String path = folderPath;
175. StringTokenizer st = new StringTokenizer(path, "/");
176. String path1 = st.nextToken() + "/";
177. String path2 = path1;
178. while (st.hasMoreTokens()) {
179. path1 = st.nextToken() + "/";
180. path2 += path1;
181. File inbox = new File(path2);
182. if (!
inbox.exists())
183. inbox.mkdir();
184. }
185. }
186.
187. public List getFilePathArr() {
188. return FilePathArr;
189. }
190.
191. public static void main(String[] args) {
192. SeparatorByLine separator = new SeparatorByLine();
193. String fileAndPath = "e:
\\test\\object_access_20091119.log";// 文件名及路径
194. long blockSize = 300000000;// 每一个文件块的大小,大小是按字节计算
195. Iterator it = separator.separatorFileByLine(fileAndPath, "",
196. blockSize).iterator();
197. while (it.hasNext()) {
198. System.out.println("=====" + it.next());
199. }
200.
201. }
202.
203.}
packagecom.yesky.apachelog.util.file;
importjava.io.BufferedReader;
importjava.io.BufferedWriter;
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.Iterator;
importjava.util.List;
importjava.util.StringTokenizer;
importcom.yesky.apachelog.util.regex.ApacheRegexString;
/**
*按行读取文件,并按行数对文件进行拆分
*/
publicclassSeparatorByLine{
ListFilePathArr=newArrayList();
StringFileName=null;//原文件名
longFileSize=0;//原文件的大小
publicSeparatorByLine(){
}
/**
*
*@paramfileAndPath
*原文件名及路径
*/
privatevoidgetFileAttribute(StringfileAndPath)//取得原文件的属性
{
Filefile=newFile(fileAndPath);
FileName=file.getName();
FileSize=file.length();
}
/**
*
*@paramfileAndPath
*原文件及完整路径
*@paramcurrentBlock
*当前块的序号
*@return现在拆分后块的文件名
*/
privateStringgenerateSeparatorFileName(StringfileAndPath,
intcurrentBlock)//生成折分后的文件名,以便于将来合并
{
returnfileAndPath+".part"+currentBlock;
}
/**
*按行写文件
*
*@paramfileSeparateName:
拆分的文件名及路径
*@paramtempLine:
一行的内容
*@return
*/
privatebooleanwriteFileByLine(StringfileSeparateName,
ListtempList)//往硬盘写文件
{
BufferedWriterwriter=null;
try{
writer=newBufferedWriter(newFileWriter(fileSeparateName,true),
10*1024*1024);
Iteratorit=tempList.iterator();
while(it.hasNext()){
Strings=(String)it.next();
writer.append(s);
writer.newLine();
}
writer.flush();
writer.close();
}catch(IOExceptione){
e.printStackTrace();
returnfalse;
}finally{
if(writer!
=null)
try{
writer.close();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
}
returntrue;
}
/**
*拆分文件,
*
*@paramfileAndPath
*@paramcurrentPath
*@paramlinNum:
行数
*@return:
路径,
*/
publicListseparatorFileByLine(StringfileAndPath,
StringcurrentPath,longlinNum)//折分文件主函数
{
Listlist=newArrayList();
getFileAttribute(fileAndPath);//将文件的名及大小属性取出来
if(null!
=currentPath&&!
("").equals(currentPath)){
this.createFolder(currentPath);
}
BufferedReaderreader=null;
try{
reader=n