注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

放飞自由

年轻人,如果三年的时间里,没有任何想法,他这一生,就基本这个样子,没有多大改变。

 
 
 

日志

 
 

FileUtil(一)  

2011-05-24 20:12:10|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
[code lang="java"] import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; public class FileUtil { /** * 创建文件夹 * @param path */ public static void createFolder(String path){ try { File file = new File(path); if(!file.exists()){ file.mkdir(); }else{ System.err.println("exist!!"); } } catch (Exception e) { e.printStackTrace(); } System.out.println("------------over------------"); } /** * 创建文本,写入内容 * @param path */ public static void createFile(String path){ BufferedWriter bw = null; try { bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(path)),"utf-8")); for (int i = 0; i < 100 i bwwritei><<<<<<<"); bw.newLine(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ if(null != bw){ try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 将primaryPath文件路径下的文件复制到newPath文件下 * * @param primaryPath 原文件路径 如:c:/test * @param newPath 复制后路径 如:f:/test/test2 */ public static void copyFolder(String primaryPath, String targetPath) { try { (new File(targetPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹 File a = new File(primaryPath); String[] file = a.list(); File temp = null; for (int i = 0; i < file length i if primaryPathendsWithFileseparator temp ="new" FileprimaryPath filei else temp ="new" FileprimaryPath Fileseparator filei if tempisFile FileInputStream input ="new" FileInputStreamtemp FileOutputStream output ="new" FileOutputStreamtargetPath tempgetNametoString byte b ="new" byte int len while len ="input.read(b))" - outputwriteb len outputflush outputclose inputclose if tempisDirectory copyFolderprimaryPath filei targetPath filei catch FileNotFoundException ex exprintStackTrace catch Exception e eprintStackTrace Systemoutprintln------------over------------ param primaryPath param targetPath public static void copyFileString primaryPathString targetPath BufferedReader br ="null;" BufferedWriter bw ="null;" try br ="new" BufferedReadernew InputStreamReadernew FileInputStreamnew FileprimaryPath utf- bw ="new" BufferedWriternew OutputStreamWriternew FileOutputStreamnew FiletargetPathutf- String content ="" whilenull ="(content" brreadLine bwwritecontent do something such as replace eg bwnewLine catch UnsupportedEncodingException e eprintStackTrace catch FileNotFoundException e eprintStackTrace catch IOException e eprintStackTrace finally ifnull ="bw){" try bwclose catch IOException e eprintStackTrace ifnull ="br){" try brclose catch IOException e eprintStackTrace Systemoutprintln------------over------------ deleteDir param f return public static boolean deleteFileFile f boolean result ="false;" try if fcanWrite result ="f.delete();" else result ="false;" Systemoutprintlnfile no exists or file is readOnly catch Exception e eprintStackTrace result ="false;" return result param f return public static boolean deleteDirFile f boolean result ="false;" if fisDirectory File files ="f.listFiles();" for File file files result ="deleteDir(file);" if result return false else result ="deleteFile(f);" return false pathprimaryNamenewName param path param paimaryName param newName public static void renameString pathString paimaryNameString newName new File path Fileseparator paimaryNamerenameTonew File path Fileseparator newName public static void mainString args copyFolderChouzhiqingtxtEhou_test copyFilecfilexlsehou createFoldercfucktxt createFilecfucktxt deleteFilenew Filecfuck_test new Filecfuck_testtestdelete renamecfuck_testhouzhiqing code >
  评论这张
 
阅读(47)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017