`
wmj2003
  • 浏览: 102019 次
  • 来自: ...
文章分类
社区版块
存档分类
最新评论

文件操作FileUtil.java

阅读更多
  1. packagecom.work.util;
  2. importjava.io.BufferedInputStream;
  3. importjava.io.BufferedOutputStream;
  4. importjava.io.BufferedReader;
  5. importjava.io.BufferedWriter;
  6. importjava.io.File;
  7. importjava.io.FileInputStream;
  8. importjava.io.FileNotFoundException;
  9. importjava.io.FileOutputStream;
  10. importjava.io.IOException;
  11. importjava.io.InputStreamReader;
  12. importjava.io.OutputStreamWriter;
  13. importjava.io.RandomAccessFile;
  14. importjava.io.StringReader;
  15. importjava.util.ArrayList;
  16. importjava.util.List;
  17. importjava.util.zip.ZipEntry;
  18. importjava.util.zip.ZipOutputStream;
  19. importorg.apache.commons.logging.Log;
  20. importorg.apache.commons.logging.LogFactory;
  21. /**
  22. *日期:2008-2-1412:05:18<br/>
  23. *project:zxj<br/>
  24. *作者:wangmingjie<br/>
  25. */
  26. publicclassFileUtil{
  27. privatestaticLoglog=LogFactory.getLog(FileUtil.class);
  28. /**
  29. *创建单个文件夹。
  30. *
  31. *@paramdir
  32. *@paramignoreIfExitst
  33. *true表示如果文件夹存在就不再创建了。false是重新创建。
  34. *@throwsIOException
  35. */
  36. publicstaticvoidcreateDir(Stringdir,booleanignoreIfExitst)
  37. throwsIOException{
  38. Filefile=newFile(dir);
  39. if(ignoreIfExitst&&file.exists()){
  40. return;
  41. }
  42. if(file.mkdir()==false){
  43. thrownewIOException("Cannotcreatethedirectory="+dir);
  44. }
  45. }
  46. /**
  47. *创建多个文件夹
  48. *
  49. *@paramdir
  50. *@paramignoreIfExitst
  51. *@throwsIOException
  52. */
  53. publicstaticvoidcreateDirs(Stringdir,booleanignoreIfExitst)
  54. throwsIOException{
  55. Filefile=newFile(dir);
  56. if(ignoreIfExitst&&file.exists()){
  57. return;
  58. }
  59. if(file.mkdirs()==false){
  60. thrownewIOException("Cannotcreatedirectories="+dir);
  61. }
  62. }
  63. /**
  64. *删除一个文件。
  65. *
  66. *@paramfilename
  67. *@throwsIOException
  68. */
  69. publicstaticvoiddeleteFile(Stringfilename)throwsIOException{
  70. Filefile=newFile(filename);
  71. log.trace("Deletefile="+filename);
  72. if(file.isDirectory()){
  73. thrownewIOException(
  74. "IOException->BadInputException:notafile.");
  75. }
  76. if(file.exists()==false){
  77. thrownewIOException(
  78. "IOException->BadInputException:fileisnotexist.");
  79. }
  80. if(file.delete()==false){
  81. thrownewIOException("Cannotdeletefile.filename="+filename);
  82. }
  83. }
  84. /**
  85. *删除文件夹及其下面的子文件夹
  86. *
  87. *@paramdir
  88. *@throwsIOException
  89. */
  90. publicstaticvoiddeleteDir(Filedir)throwsIOException{
  91. if(dir.isFile())
  92. thrownewIOException(
  93. "IOException->BadInputException:notadirectory.");
  94. File[]files=dir.listFiles();
  95. if(files!=null){
  96. for(inti=0;i<files.length;i++){
  97. Filefile=files[i];
  98. if(file.isFile()){
  99. file.delete();
  100. }else{
  101. deleteDir(file);
  102. }
  103. }
  104. }//if
  105. dir.delete();
  106. }
  107. publicstaticStringgetPathSeparator(){
  108. returnjava.io.File.pathSeparator;
  109. }
  110. publicstaticStringgetFileSeparator(){
  111. returnjava.io.File.separator;
  112. }
  113. /**
  114. *列出指定文件目录下面的文件信息。
  115. *
  116. *@paramdir
  117. *@return
  118. *@throwsIOException
  119. */
  120. publicstaticList<FileInfo>getFiles(Filedir)throwsIOException{
  121. if(dir.isFile())
  122. thrownewIOException("BadInputException:notadirectory.");
  123. if(!dir.exists()){
  124. thrownewIOException("don'texist");
  125. }
  126. File[]files=dir.listFiles();
  127. intLEN=0;
  128. if(files!=null){
  129. LEN=files.length;
  130. }
  131. List<FileInfo>l=newArrayList<FileInfo>();
  132. longtempFLen=0;//文件长度
  133. for(inti=0;i<LEN;i++){
  134. FileInfotemp=newFileInfo();
  135. temp.setFileName(files[i].getName());
  136. temp.setIsDir(files[i].isDirectory());
  137. //是文件,且包含.
  138. if(files[i].isFile()){
  139. if(files[i].getName().lastIndexOf(".")!=-1)
  140. temp.setFileType(files[i].getName().substring(
  141. files[i].getName().lastIndexOf(".")));
  142. }else{
  143. temp.setFileType("文件夹");
  144. }
  145. tempFLen=files[i].length();
  146. temp.setFileLen(tempFLen);
  147. if(tempFLen/1024/1024/1024>0){
  148. temp.setFileLength(files[i].length()/1024/1024/1024+"G");
  149. }elseif(tempFLen/1024/1024>0){
  150. temp.setFileLength(files[i].length()/1024/1024+"M");
  151. }elseif(tempFLen/1024>0){
  152. temp.setFileLength(files[i].length()/1024+"K");
  153. }else{
  154. temp.setFileLength(tempFLen+"byte");
  155. }
  156. temp.setFilePath(files[i].getAbsolutePath().replaceAll("[\\\\]","/"));
  157. temp.setLastModifiedTime(com.work.util.DateUtil
  158. .getDateTime(files[i].lastModified()));
  159. temp.setIsHidden(files[i].isHidden());
  160. temp.setAuthor(null);
  161. temp.setVersion(null);
  162. temp.setFileClass(null);
  163. temp.setRemark(null);
  164. l.add(temp);
  165. }
  166. returnl;
  167. }
  168. /**
  169. *获取到目录下面文件的大小。包含了子目录。
  170. *
  171. *@paramdir
  172. *@return
  173. *@throwsIOException
  174. */
  175. publicstaticlonggetDirLength(Filedir)throwsIOException{
  176. if(dir.isFile())
  177. thrownewIOException("BadInputException:notadirectory.");
  178. longsize=0;
  179. File[]files=dir.listFiles();
  180. if(files!=null){
  181. for(inti=0;i<files.length;i++){
  182. Filefile=files[i];
  183. //file.getName();
  184. //System.out.println(file.getName());
  185. longlength=0;
  186. if(file.isFile()){
  187. length=file.length();
  188. }else{
  189. length=getDirLength(file);
  190. }
  191. size+=length;
  192. }//for
  193. }//if
  194. returnsize;
  195. }
  196. /**
  197. *将文件清空。
  198. *
  199. *@paramsrcFilename
  200. *@throwsIOException
  201. */
  202. publicstaticvoidemptyFile(StringsrcFilename)throwsIOException{
  203. FilesrcFile=newFile(srcFilename);
  204. if(!srcFile.exists()){
  205. thrownewFileNotFoundException("Cannotfindthefile:"
  206. +srcFile.getAbsolutePath());
  207. }
  208. if(!srcFile.canWrite()){
  209. thrownewIOException("Cannotwritethefile:"
  210. +srcFile.getAbsolutePath());
  211. }
  212. FileOutputStreamoutputStream=newFileOutputStream(srcFilename);
  213. outputStream.close();
  214. }
  215. /**
  216. *WritecontenttoafileNamewiththedestEncoding写文件。如果此文件不存在就创建一个。
  217. *
  218. *@paramcontent
  219. *String
  220. *@paramfileName
  221. *String
  222. *@paramdestEncoding
  223. *String
  224. *@throwsFileNotFoundException
  225. *@throwsIOException
  226. */
  227. publicstaticvoidwriteFile(Stringcontent,StringfileName,
  228. StringdestEncoding)throwsFileNotFoundException,IOException{
  229. Filefile=null;
  230. try{
  231. file=newFile(fileName);
  232. if(!file.exists()){
  233. if(file.createNewFile()==false){
  234. thrownewIOException("createfile'"+fileName
  235. +"'failure.");
  236. }
  237. }
  238. if(file.isFile()==false){
  239. thrownewIOException("'"+fileName+"'isnotafile.");
  240. }
  241. if(file.canWrite()==false){
  242. thrownewIOException("'"+fileName+"'isaread-onlyfile.");
  243. }
  244. }finally{
  245. //wedonthavetocloseFilehere
  246. }
  247. BufferedWriterout=null;
  248. try{
  249. FileOutputStreamfos=newFileOutputStream(fileName);
  250. out=newBufferedWriter(newOutputStreamWriter(fos,destEncoding));
  251. out.write(content);
  252. out.flush();
  253. }catch(FileNotFoundExceptionfe){
  254. log.error("Error",fe);
  255. throwfe;
  256. }catch(IOExceptione){
  257. log.error("Error",e);
  258. throwe;
  259. }finally{
  260. try{
  261. if(out!=null)
  262. out.close();
  263. }catch(IOExceptionex){
  264. }
  265. }
  266. }
  267. /**
  268. *读取文件的内容,并将文件内容以字符串的形式返回。
  269. *
  270. *@paramfileName
  271. *@paramsrcEncoding
  272. *@return
  273. *@throwsFileNotFoundException
  274. *@throwsIOException
  275. */
  276. publicstaticStringreadFile(StringfileName,StringsrcEncoding)
  277. throwsFileNotFoundException,IOException{
  278. Filefile=null;
  279. try{
  280. file=newFile(fileName);
  281. if(file.isFile()==false){
  282. thrownewIOException("'"+fileName+"'isnotafile.");
  283. }
  284. }finally{
  285. //wedonthavetocloseFilehere
  286. }
  287. BufferedReaderreader=null;
  288. try{
  289. StringBufferresult=newStringBuffer(1024);
  290. FileInputStreamfis=newFileInputStream(fileName);
  291. reader=newBufferedReader(newInputStreamReader(fis,srcEncoding));
  292. char[]block=newchar[512];
  293. while(true){
  294. intreadLength=reader.read(block);
  295. if(readLength==-1)
  296. break;//endoffile
  297. result.append(block,0,readLength);
  298. }
  299. returnresult.toString();
  300. }catch(FileNotFoundExceptionfe){
  301. log.error("Error",fe);
  302. throwfe;
  303. }catch(IOExceptione){
  304. log.error("Error",e);
  305. throwe;
  306. }finally{
  307. try{
  308. if(reader!=null)
  309. reader.close();
  310. }catch(IOExceptionex){
  311. }
  312. }
  313. }
  314. /*
  315. *1ABC2abCGiasudoctudong1laycathay5dong=>1-->53ABC
  316. */
  317. publicstaticString[]getLastLines(Filefile,intlinesToReturn)
  318. throwsIOException,FileNotFoundException{
  319. finalintAVERAGE_CHARS_PER_LINE=250;
  320. finalintBYTES_PER_CHAR=2;
  321. RandomAccessFilerandomAccessFile=null;
  322. StringBufferbuffer=newStringBuffer(linesToReturn
  323. *AVERAGE_CHARS_PER_LINE);
  324. intlineTotal=0;
  325. try{
  326. randomAccessFile=newRandomAccessFile(file,"r");
  327. longbyteTotal=randomAccessFile.length();
  328. longbyteEstimateToRead=linesToReturn*AVERAGE_CHARS_PER_LINE
  329. *BYTES_PER_CHAR;
  330. longoffset=byteTotal-byteEstimateToRead;
  331. if(offset<0){
  332. offset=0;
  333. }
  334. randomAccessFile.seek(offset);
  335. //log.debug("SKIPIS::"+offset);
  336. Stringline=null;
  337. StringlineUTF8=null;
  338. while((line=randomAccessFile.readLine())!=null){
  339. lineUTF8=newString(line.getBytes("ISO8859_1"),"UTF-8");
  340. lineTotal++;
  341. buffer.append(lineUTF8).append("\n");
  342. }
  343. }finally{
  344. if(randomAccessFile!=null){
  345. try{
  346. randomAccessFile.close();
  347. }catch(IOExceptionex){
  348. }
  349. }
  350. }
  351. String[]resultLines=newString[linesToReturn];
  352. BufferedReaderin=null;
  353. try{
  354. in=newBufferedReader(newStringReader(buffer.toString()));
  355. intstart=lineTotal/*+2*/-linesToReturn;//Ex:55-10=45
  356. //~offset
  357. if(start<0)
  358. start=0;//notstartline
  359. for(inti=0;i<start;i++){
  360. in.readLine();//loopuntiltheoffset.Ex:loop0,1~~2
  361. //lines
  362. }
  363. inti=0;
  364. Stringline=null;
  365. while((line=in.readLine())!=null){
  366. resultLines[i]=line;
  367. i++;
  368. }
  369. }catch(IOExceptionie){
  370. log.error("Error"+ie);
  371. throwie;
  372. }finally{
  373. if(in!=null){
  374. try{
  375. in.close();
  376. }catch(IOExceptionex){
  377. }
  378. }
  379. }
  380. returnresultLines;
  381. }
  382. /**
  383. *单个文件拷贝。
  384. *
  385. *@paramsrcFilename
  386. *@paramdestFilename
  387. *@paramoverwrite
  388. *@throwsIOException
  389. */
  390. publicstaticvoidcopyFile(StringsrcFilename,StringdestFilename,
  391. booleanoverwrite)throwsIOException{
  392. FilesrcFile=newFile(srcFilename);
  393. //首先判断源文件是否存在
  394. if(!srcFile.exists()){
  395. thrownewFileNotFoundException("Cannotfindthesourcefile:"
  396. +srcFile.getAbsolutePath());
  397. }
  398. //判断源文件是否可读
  399. if(!srcFile.canRead()){
  400. thrownewIOException("Cannotreadthesourcefile:"
  401. +srcFile.getAbsolutePath());
  402. }
  403. FiledestFile=newFile(destFilename);
  404. if(overwrite==false){
  405. //目标文件存在就不覆盖
  406. if(destFile.exists())
  407. return;
  408. }else{
  409. //如果要覆盖已经存在的目标文件,首先判断是否目标文件可写。
  410. if(destFile.exists()){
  411. if(!destFile.canWrite()){
  412. thrownewIOException("Cannotwritethedestinationfile:"
  413. +destFile.getAbsolutePath());
  414. }
  415. }else{
  416. //不存在就创建一个新的空文件。
  417. if(!destFile.createNewFile()){
  418. thrownewIOException("Cannotwritethedestinationfile:"
  419. +destFile.getAbsolutePath());
  420. }
  421. }
  422. }
  423. BufferedInputStreaminputStream=null;
  424. BufferedOutputStreamoutputStream=null;
  425. byte[]block=newbyte[1024];
  426. try{
  427. inputStream=newBufferedInputStream(newFileInputStream(srcFile));
  428. outputStream=newBufferedOutputStream(newFileOutputStream(
  429. destFile));
  430. while(true){
  431. intreadLength=inputStream.read(block);
  432. if(readLength==-1)
  433. break;//endoffile
  434. outputStream.write(block,0,readLength);
  435. }
  436. }finally{
  437. if(inputStream!=null){
  438. try{
  439. inputStream.close();
  440. }catch(IOExceptionex){
  441. //justignore
  442. }
  443. }
  444. if(outputStream!=null){
  445. try{
  446. outputStream.close();
  447. }catch(IOExceptionex){
  448. //justignore
  449. }
  450. }
  451. }
  452. }
  453. /**
  454. *单个文件拷贝。
  455. *
  456. *@paramsrcFile
  457. *@paramdestFile
  458. *@paramoverwrite
  459. *是否覆盖目的文件
  460. *@throwsIOException
  461. */
  462. publicstaticvoidcopyFile(FilesrcFile,FiledestFile,booleanoverwrite)
  463. throwsIOException{
  464. //首先判断源文件是否存在
  465. if(!srcFile.exists()){
  466. thrownewFileNotFoundException("Cannotfindthesourcefile:"
  467. +srcFile.getAbsolutePath());
  468. }
  469. //判断源文件是否可读
  470. if(!srcFile.canRead()){
  471. thrownewIOException("Cannotreadthesourcefile:"
  472. +srcFile.getAbsolutePath());
  473. }
  474. if(overwrite==false){
  475. //目标文件存在就不覆盖
  476. if(destFile.exists())
  477. return;
  478. }else{
  479. //如果要覆盖已经存在的目标文件,首先判断是否目标文件可写。
  480. if(destFile.exists()){
  481. if(!destFile.canWrite()){
  482. thrownewIOException("Cannotwritethedestinationfile:"
  483. +destFile.getAbsolutePath());
  484. }
  485. }else{
  486. //不存在就创建一个新的空文件。
  487. if(!destFile.createNewFile()){
  488. thrownewIOException("Cannotwritethedestinationfile:"
  489. +destFile.getAbsolutePath());
  490. }
  491. }
  492. }
  493. BufferedInputStreaminputStream=null;
  494. BufferedOutputStreamoutputStream=null;
  495. byte[]block=newbyte[1024];
  496. try{
  497. inputStream=newBufferedInputStream(newFileInputStream(srcFile));
  498. outputStream=newBufferedOutputStream(newFileOutputStream(
  499. destFile));
  500. while(true){
  501. intreadLength=inputStream.read(block);
  502. if(readLength==-1)
  503. break;//endoffile
  504. outputStream.write(block,0,readLength);
  505. }
  506. }finally{
  507. if(inputStream!=null){
  508. try{
  509. inputStream.close();
  510. }catch(IOExceptionex){
  511. //justignore
  512. }
  513. }
  514. if(outputStream!=null){
  515. try{
  516. outputStream.close();
  517. }catch(IOExceptionex){
  518. //justignore
  519. }
  520. }
  521. }
  522. }
  523. /**
  524. *拷贝文件,从源文件夹拷贝文件到目的文件夹。<br>
  525. *参数源文件夹和目的文件夹,最后都不要带文件路径符号,例如:c:/aa正确,c:/aa/错误。
  526. *
  527. *@paramsrcDirName
  528. *源文件夹名称,例如:c:/test/aa或者c:\\test\\aa
  529. *@paramdestDirName
  530. *目的文件夹名称,例如:c:/test/aa或者c:\\test\\aa
  531. *@paramoverwrite
  532. *是否覆盖目的文件夹下面的文件。
  533. *@throwsIOException
  534. */
  535. publicstaticvoidcopyFiles(StringsrcDirName,StringdestDirName,
  536. booleanoverwrite)throwsIOException{
  537. FilesrcDir=newFile(srcDirName);//声明源文件夹
  538. //首先判断源文件夹是否存在
  539. if(!srcDir.exists()){
  540. thrownewFileNotFoundException(
  541. "Cannotfindthesourcedirectory:"
  542. +srcDir.getAbsolutePath());
  543. }
  544. FiledestDir=newFile(destDirName);
  545. if(overwrite==false){
  546. if(destDir.exists()){
  547. //donothing
  548. }else{
  549. if(destDir.mkdirs()==false){
  550. thrownewIOException(
  551. "Cannotcreatethedestinationdirectories="
  552. +destDir);
  553. }
  554. }
  555. }else{
  556. //覆盖存在的目的文件夹
  557. if(destDir.exists()){
  558. //donothing
  559. }else{
  560. //createanewdirectory
  561. if(destDir.mkdirs()==false){
  562. thrownewIOException(
  563. "Cannotcreatethedestinationdirectories="
  564. +destDir);
  565. }
  566. }
  567. }
  568. //循环查找源文件夹目录下面的文件(屏蔽子文件夹),然后将其拷贝到指定的目的文件夹下面。
  569. File[]srcFiles=srcDir.listFiles();
  570. if(srcFiles==null||srcFiles.length<1){
  571. //thrownewIOException("Cannotfindanyfilefromsource
  572. //directory!!!");
  573. return;//donothing
  574. }
  575. //开始复制文件
  576. intSRCLEN=srcFiles.length;
  577. for(inti=0;i<SRCLEN;i++){
  578. //FiletempSrcFile=srcFiles[i];
  579. FiledestFile=newFile(destDirName+File.separator
  580. +srcFiles[i].getName());
  581. //注意构造文件对象时候,文件名字符串中不能包含文件路径分隔符";".
  582. //log.debug(destFile);
  583. if(srcFiles[i].isFile()){
  584. copyFile(srcFiles[i],destFile,overwrite);
  585. }else{
  586. //在这里进行递归调用,就可以实现子文件夹的拷贝
  587. copyFiles(srcFiles[i].getAbsolutePath(),destDirName
  588. +File.separator+srcFiles[i].getName(),overwrite);
  589. }
  590. }
  591. }
  592. /**
  593. *压缩文件。注意:中文文件名称和中文的评论会乱码。
  594. *@paramsrcFilename
  595. *@paramdestFilename
  596. *@paramoverwrite
  597. *@throwsIOException
  598. */
  599. publicstaticvoidzipFile(StringsrcFilename,StringdestFilename,
  600. booleanoverwrite)throwsIOException{
  601. FilesrcFile=newFile(srcFilename);
  602. //首先判断源文件是否存在
  603. if(!srcFile.exists()){
  604. thrownewFileNotFoundException("Cannotfindthesourcefile:"
  605. +srcFile.getAbsolutePath());
  606. }
  607. //判断源文件是否可读
  608. if(!srcFile.canRead()){
  609. thrownewIOException("Cannotreadthesourcefile:"
  610. +srcFile.getAbsolutePath());
  611. }
  612. if(destFilename==null||destFilename.trim().equals("")){
  613. destFilename=srcFilename+".zip";
  614. }else{
  615. destFilename+=".zip";
  616. }
  617. FiledestFile=newFile(destFilename);
  618. if(overwrite==false){
  619. //目标文件存在就不覆盖
  620. if(destFile.exists())
  621. return;
  622. }else{
  623. //如果要覆盖已经存在的目标文件,首先判断是否目标文件可写。
  624. if(destFile.exists()){
  625. if(!destFile.canWrite()){
  626. thrownewIOException("Cannotwritethedestinationfile:"
  627. +destFile.getAbsolutePath());
  628. }
  629. }else{
  630. //不存在就创建一个新的空文件。
  631. if(!destFile.createNewFile()){
  632. thrownewIOException("Cannotwritethedestinationfile:"
  633. +destFile.getAbsolutePath());
  634. }
  635. }
  636. }
  637. BufferedInputStreaminputStream=null;
  638. BufferedOutputStreamoutputStream=null;
  639. ZipOutputStreamzipOutputStream=null;
  640. byte[]block=newbyte[1024];
  641. try{
  642. inputStream=newBufferedInputStream(newFileInputStream(srcFile));
  643. outputStream=newBufferedOutputStream(newFileOutputStream(destFile));
  644. zipOutputStream=newZipOutputStream(outputStream);
  645. zipOutputStream.setComment("通过java程序压缩的");
  646. ZipEntryzipEntry=newZipEntry(srcFile.getName());
  647. zipEntry.setComment("zipEntry通过java程序压缩的");
  648. zipOutputStream.putNextEntry(zipEntry);
  649. while(true){
  650. intreadLength=inputStream.read(block);
  651. if(readLength==-1)
  652. break;//endoffile
  653. zipOutputStream.write(block,0,readLength);
  654. }
  655. zipOutputStream.flush();
  656. zipOutputStream.finish();
  657. }finally{
  658. if(inputStream!=null){
  659. try{
  660. inputStream.close();
  661. }catch(IOExceptionex){
  662. //justignore
  663. }
  664. }
  665. if(outputStream!=null){
  666. <span
    分享到:
    评论

相关推荐

    FileUtil(文件操作工具类)

    `FileUtil.java`源码中,这些方法通常会使用Java的`java.io`包提供的基本文件操作类,如`File`, `FileInputStream`, `FileOutputStream`, `BufferedReader`, `BufferedWriter`等。通过封装这些基本操作,`FileUtil`...

    java中用FileUtil实现文件读写

    ### Java中用FileUtil实现文件读写 ...通过上述介绍可以看出,`FileUtil` 工具类在Java项目中对于简化文件操作方面具有重要的作用。开发者可以根据具体需求进一步扩展此类的功能,以便更好地服务于项目的需求。

    FileUtil.java

    该工具类专门转对于Java中的数据文件的移动,复制,拷贝等方法,为开发者提供一系列的便捷的操作方法!极大的方便开发者开发!!

    一个常用的Java文件操作类FileUtil.java源码下载

    一个常用的Java文件操作类FileUtil.java源代码,类中的所有方法都是静态方法,不需要生成此类的实例,这些Java文件操作类主要有修改文件的最后访问时间、判断指定的文件是否存在、创建指定的目录、清空指定目录中的...

    java工具类:文件操作工具类.java

    public class FileUtil { protected static Logger log = LoggerFactory.getLogger(FileUtil.class); /** * 压缩文件 * @param inputFileName 要压缩的文件或文件夹路径,例如:c:\\a.txt,c:\\a\ * ...

    FileUtil.java.rar_Java编程_Java_

    在Java编程领域,FileUtil.java是一个常见的工具类...实际使用中,根据项目需求,FileUtil.java可能会包含更多的定制化功能,以满足各种复杂的文件操作场景。了解并熟练使用这类工具类,对于提升Java开发效率至关重要。

    Java文件处理工具类--FileUtil

    import java.io.*; /** * FileUtil. Simple file operation class. * * @author BeanSoft * */ public class FileUtil { /** * The buffer. */ protected static byte buf[] = new byte[1024]; /**...

    FileUtil.rar

    1. **文件操作**:Java的java.io和java.nio包提供了丰富的文件操作API,包括读写、复制、删除等,用于处理本地文件。 2. **网络通信**:如果共享目录位于远程服务器,项目可能采用了Java的Socket编程或基于...

    30个java工具类

    [工具类] 文件操作工具类 .java.txt [工具类] 序列化保存为XML文件的工具类 .java.txt [工具类] 一个压缩工具类.java.txt [工具类] 用java编写简单UDP网络通信程序 .java.txt [工具类] 中文验证.jsp.txt [工具类] ...

    【强2】30个java工具类

    [工具类] 文件操作工具类 .java.txt [工具类] 序列化保存为XML文件的工具类 .java.txt [工具类] 一个压缩工具类.java.txt [工具类] 用java编写简单UDP网络通信程序 .java.txt [工具类] 中文验证.jsp.txt [工具类] ...

    FileUtil

    "FileUtil"是一个Java工具类库,用于处理与文件操作相关的任务。在Java开发中,文件操作是一项常见的任务,例如读取、写入、移动、复制文件等。FileUtil类通常封装了这些基本操作,提供了更简洁、易用的API,以减少...

    FileUtil类文件整理

    1. **文件操作**:`FileUtil`类可能会有用于创建新文件的方法,如`createFile(String filePath)`,这个方法会根据指定的路径创建一个新的文件。此外,还可能包含删除文件的方法,如`deleteFile(String filePath)`,...

    使用POI读写Excel文件(兼容xls与xlsx版本)

    3. `BaseExcelFileUtil.java`:这个名字暗示它可能是一个基础类,包含了通用的Excel文件操作方法,如打开工作簿、创建工作表、设置单元格样式等。这两个子类(2003和2007版本的文件工具)可能会继承或引用这个基类,...

    V3FileUtil.rar_Java编程_Java_

    在Java编程领域,文件操作是常见且重要的任务之一。标题中的"V3FileUtil.rar"表明这是一个关于处理V3格式文件的工具类,而描述提到的是将V3格式的音频文件转换为WAV格式,这涉及到音频文件的编码与解码技术。在Java...

    基于JAVA的常用文件操作方法

    在Java编程语言中,文件操作是一项基础且重要的任务。这篇博文主要探讨了如何使用Java进行常见的...对于实际项目开发,编写一个`FileUtil`工具类是非常常见的做法,这样可以将文件操作封装起来,便于代码的复用和维护。

    Java文件操作工具类fileUtil实例【文件增删改,复制等】

    Java文件操作工具类fileUtil实例 Java文件操作工具类fileUtil实例是一个提供了多种文件操作方法的工具类,包括文件读取、增加、删除、修改、复制等操作。该工具类可以帮助开发者快速实现文件操作功能,提高开发效率...

    java工具类 utils.zip

    9. **FileUtil.java**:文件操作工具类,提供读写文件、创建目录、删除文件等方法。例如,`readFileToString(File file, String encoding)`用于读取文件内容,`deleteFile(String filePath)`用于删除文件。 10. **...

    一个写文件的java类

    在Java中,文件操作主要通过java.io包中的类来完成。例如,`File`类用于表示文件或目录的路径名,`FileWriter`和`BufferedWriter`则用于向文件写入文本内容。`FileUtil`类可能会包含静态方法,这些方法提供了一种...

    java 文件处理

    1. **Java.IO 包**:Java的核心库提供了`java.io`包,它包含了处理输入/输出流的类,这些类用于进行文件操作。如`File`类,用于表示文件或目录的路径名,提供了许多与文件相关的操作。 2. **File 类**:`File`类是...

    关于Java文件路径问题

    此外,Java 7引入了`java.nio.file`包,提供了更现代和强大的文件系统操作API。`Paths`类和`Path`接口提供了一种更面向对象的方式来处理路径。例如,`Paths.get("relative", "path").toAbsolutePath()`可以得到一个...

Global site tag (gtag.js) - Google Analytics