ImageVerifierCode 换一换
格式:DOCX , 页数:37 ,大小:35.78KB ,
资源ID:4641614      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/4641614.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Java文件的拆分与合并.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Java文件的拆分与合并.docx

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