JAVA中使用File类批量重命名文件及java.io.File的常见用法

转自:http://blog.csdn.net/wlwqw/article/details/2037930【带代码进行了整理】

起因:上份工作辞掉了,找新工作,笔试题有一道题按照所给出的格式将重新命名文件名。

当时对io包中的file类操作生疏了,回来后整理了一下。

先简单说下java.io.File的构造方法和常用方法,具体可参考JDK1.6文档。

在线文档:http://download.oracle.com/technetwork/java/javase/6/docs/zh/api/java/io/File.html

构造方法如下:

File(File parent, String child) 
          根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
File(String pathname) 
          通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。
File(String parent, String child) 
          根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。
File(URI uri) 
          通过将给定的 file: URI 转换为一个抽象路径名来创建一个新的 File 实例。

代码如下:

 1 package com.file;
 2 import java.io.File;
 5 
 6 public class ChangeFileName {
 7     public static void main(String args[]) {
 8         
 9         File fl = new File("d://文件夹");  // 这里写上发替换的文件夹路径,注意使用双斜杠
10         String[] files = fl.list();
11         File f = null;
12         String filename = "";
13         
14         for(String file:files){
15          /*
16           * 注意,这里一定要写成File(fl,file)
17           * 如果写成File(file)是行不通的,
18           * 一定要全路径
19           */
20            f = new File(fl,file); 
21            filename = f.getName();
22            //System.out.println(filename);
23            /*
24             * 这里可以反复使用replace替换,
25             * 当然也可以使用正则表达式来替换了
26             */
27            f.renameTo(new File(fl.getAbsolutePath()+"//"+filename.replace("要替换掉的内容", "替换成的内容")));
28         }
29  }
30 }

