1 packagecom.cnblogs.yjmyzz;2
3 import java.io.*;4 import java.util.*;5 import java.util.concurrent.*;6
7 /**
8 * 文件处理辅助类9 *10 *@authoryjmyzz@126.com11 *@version0.212 *@since2014-11-1713 *14 */
15 public classFileUtil {16
17 /**
18 * 当前目录路径19 */
20 public static String currentWorkDir = System.getProperty("user.dir") + "\\";21
22 /**
23 * 左填充24 *25 *@paramstr26 *@paramlength27 *@paramch28 *@return
29 */
30 public static String leftPad(String str, int length, charch) {31 if (str.length() >=length) {32 returnstr;33 }34 char[] chs = new char[length];35 Arrays.fill(chs, ch);36 char[] src =str.toCharArray();37 System.arraycopy(src, 0, chs, length -src.length, src.length);38 return newString(chs);39
40 }41
42 /**
43 * 删除文件44 *45 *@paramfileName46 * 待删除的完整文件名47 *@return
48 */
49 public static booleandelete(String fileName) {50 boolean result = false;51 File f = newFile(fileName);52 if(f.exists()) {53 result =f.delete();54
55 } else{56 result = true;57 }58 returnresult;59 }60
61 /***62 * 递归获取指定目录下的所有的文件(不包括文件夹)63 *64 *@paramobj65 *@return
66 */
67 public static ArrayListgetAllFiles(String dirPath) {68 File dir = newFile(dirPath);69
70 ArrayList files = new ArrayList();71
72 if(dir.isDirectory()) {73 File[] fileArr =dir.listFiles();74 for (int i = 0; i < fileArr.length; i++) {75 File f =fileArr[i];76 if(f.isFile()) {77 files.add(f);78 } else{79 files.addAll(getAllFiles(f.getPath()));80 }81 }82 }83 returnfiles;84 }85
86 /**
87 * 获取指定目录下的所有文件(不包括子文件夹)88 *89 *@paramdirPath90 *@return
91 */
92 public static ArrayListgetDirFiles(String dirPath) {93 File path = newFile(dirPath);94 File[] fileArr =path.listFiles();95 ArrayList files = new ArrayList();96
97 for(File f : fileArr) {98 if(f.isFile()) {99 files.add(f);100 }101 }102 returnfiles;103 }104
105 /**
106 * 获取指定目录下特定文件后缀名的文件列表(不包括子文件夹)107 *108 *@paramdirPath109 * 目录路径110 *@paramsuffix111 * 文件后缀112 *@return
113 */
114 public static ArrayListgetDirFiles(String dirPath,115 finalString suffix) {116 File path = newFile(dirPath);117 File[] fileArr = path.listFiles(newFilenameFilter() {118 public booleanaccept(File dir, String name) {119 String lowerName =name.toLowerCase();120 String lowerSuffix =suffix.toLowerCase();121 if(lowerName.endsWith(lowerSuffix)) {122 return true;123 }124 return false;125 }126
127 });128 ArrayList files = new ArrayList();129
130 for(File f : fileArr) {131 if(f.isFile()) {132 files.add(f);133 }134 }135 returnfiles;136 }137
138 /**
139 * 读取文件内容140 *141 *@paramfileName142 * 待读取的完整文件名143 *@return文件内容144 *@throwsIOException145 */
146 public static String read(String fileName) throwsIOException {147 File f = newFile(fileName);148 FileInputStream fs = newFileInputStream(f);149 String result = null;150 byte[] b = new byte[fs.available()];151 fs.read(b);152 fs.close();153 result = newString(b);154 returnresult;155 }156
157 /**
158 * 写文件159 *160 *@paramfileName161 * 目标文件名162 *@paramfileContent163 * 写入的内容164 *@return
165 *@throwsIOException166 */
167 public static booleanwrite(String fileName, String fileContent)168 throwsIOException {169 boolean result = false;170 File f = newFile(fileName);171 FileOutputStream fs = newFileOutputStream(f);172 byte[] b =fileContent.getBytes();173 fs.write(b);174 fs.flush();175 fs.close();176 result = true;177 returnresult;178 }179
180 /**
181 * 追加内容到指定文件182 *183 *@paramfileName184 *@paramfileContent185 *@return
186 *@throwsIOException187 */
188 public static booleanappend(String fileName, String fileContent)189 throwsIOException {190 boolean result = false;191 File f = newFile(fileName);192 if(f.exists()) {193 RandomAccessFile rFile = new RandomAccessFile(f, "rw");194 byte[] b =fileContent.getBytes();195 long originLen =f.length();196 rFile.setLength(originLen +b.length);197 rFile.seek(originLen);198 rFile.write(b);199 rFile.close();200 }201 result = true;202 returnresult;203 }204
205 /**
206 * 拆分文件207 *208 *@paramfileName209 * 待拆分的完整文件名210 *@parambyteSize211 * 按多少字节大小拆分212 *@return拆分后的文件名列表213 *@throwsIOException214 */
215 public List splitBySize(String fileName, intbyteSize)216 throwsIOException {217 List parts = new ArrayList();218 File file = newFile(fileName);219 int count = (int) Math.ceil(file.length() / (double) byteSize);220 int countLen = (count + "").length();221 ThreadPoolExecutor threadPool = newThreadPoolExecutor(count,222 count * 3, 1, TimeUnit.SECONDS,223 new ArrayBlockingQueue(count * 2));224
225 for (int i = 0; i < count; i++) {226 String partFileName = file.getName() + "."
227 + leftPad((i + 1) + "", countLen, '0') + ".part";228 threadPool.execute(new SplitRunnable(byteSize, i *byteSize,229 partFileName, file));230 parts.add(partFileName);231 }232 returnparts;233 }234
235 /**
236 * 合并文件237 *238 *@paramdirPath239 * 拆分文件所在目录名240 *@parampartFileSuffix241 * 拆分文件后缀名242 *@parampartFileSize243 * 拆分文件的字节数大小244 *@parammergeFileName245 * 合并后的文件名246 *@throwsIOException247 */
248 public voidmergePartFiles(String dirPath, String partFileSuffix,249 int partFileSize, String mergeFileName) throwsIOException {250 ArrayList partFiles =FileUtil.getDirFiles(dirPath,251 partFileSuffix);252 Collections.sort(partFiles, newFileComparator());253
254 RandomAccessFile randomAccessFile = newRandomAccessFile(mergeFileName,255 "rw");256 randomAccessFile.setLength(partFileSize * (partFiles.size() - 1)257 + partFiles.get(partFiles.size() - 1).length());258 randomAccessFile.close();259
260 ThreadPoolExecutor threadPool = newThreadPoolExecutor(261 partFiles.size(), partFiles.size() * 3, 1, TimeUnit.SECONDS,262 new ArrayBlockingQueue(partFiles.size() * 2));263
264 for (int i = 0; i < partFiles.size(); i++) {265 threadPool.execute(new MergeRunnable(i *partFileSize,266 mergeFileName, partFiles.get(i)));267 }268
269 }270
271 /**
272 * 根据文件名,比较文件273 *274 *@authoryjmyzz@126.com275 *276 */
277 private class FileComparator implements Comparator{278 public intcompare(File o1, File o2) {279 returno1.getName().compareToIgnoreCase(o2.getName());280 }281 }282
283 /**
284 * 分割处理Runnable285 *286 *@authoryjmyzz@126.com287 *288 */
289 private class SplitRunnable implementsRunnable {290 intbyteSize;291 String partFileName;292 File originFile;293 intstartPos;294
295 public SplitRunnable(int byteSize, intstartPos, String partFileName,296 File originFile) {297 this.startPos =startPos;298 this.byteSize =byteSize;299 this.partFileName =partFileName;300 this.originFile =originFile;301 }302
303 public voidrun() {304 RandomAccessFile rFile;305 OutputStream os;306 try{307 rFile = new RandomAccessFile(originFile, "r");308 byte[] b = new byte[byteSize];309 rFile.seek(startPos);//移动指针到每“段”开头
310 int s =rFile.read(b);311 os = newFileOutputStream(partFileName);312 os.write(b, 0, s);313 os.flush();314 os.close();315 } catch(IOException e) {316 e.printStackTrace();317 }318 }319 }320
321 /**
322 * 合并处理Runnable323 *324 *@authoryjmyzz@126.com325 *326 */
327 private class MergeRunnable implementsRunnable {328 longstartPos;329 String mergeFileName;330 File partFile;331
332 public MergeRunnable(longstartPos, String mergeFileName, File partFile) {333 this.startPos =startPos;334 this.mergeFileName =mergeFileName;335 this.partFile =partFile;336 }337
338 public voidrun() {339 RandomAccessFile rFile;340 try{341 rFile = new RandomAccessFile(mergeFileName, "rw");342 rFile.seek(startPos);343 FileInputStream fs = newFileInputStream(partFile);344 byte[] b = new byte[fs.available()];345 fs.read(b);346 fs.close();347 rFile.write(b);348 rFile.close();349 } catch(IOException e) {350 e.printStackTrace();351 }352 }353 }354
355 }