1、Java文件的拆分与合并一:按文件行数拆分Java代码 1. packagecom.yesky.apachelog.util.file; 2. 3. importjava.io.BufferedReader; 4. importjava.io.BufferedWriter; 5. importjava.io.File; 6. importjava.io.FileNotFoundException; 7. importjava.io.FileReader; 8. importjava.io.FileWriter; 9. importjava.io.IOException; 10. importj
2、ava.util.ArrayList; 11. importjava.util.Iterator; 12. importjava.util.List; 13. importjava.util.StringTokenizer; 14. 15. importcom.yesky.apachelog.util.regex.ApacheRegexString; 16. 17. /* 18. *按行读取文件,并按行数对文件进行拆分 19. */20. publicclassSeparatorByLine 21. ListFilePathArr=newArrayList(); 22. StringFileN
3、ame=null;/原文件名 23. longFileSize=0;/原文件的大小 24. 25. publicSeparatorByLine() 26. 27. 28. /* 29. * 30. *paramfileAndPath 31. *原文件名及路径 32. */33. privatevoidgetFileAttribute(StringfileAndPath)/取得原文件的属性 34. 35. Filefile=newFile(fileAndPath); 36. FileName=file.getName(); 37. FileSize=file.length(); 38. 39.
4、40. /* 41. * 42. *paramfileAndPath 43. *原文件及完整路径 44. *paramcurrentBlock 45. *当前块的序号 46. *return现在拆分后块的文件名 47. */48. privateStringgenerateSeparatorFileName(StringfileAndPath, 49. intcurrentBlock)/生成折分后的文件名,以便于将来合并 50. 51. returnfileAndPath+.part+currentBlock; 52. 53. 54. /* 55. *按行写文件 56. * 57. *para
5、mfileSeparateName:拆分的文件名及路径 58. *paramtempLine:一行的内容 59. *return 60. */61. privatebooleanwriteFileByLine(StringfileSeparateName, 62. ListtempList)/往硬盘写文件 63. 64. BufferedWriterwriter=null; 65. try 66. writer=newBufferedWriter(newFileWriter(fileSeparateName,true), 10*1024*1024); 67. Iteratorit=tempLi
6、st.iterator(); 68. while(it.hasNext() 69. Strings=(String)it.next(); 70. writer.append(s); 71. writer.newLine(); 72. 73. writer.flush(); 74. writer.close(); 75. catch(IOExceptione) 76. e.printStackTrace(); 77. returnfalse; 78. 79. finally 80. if(writer!=null) 81. try 82. writer.close(); 83. catch(IO
7、Exceptione) 84. /TODOAuto-generatedcatchblock 85. e.printStackTrace(); 86. 87. 88. 89. returntrue; 90. 91. 92. 93. /* 94. *拆分文件, 95. * 96. *paramfileAndPath 97. *paramcurrentPath 98. *paramlinNum:行数 99. *return:路径, 100. */101. publicListseparatorFileByLine(StringfileAndPath, 102. StringcurrentPath,l
8、onglinNum)/折分文件主函数 103. 104. 105. Listlist=newArrayList(); 106. getFileAttribute(fileAndPath);/将文件的名及大小属性取出来 107. if(null!=currentPath&!().equals(currentPath) 108. this.createFolder(currentPath); 109. 110. 111. BufferedReaderreader=null; 112. try 113. reader=newBufferedReader(newFileReader(fileAndPa
9、th); 114. catch(FileNotFoundExceptione) 115. /TODOAuto-generatedcatchblock 116. e.printStackTrace(); 117. 118. Stringtemp=; 119. intpartName=1; 120. StringFileCurrentNameAndPath=null; 121. try 122. while(temp=reader.readLine()!=null) 123. 124. if(currentPath=null|().equals(currentPath) 125. FileCurr
10、entNameAndPath=generateSeparatorFileName( 126. fileAndPath,partName); 127. else128. FileCurrentNameAndPath=generateSeparatorFileName( 129. currentPath+FileName,partName); 130. temp=temp.replaceAll(,); 131. StringregexStr=ApacheRegexString.regexString(temp); 132. if(regexStr!=null) 133. list.add(rege
11、xStr); 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);
12、147. FilePathArr.add(FileCurrentNameAndPath); 148. list.clear(); 149. 150. reader.close(); 151. catch(IOExceptione) 152. e.printStackTrace(); 153. finally 154. if(reader!=null) 155. try 156. reader.close(); 157. catch(IOExceptione) 158. /TODOAuto-generatedcatchblock 159. e.printStackTrace(); 160. 16
13、1. 162. 163. returnFilePathArr; 164. 165. 166. /* 167. *新建目录 168. * 169. *paramfolderPath 170. *目录 171. *return返回目录创建后的路径 172. */173. publicvoidcreateFolder(StringfolderPath) 174. Stringpath=folderPath; 175. StringTokenizerst=newStringTokenizer(path,/); 176. Stringpath1=st.nextToken()+/; 177. String
14、path2=path1; 178. while(st.hasMoreTokens() 179. path1=st.nextToken()+/; 180. path2+=path1; 181. Fileinbox=newFile(path2); 182. if(!inbox.exists() 183. inbox.mkdir(); 184. 185. 186. 187. publicListgetFilePathArr() 188. returnFilePathArr; 189. 190. 191. publicstaticvoidmain(Stringargs) 192. SeparatorB
15、yLineseparator=newSeparatorByLine(); 193. StringfileAndPath=e:testobject_access_20091119.log;/文件名及路径 194. longblockSize=300000000;/每一个文件块的大小,大小是按字节计算 195. Iteratorit=separator.separatorFileByLine(fileAndPath, 196. blockSize).iterator(); 197. while(it.hasNext() 198. System.out.println(=+it.next(); 19
16、9. 200. 201. 202. 203. package com.yesky.apachelog.util.file;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;import java.util
17、.Iterator;import java.util.List;import java.util.StringTokenizer;import com.yesky.apachelog.util.regex.ApacheRegexString;/* * 按行读取文件,并按行数对文件进行拆分 */public class SeparatorByLine List FilePathArr = new ArrayList(); String FileName = null;/ 原文件名 long FileSize = 0;/ 原文件的大小 public SeparatorByLine() /* * *
18、 param fileAndPath * 原文件名及路径 */ private void getFileAttribute(String fileAndPath)/ 取得原文件的属性 File file = new File(fileAndPath); FileName = file.getName(); FileSize = file.length(); /* * * param fileAndPath * 原文件及完整路径 * param currentBlock * 当前块的序号 * return 现在拆分后块的文件名 */ private String generateSeparato
19、rFileName(String fileAndPath, int currentBlock)/ 生成折分后的文件名,以便于将来合并 return fileAndPath + .part + currentBlock; /* * 按行写文件 * * param fileSeparateName:拆分的文件名及路径 * param tempLine:一行的内容 * return */ private boolean writeFileByLine(String fileSeparateName, List tempList)/ 往硬盘写文件 BufferedWriter writer = nul
20、l; try writer = new BufferedWriter(new FileWriter(fileSeparateName, true), 10 * 1024 * 1024); Iterator it = tempList.iterator(); while (it.hasNext() String s = (String) it.next(); writer.append(s); writer.newLine(); writer.flush(); writer.close(); catch (IOException e) e.printStackTrace(); return fa
21、lse; finally if (writer != null) try writer.close(); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace(); return true; /* * 拆分文件, * * param fileAndPath * param currentPath * param linNum:行数 * return:路径, */ public List separatorFileByLine(String fileAndPath, String currentPath, long linNum)/ 折分文件主函数 List list = new ArrayList(); getFileAttribute(fileAndPath);/ 将文件的名及大小属性取出来 if (null != currentPath & !().equals(currentPath) this.createFolder(currentPath); BufferedReader reader = null; try reader = n
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1