另外讲述下java.io.File的一些常见用法。

  1 import java.io.*;
  2 
  3 public class FileOperate {
  4   public FileOperate() {
  5   }
  6 
  7   /**
  8    * 新建目录
  9    * @param folderPath String 如 c:/fqf
 10    * @return boolean
 11    */
 12   public void newFolder(String folderPath) {
 13     try {
 14       String filePath = folderPath;
 15       filePath = filePath.toString();
 16       java.io.File myFilePath = new java.io.File(filePath);
 17       if (!myFilePath.exists()) {
 18         myFilePath.mkdir();
 19       }
 20     }
 21     catch (Exception e) {
 22       System.out.println("新建目录操作出错");
 23       e.printStackTrace();
 24     }
 25   }
 26 
 27   /**
 28    * 新建文件
 29    * @param filePathAndName String 文件路径及名称 如c:/fqf.txt
 30    * @param fileContent String 文件内容
 31    * @return boolean
 32    */
 33   public void newFile(String filePathAndName, String fileContent) {
 34 
 35     try {
 36       String filePath = filePathAndName;
 37       filePath = filePath.toString();
 38       File myFilePath = new File(filePath);
 39       if (!myFilePath.exists()) {
 40         myFilePath.createNewFile();
 41       }
 42       FileWriter resultFile = new FileWriter(myFilePath);
 43       PrintWriter myFile = new PrintWriter(resultFile);
 44       String strContent = fileContent;
 45       myFile.println(strContent);
 46       resultFile.close();
 47 
 48     }
 49     catch (Exception e) {
 50       System.out.println("新建目录操作出错");
 51       e.printStackTrace();
 52 
 53     }
 54 
 55   }
 56 
 57   /**
 58    * 删除文件
 59    * @param filePathAndName String 文件路径及名称 如c:/fqf.txt
 60    * @param fileContent String
 61    * @return boolean
 62    */
 63   public void delFile(String filePathAndName) {
 64     try {
 65       String filePath = filePathAndName;
 66       filePath = filePath.toString();
 67       java.io.File myDelFile = new java.io.File(filePath);
 68       myDelFile.delete();
 69 
 70     }
 71     catch (Exception e) {
 72       System.out.println("删除文件操作出错");
 73       e.printStackTrace();
 74 
 75     }
 76 
 77   }
 78 
 79   /**
 80    * 删除文件夹
 81    * @param filePathAndName String 文件夹路径及名称 如c:/fqf
 82    * @param fileContent String
 83    * @return boolean
 84    */
 85   public void delFolder(String folderPath) {
 86     try {
 87       delAllFile(folderPath); //删除完里面所有内容
 88       String filePath = folderPath;
 89       filePath = filePath.toString();
 90       java.io.File myFilePath = new java.io.File(filePath);
 91       myFilePath.delete(); //删除空文件夹
 92 
 93     }
 94     catch (Exception e) {
 95       System.out.println("删除文件夹操作出错");
 96       e.printStackTrace();
 97 
 98     }
 99 
100   }
101 
102   /**
103    * 删除文件夹里面的所有文件
104    * @param path String 文件夹路径 如 c:/fqf
105    */
106   public void delAllFile(String path) {
107     File file = new File(path);
108     if (!file.exists()) {
109       return;
110     }
111     if (!file.isDirectory()) {
112       return;
113     }
114     String[] tempList = file.list();
115     File temp = null;
116     for (int i = 0; i < tempList.length; i++) {
117       if (path.endsWith(File.separator)) {
118         temp = new File(path + tempList[i]);
119       }
120       else {
121         temp = new File(path + File.separator + tempList[i]);
122       }
123       if (temp.isFile()) {
124         temp.delete();
125       }
126       if (temp.isDirectory()) {
127         delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件
128         delFolder(path+"/"+ tempList[i]);//再删除空文件夹
129       }
130     }
131   }
132 
133   /**
134    * 复制单个文件
135    * @param oldPath String 原文件路径 如:c:/fqf.txt
136    * @param newPath String 复制后路径 如:f:/fqf.txt
137    * @return boolean
138    */
139   public void copyFile(String oldPath, String newPath) {
140     try {
141       int bytesum = 0;
142       int byteread = 0;
143       File oldfile = new File(oldPath);
144       if (oldfile.exists()) { //文件存在时
145         InputStream inStream = new FileInputStream(oldPath); //读入原文件
146         FileOutputStream fs = new FileOutputStream(newPath);
147         byte[] buffer = new byte[1444];
148         int length;
149         while ( (byteread = inStream.read(buffer)) != -1) {
150           bytesum += byteread; //字节数 文件大小
151           System.out.println(bytesum);
152           fs.write(buffer, 0, byteread);
153         }
154         inStream.close();
155       }
156     }
157     catch (Exception e) {
158       System.out.println("复制单个文件操作出错");
159       e.printStackTrace();
160 
161     }
162 
163   }
164 
165   /**
166    * 复制整个文件夹内容
167    * @param oldPath String 原文件路径 如:c:/fqf
168    * @param newPath String 复制后路径 如:f:/fqf/ff
169    * @return boolean
170    */
171   public void copyFolder(String oldPath, String newPath) {
172 
173     try {
174       (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹
175       File a=new File(oldPath);
176       String[] file=a.list();
177       File temp=null;
178       for (int i = 0; i < file.length; i++) {
179         if(oldPath.endsWith(File.separator)){
180           temp=new File(oldPath+file[i]);
181         }
182         else{
183           temp=new File(oldPath+File.separator+file[i]);
184         }
185 
186         if(temp.isFile()){
187           FileInputStream input = new FileInputStream(temp);
188           FileOutputStream output = new FileOutputStream(newPath + "/" +
189               (temp.getName()).toString());
190           byte[] b = new byte[1024 * 5];
191           int len;
192           while ( (len = input.read(b)) != -1) {
193             output.write(b, 0, len);
194           }
195           output.flush();
196           output.close();
197           input.close();
198         }
199         if(temp.isDirectory()){//如果是子文件夹
200           copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
201         }
202       }
203     }
204     catch (Exception e) {
205       System.out.println("复制整个文件夹内容操作出错");
206       e.printStackTrace();
207 
208     }
209 
210   }
211 
212   /**
213    * 移动文件到指定目录
214    * @param oldPath String 如:c:/fqf.txt
215    * @param newPath String 如:d:/fqf.txt
216    */
217   public void moveFile(String oldPath, String newPath) {
218     copyFile(oldPath, newPath);
219     delFile(oldPath);
220 
221   }
222 
223   /**
224    * 移动文件到指定目录
225    * @param oldPath String 如:c:/fqf.txt
226    * @param newPath String 如:d:/fqf.txt
227    */
228   public void moveFolder(String oldPath, String newPath) {
229     copyFolder(oldPath, newPath);
230     delFolder(oldPath);
231 
232   }
233 }

 

posted @ 2013-03-14 14:53  古来征战几人回  阅读(5979)  评论(3编辑  收藏  举报