Java文件处理组件

警告
本文最后更新于 2022-06-24,文中内容可能已过时。
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import com.ultrapower.eoms.common.RecordLog;

/**
* 文件处理组件
* @version Apr 21, 2010 5:50:00 PM
*/
public class FileOperUtil {
     /**
     * 删除目录下的所有文件
     * @param path 文件夹路径
     * @exception Exception
     */
     public static void deleteFile(String path) throws Exception {
          File file = new File(path);
          if (file.isDirectory()) {
               File[] files = file.listFiles();
               int len = files.length ;
               for (int i = 0; i < len; i++) {
                    deleteFile(files[i].getAbsolutePath());
               }
          }
          file.delete();
     }

     /**
     * 新建目录
     * @param folderPath 文件夹路径 例如:c:/test
     */
     public static void newFolder(String folderPath) {
          try {
               String filePath = folderPath;
               filePath = filePath.toString();
               java.io.File myFilePath = new java.io.File(filePath);
               if (!myFilePath.exists()) {
                    myFilePath.mkdir();
               }
          } catch (Exception e) {
               RecordLog.printLog("新建目录,"+folderPath+ ",出错,"+e.getMessage(), RecordLog.LOG_LEVEL_ERROR);
               e.printStackTrace();
          }
     }

     /**
     * 新建文件
     * @param filePathAndName 文件路径 例如:c:/test/a.txt
     * @param fileContent 文件的内容
     * @throws IOException
     */
     public static void newFile(String filePathAndName, String fileContent) throws IOException {
          String filePath = filePathAndName;
          filePath = filePath.toString();
          File myFilePath = new File(filePath);
          if (!myFilePath.exists()) {
               myFilePath.createNewFile();
          }
          FileWriter resultFile = new FileWriter(myFilePath);
          PrintWriter myFile = new PrintWriter(resultFile);
          String strContent = fileContent;
          myFile.println(strContent);
          resultFile.close();
     }

     /**
     * 删除文件
     *
     * @param filePathAndName
     *            文件路径 例如:c:/test/a.txt
     */
     public static void delFile(String filePathAndName) {
          try {
               String filePath = filePathAndName;
               filePath = filePath.toString();
               java.io.File myDelFile = new java.io.File(filePath);
               myDelFile.delete();
          } catch (Exception e) {
               RecordLog.printLog("删除文件,"+filePathAndName+ ",出错,"+e.getMessage(), RecordLog.LOG_LEVEL_ERROR);
               e.printStackTrace();
          }
     }


     /**
     * 删除文件夹
     * @param folderPath 文件夹路径 例如:c:/test
     * @return 如果删除成功,则返回 true;否则返回 false。
     */
     public static boolean delFolder(String folderPath) {
          boolean flag = false;
          try {
               delAllFile(folderPath); //删除完里面所有内容
               String filePath = folderPath;
               filePath = filePath.toString();
               java.io.File myFilePath = new java.io.File(filePath);
               flag = myFilePath.delete(); //删除空文件夹
          } catch (Exception e) {
               RecordLog.printLog("删除文件夹,"+folderPath+",出错," +e.getMessage(), RecordLog .LOG_LEVEL_ERROR);
               e.printStackTrace();
          }
          return flag;
     }

     /**
     * 删除文件夹里面的所有文件
     * @param path 文件夹路径 例如:c:/test
     */
     public static void delAllFile(String path) {
          File file = new File(path);
          if (!file.exists()) {
               return;
          }
          if (!file.isDirectory()) {
               return;
          }
          String[] tempList = file.list();
          File temp = null;
          for (int i = 0; i < tempList.length; i++) {
               if (path.endsWith(File.separator)) {
                    temp = new File(path + tempList[i]);
               } else {
                    temp = new File(path + File.separator + tempList[i]);
               }
               if (temp.isFile()) {
                    temp.delete();
               }
               if (temp.isDirectory()) {
                    delAllFile(path + "/" + tempList[i]); // 先删除文件夹里面的文件
                    delFolder(path + "/" + tempList[i]); // 再删除空文件夹
               }
          }
     }

     /**
     * 复制单个文件
     * @param oldPath 文件夹路径 例如:c:/test/a.txt
     * @param newPath 文件夹路径 例如:c:/test/a.txt
     * @throws IOException
     */
     public static void copyFile(String oldPath, String newPath) throws IOException {
               int bytesum = 0;
               int byteread = 0;
               File oldfile = new File(oldPath);
               if (oldfile.exists()) { // 文件存在时
                    InputStream inStream = new FileInputStream(oldPath); // 读入原文件
                    FileOutputStream fs = new FileOutputStream(newPath);
                    byte[] buffer = new byte[1444];
                    while ((byteread = inStream.read(buffer)) != -1) {
                         bytesum += byteread; // 字节数 文件大小
                         System. out.println(bytesum);
                         fs.write(buffer, 0, byteread);
                    }
                    inStream.close();
               }
     }

     /**
     * 复制整个文件夹内容
     * @param oldPath 文件夹路径 例如:c:/test
     * @param newPath 文件夹路径 例如:c:/abc
     */
     public static void copyFolder(String oldPath, String newPath) {
          try {
               ( new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
               File a = new File(oldPath);
               String[] file = a.list();
               File temp = null;
               for (int i = 0; i < file.length; i++) {
                    if (oldPath.endsWith(File.separator)) {
                         temp = new File(oldPath + file[i]);
                    } else {
                         temp = new File(oldPath + File.separator + file[i]);
                    }
                    if (temp.isFile()) {
                         FileInputStream input = new FileInputStream(temp);
                         FileOutputStream output = new FileOutputStream(newPath
                                   + "/" + (temp.getName()).toString());
                         byte[] b = new byte[1024 * 5];
                         int len;
                         while ((len = input.read(b)) != -1) {
                              output.write(b, 0, len);
                         }
                         output.flush();
                         output.close();
                         input.close();
                    }
                    if (temp.isDirectory()) {// 如果是子文件夹
                         copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
                    }
               }
          } catch (Exception e) {
               RecordLog.printLog("复制整个文件夹内容操作出错," +e.getMessage(), RecordLog .LOG_LEVEL_ERROR);
               e.printStackTrace();
          }
     }


     /**
     * 移动文件到指定目录
     * @param oldPath 文件夹路径 例如:c:/test/a.txt
     * @param newPath 文件夹路径 例如:c:/abc/a.txt
     * @throws IOException
     */
     public static void moveFile(String oldPath, String newPath) throws IOException {
          copyFile(oldPath, newPath);
          delFile(oldPath);

     }

     /**
     * 移动文件到指定目录
     * @param oldPath String 如:c:/test
     * @param newPath String 如:d:/abc
     */
     public static void moveFolder(String oldPath, String newPath) {
          copyFolder(oldPath, newPath);
          delFolder(oldPath);
     }
   
     public static void main(String[] args){
          rename("d:/aaa", "av.xml","ne.xml" );
     }
   
     public static boolean rename(String filepath,String srcname,String destname){
          boolean flag = false;
          File f = new File(filepath+File.separator+srcname);
          String c = f.getParent();
          File mm = new File(c + File.separator + destname);
          if (f.renameTo(mm))
               flag = true;
          return flag;
     }
}
0%