java文件操作类(新建 复制 移动 删除文件和文件夹 获取扩展名)

  1 import java.io.File;
  2 import java.io.FileInputStream;
  3 import java.io.FileOutputStream;
  4 import java.io.FileWriter;
  5 import java.io.InputStream;
  6 import java.io.PrintWriter;
  7  
  8 /**
  9  * 
 10  * @author
 11  * 
 12  *         文件操作类
 13  * 
 14  *         (新建文件夹、删除文件夹、复制文件夹、移动文件夹、新建文件、删除文件、复制文件、移动文件、获取文件扩展名、获取文件路径)
 15  * 
 16  */
 17 public class OperateFiles {
 18  
 19     /**
 20      * @param args
 21      */
 22     public static void main(String[] args) {
 23         OperateFiles operateFiles = new OperateFiles();
 24         // 新建一个文件夹
 25         operateFiles.newFolder("c:/hongten");
 26         // 新建一个文件,同时向里面写入内容
 27         operateFiles.newFile("c:/hongten/Hello.txt", "hello,i'm Hongten.你好");
 28         // 删除一个文件
 29         operateFiles.deleteFile("c:/hongten/Hello.txt");
 30         // 删除一个文件夹
 31         operateFiles.deleteFolder("c:/hongten");
 32         // 复制文件夹
 33         operateFiles.copyFolder("c:/hongten", "e:/hongten");
 34         // 提取文件的扩展名
 35         String expandedName = operateFiles
 36                 .getExpandedName("c:/hongten/Hello.txt");
 37         System.out.println(expandedName);
 38         // 提取文件的路径
 39         System.out.println(operateFiles.getFilePath("c:/hongten/Hello.txt"));
 40     }
 41  
 42     /**
 43      * 获得文件的扩展名
 44      * 
 45      * @param filePath
 46      *            文件的路径 如: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      * 获得文件的路径
 55      * 
 56      * @param file
 57      *            文件的路径
 58      * @return 文件的路径
 59      */
 60     public String getFilePath(String file) {
 61         return file.substring(0, file.lastIndexOf("/"));
 62     }
 63  
 64     /**
 65      * 新建一个目录
 66      * 
 67      * @param folderPath
 68      *            新建目录的路径 如:c:\\newFolder
 69      */
 70     public void newFolder(String folderPath) {
 71         try {
 72             File myFolderPath = new File(folderPath.toString());
 73             if (!myFolderPath.exists()) {
 74                 myFolderPath.mkdir();
 75             }
 76         } catch (Exception e) {
 77             System.out.println("新建目录操作出错");
 78             e.printStackTrace();
 79         }
 80     }
 81  
 82     /**
 83      * 新建一个文件
 84      * 
 85      * @param filePath
 86      *            新建文件的目录 如:c:\\hongten.java
 87      */
 88     public void newFile(String filePath) {
 89         try {
 90             File myFilePathFile = new File(filePath.toString());
 91             if (!myFilePathFile.exists()) {
 92                 myFilePathFile.createNewFile();
 93             }
 94         } catch (Exception e) {
 95             System.out.println("新文件创建失败");
 96             e.printStackTrace();
 97         }
 98     }
 99  
100     /**
101      * 新建一个文件,同时向文件中写入内容
102      * 
103      * @param filePath
104      *            新建文件的目录 如:c:\\hongten.java
105      * @param fileContent
106      *            向文件中写入的内容
107      */
108     public void newFile(String filePath, String fileContent) {
109         try {
110             newFile(filePath);
111             FileWriter resultFile = new FileWriter(filePath);
112             PrintWriter myFile = new PrintWriter(resultFile);
113             myFile.println(fileContent);
114             resultFile.close();
115         } catch (Exception e) {
116             System.out.println("新建文件操作出错");
117             e.printStackTrace();
118         }
119     }
120  
121     /**
122      * 删除一个文件
123      * 
124      * @param filePath
125      *            要删除文件的绝对路径 如:c:\\hongten\\Hello.txt
126      */
127     public void deleteFile(String filePath) {
128         try {
129             File preDelFile = new File(filePath);
130             if (preDelFile.exists()) {
131                 preDelFile.delete();
132             } else {
133                 System.out.println(filePath + "不存在!");
134             }
135         } catch (Exception e) {
136             System.out.println("删除文件操作出错");
137             e.printStackTrace();
138         }
139     }
140  
141     /**
142      * 删除一个文件夹
143      * 
144      * @param folderPath
145      *            要删除的文件夹的绝对路径 如:c:\\hongten
146      */
147     public void deleteFolder(String folderPath) {
148         try {
149             delAllFiles(folderPath);
150             File preDelFoder = new File(folderPath);
151             if (preDelFoder.exists()) {
152                 preDelFoder.delete();
153             } else {
154                 System.out.println(folderPath + "不存在!");
155             }
156         } catch (Exception e) {
157             System.out.println("删除文件操作出错");
158             e.printStackTrace();
159         }
160     }
161  
162     /**
163      * 删除一个文件夹下的所有文件
164      * 
165      * @param folderPath
166      *            要删除的文件夹的绝对路径 如:c:\\hongten
167      */
168     public void delAllFiles(String folderPath) {
169         File file = new File(folderPath);
170         if (!file.exists()) {
171             return;
172         }
173         if (!file.isDirectory()) {
174             return;
175         }
176         String[] tempList = file.list();
177         File temp = null;
178         for (int i = 0; i < tempList.length; i++) {
179             if (folderPath.endsWith(File.separator)) {
180                 temp = new File(folderPath + tempList[i]);
181             } else {
182                 temp = new File(folderPath + File.separator + tempList[i]);
183             }
184             if (temp.isFile()) {
185                 temp.delete();
186             }
187             if (temp.isDirectory()) {
188                 delAllFiles(folderPath + "/" + tempList[i]);// 先删除文件夹里面的文件
189                 deleteFolder(folderPath + "/" + tempList[i]);// 再删除空文件夹
190             }
191         }
192     }
193  
194     /**
195      * 单个文件的复制
196      * 
197      * @param oldPath
198      *            原路径 如:c:\\Hello.java
199      * @param newPath
200      *            新路径 如:f:\\Hello.java
201      */
202     public void copyFile(String oldPath, String newPath) {
203         try {
204             int bytesum = 0;
205             int byteread = 0;
206             File oldfile = new File(oldPath);
207             if (oldfile.exists()) { // 文件存在时
208                 InputStream inStream = new FileInputStream(oldPath); // 读入原文件
209                 FileOutputStream fs = new FileOutputStream(newPath);
210                 byte[] buffer = new byte[1444];
211                 // int length = 0;
212                 while ((byteread = inStream.read(buffer)) != -1) {
213                     bytesum += byteread; // 字节数 文件大小
214                     fs.write(buffer, 0, byteread);
215                 }
216                 inStream.close();
217             }
218         } catch (Exception e) {
219             System.out.println("复制单个文件操作出错");
220             e.printStackTrace();
221  
222         }
223     }
224  
225     /**
226      * 文件夹的复制
227      * 
228      * @param oldPath
229      *            原文件夹路径 如: c:\\hello
230      * @param newPath
231      *            新文件夹路径 如: e:\\hello
232      */
233     public void copyFolder(String oldPath, String newPath) {
234  
235         try {
236             (new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
237             File a = new File(oldPath);
238             String[] file = a.list();
239             File temp = null;
240             for (int i = 0; i < file.length; i++) {
241                 if (oldPath.endsWith(File.separator)) {
242                     temp = new File(oldPath + file[i]);
243                 } else {
244                     temp = new File(oldPath + File.separator + file[i]);
245                 }
246  
247                 if (temp.isFile()) {
248                     FileInputStream input = new FileInputStream(temp);
249                     FileOutputStream output = new FileOutputStream(newPath
250                             + "/" + (temp.getName()).toString());
251                     byte[] b = new byte[1024 * 5];
252                     int len;
253                     while ((len = input.read(b)) != -1) {
254                         output.write(b, 0, len);
255                     }
256                     output.flush();
257                     output.close();
258                     input.close();
259                 }
260                 if (temp.isDirectory()) {// 如果是子文件夹
261                     copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
262                 }
263             }
264         } catch (Exception e) {
265             System.out.println("复制整个文件夹内容操作出错");
266             e.printStackTrace();
267  
268         }
269     }
270  
271     /**
272      * 移动单个文件
273      * 
274      * @param oldPath
275      *            源文件路径 如:c:\\hello.java
276      * @param newPath
277      *            新文件路径 如:e:\\hello.java
278      */
279     public void moveFile(String oldPath, String newPath) {
280         copyFile(oldPath, newPath);
281         deleteFile(oldPath);
282     }
283  
284     /**
285      * 移动文件夹
286      * 
287      * @param oldPath
288      *            原文件夹路径 如:c:\\hongten
289      * @param newPath
290      *            新文件夹路径 如:e:\\hongten
291      */
292     public void moveFolder(String oldPath, String newPath) {
293         copyFolder(oldPath, newPath);
294         deleteFolder(oldPath);
295     }
296  
297     /**
298      * 获得系统根目录绝对路径
299      * 
300      * @return
301      */
302     public String getPath() {
303         String sysPath = this.getClass().getResource("/").getPath();
304         // 对路径进行修改
305         sysPath = sysPath.substring(1, sysPath.length() - 16);
306         return sysPath;
307     }
308  
309 }
来自:http://www.open-open.com/code/view/1420037773921
posted @ 2015-04-17 13:48  Eddylon  阅读(394)  评论(0)    收藏  举报