免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1419 | 回复: 2
打印 上一主题 下一主题

java file 文件操作 operate file of java java文件操作 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-03 11:47 |只看该作者 |倒序浏览
java file 文件操作 operate file of java java文件操作
  1.   1 package com.b510;  2   3 import java.io.File;  4 import java.io.FileInputStream;  5 import java.io.FileOutputStream;  6 import java.io.FileWriter;  7 import java.io.InputStream;  8 import java.io.PrintWriter;  9  10 /** 11  *  12  * @author Hongten</br> 13  *  14  *         文件的操作 15  *  16  *         参考:http://www.newsmth.net/pc/pccon.php?id=2206&nid=318002 17  *  18  *  19  */ 20 public class OperateFiles { 21  22     /** 23      * @param args 24 */ 25     public static void main(String[] args) { 26         OperateFiles operateFiles = new OperateFiles(); 27         //新建一个文件夹 28         operateFiles.newFolder("c:/hongten"); 29         //新建一个文件,同时向里面写入内容 30         operateFiles.newFile("c:/hongten/Hello.txt", "hello,i'm Hongten.你好"); 31         //删除一个文件 32         operateFiles.deleteFile("c:/hongten/Hello.txt"); 33         //删除一个文件夹 34         operateFiles.deleteFolder("c:/hongten"); 35         //复制文件夹 36         operateFiles.copyFolder("c:/hongten", "e:/hongten"); 37         //提取文件的扩展名 38         String expandedName=operateFiles.getExpandedName("c:/hongten/Hello.txt"); 39         System.out.println(expandedName); 40         //提取文件的路径 41         System.out.println(operateFiles.getFilePath("c:/hongten/Hello.txt")); 42     } 43  44     /** 45      * 获得文件的扩展名 46      * @param filePath 文件的路径 如:c:/hongten/Hello.txt 47      * @return 文件的扩展名 如:txt 48 */ 49     public String getExpandedName(String filePath){ 50         return filePath.substring(filePath.lastIndexOf(".")+1); 51     } 52     /** 53      * 获得文件的路径 54      * @param file 文件的路径 55      * @return 文件的路径 56 */ 57     public String getFilePath(String file){ 58         return file.substring(0,file.lastIndexOf("/"));   59     } 60     /** 61      * 新建一个目录 62      *  63      * @param folderPath 64      *            新建目录的路径 如:c:\\newFolder 65 */ 66     public void newFolder(String folderPath) { 67         try { 68             File myFolderPath = new File(folderPath.toString()); 69             if (!myFolderPath.exists()) { 70                 myFolderPath.mkdir(); 71             } 72         } catch (Exception e) { 73             System.out.println("新建目录操作出错"); 74             e.printStackTrace(); 75         } 76     } 77  78     /** 79      * 新建一个文件 80      *  81      * @param filePath 82      *            新建文件的目录 如:c:\\hongten.java 83 */ 84     public void newFile(String filePath) { 85         try { 86             File myFilePathFile = new File(filePath.toString()); 87             if (!myFilePathFile.exists()) { 88                 myFilePathFile.createNewFile(); 89             } 90         } catch (Exception e) { 91             System.out.println("新文件创建失败"); 92             e.printStackTrace(); 93         } 94     } 95  96     /** 97      * 新建一个文件,同时向文件中写入内容 98      *  99      * @param filePath100      *            新建文件的目录 如:c:\\hongten.java101      * @param fileContent102      *            向文件中写入的内容103 */104     public void newFile(String filePath, String fileContent) {105         try {106             newFile(filePath);107             FileWriter resultFile = new FileWriter(filePath);108             PrintWriter myFile = new PrintWriter(resultFile);109             myFile.println(fileContent);110             resultFile.close();111         } catch (Exception e) {112             System.out.println("新建文件操作出错");113             e.printStackTrace();114         }115     }116 117     /**118      * 删除一个文件119      * 120      * @param filePath121      *            要删除文件的绝对路径 如:c:\\hongten\\Hello.txt122 */123     public void deleteFile(String filePath) {124         try {125             File preDelFile = new File(filePath);126             if (preDelFile.exists()) {127                 preDelFile.delete();128             } else {129                 System.out.println(filePath + "不存在!");130             }131         } catch (Exception e) {132             System.out.println("删除文件操作出错");133             e.printStackTrace();134         }135     }136 137     /**138      * 删除一个文件夹139      * 140      * @param folderPath141      *            要删除的文件夹的绝对路径 如:c:\\hongten142 */143     public void deleteFolder(String folderPath) {144         try {145             delAllFiles(folderPath);146             File preDelFoder = new File(folderPath);147             if (preDelFoder.exists()) {148                 preDelFoder.delete();149             } else {150                 System.out.println(folderPath + "不存在!");151             }152         } catch (Exception e) {153             System.out.println("删除文件操作出错");154             e.printStackTrace();155         }156     }157 158     /**159      * 删除一个文件夹下的所有文件160      * 161      * @param folderPath162      *            要删除的文件夹的绝对路径 如:c:\\hongten163 */164     public void delAllFiles(String folderPath) {165         File file = new File(folderPath);166         if (!file.exists()) {167             return;168         }169         if (!file.isDirectory()) {170             return;171         }172         String[] tempList = file.list();173         File temp = null;174         for (int i = 0; i < tempList.length; i++) {175             if (folderPath.endsWith(File.separator)) {176                 temp = new File(folderPath + tempList[i]);177             } else {178                 temp = new File(folderPath + File.separator + tempList[i]);179             }180             if (temp.isFile()) {181                 temp.delete();182             }183             if (temp.isDirectory()) {184                 delAllFiles(folderPath + "/" + tempList[i]);// 先删除文件夹里面的文件185                 deleteFolder(folderPath + "/" + tempList[i]);// 再删除空文件夹186             }187         }188     }189 190     /**191      * 单个文件的复制192      * 193      * @param oldPath194      *            原路径 如:c:\\Hello.java195      * @param newPath196      *            新路径 如:f:\\Hello.java197 */198     public void copyFile(String oldPath, String newPath) {199         try {200             int bytesum = 0;201             int byteread = 0;202             File oldfile = new File(oldPath);203             if (oldfile.exists()) { // 文件存在时204                 InputStream inStream = new FileInputStream(oldPath); // 读入原文件205                 FileOutputStream fs = new FileOutputStream(newPath);206                 byte[] buffer = new byte[1444];207                 // int length = 0;208                 while ((byteread = inStream.read(buffer)) != -1) {209                     bytesum += byteread; // 字节数 文件大小210                     fs.write(buffer, 0, byteread);211                 }212                 inStream.close();213             }214         } catch (Exception e) {215             System.out.println("复制单个文件操作出错");216             e.printStackTrace();217 218         }219     }220 221     /**222      * 文件夹的复制223      * 224      * @param oldPath225      *            原文件夹路径 如: c:\\hello226      * @param newPath227      *            新文件夹路径 如: e:\\hello228 */229     public void copyFolder(String oldPath, String newPath) {230 231         try {232             (new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹233             File a = new File(oldPath);234             String[] file = a.list();235             File temp = null;236             for (int i = 0; i < file.length; i++) {237                 if (oldPath.endsWith(File.separator)) {238                     temp = new File(oldPath + file[i]);239                 } else {240                     temp = new File(oldPath + File.separator + file[i]);241                 }242 243                 if (temp.isFile()) {244                     FileInputStream input = new FileInputStream(temp);245                     FileOutputStream output = new FileOutputStream(newPath246                             + "/" + (temp.getName()).toString());247                     byte[] b = new byte[1024 * 5];248                     int len;249                     while ((len = input.read(b)) != -1) {250                         output.write(b, 0, len);251                     }252                     output.flush();253                     output.close();254                     input.close();255                 }256                 if (temp.isDirectory()) {// 如果是子文件夹257                     copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);258                 }259             }260         } catch (Exception e) {261             System.out.println("复制整个文件夹内容操作出错");262             e.printStackTrace();263 264         }265     }266 267     /**268      * 移动单个文件269      * 270      * @param oldPath271      *            源文件路径 如:c:\\hello.java272      * @param newPath273      *            新文件路径 如:e:\\hello.java274 */275     public void moveFile(String oldPath, String newPath) {276         copyFile(oldPath, newPath);277         deleteFile(oldPath);278     }279 280     /**281      * 移动文件夹282      * 283      * @param oldPath284      *            原文件夹路径 如:c:\\hongten285      * @param newPath286      *            新文件夹路径 如:e:\\hongten287 */288     public void moveFolder(String oldPath, String newPath) {289         copyFolder(oldPath, newPath);290         deleteFolder(oldPath);291     }292 293     /**294      * 获得系统根目录绝对路径295      * 296      * @return297 */298     public String getPath() {299         String sysPath = this.getClass().getResource("/").getPath();300         // 对路径进行修改301         sysPath = sysPath.substring(1, sysPath.length() - 16);302         return sysPath;303     }304 305 }复制代码
复制代码
现在有时间把这些东西整理出来,给大家分享一下……

论坛徽章:
0
2 [报告]
发表于 2012-03-03 11:48 |只看该作者
谢谢分享

论坛徽章:
0
3 [报告]
发表于 2012-03-06 10:37 |只看该作者
    哥们儿们有没有和我一样想学习JAVA的,最近我在私塾在线上看了很多经典的、很不错的关于JAVA的视频,如果遇到看不明白的地方,可以在他们论坛上讨论,他们的老师会上去解答。前几天我看到他们在搞个新的活动,”远程学习+地面冲刺=高薪就业“,远程学习一共252个小时,价格确实很诱人,单人报名是1200,团报是1000,虽说团报价格差不了多少,但是自己学太枯燥,想找朋友们一起学习,这样不仅可以相互学习,还可以互相交流交流嘛。你们也可以先去JAVA私塾上面了解了解,然后去看看他们的视频,觉得可以的话可以加我一起团报,我的QQ:1477861213
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP