java file 文件操作 operate file of java

java文件操作

  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 filePath
100 * 新建文件的目录 如:c:\\hongten.java
101 * @param fileContent
102 * 向文件中写入的内容
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 filePath
121 * 要删除文件的绝对路径 如:c:\\hongten\\Hello.txt
122 */
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 folderPath
141 * 要删除的文件夹的绝对路径 如:c:\\hongten
142 */
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 folderPath
162 * 要删除的文件夹的绝对路径 如:c:\\hongten
163 */
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 oldPath
194 * 原路径 如:c:\\Hello.java
195 * @param newPath
196 * 新路径 如:f:\\Hello.java
197 */
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 oldPath
225 * 原文件夹路径 如: c:\\hello
226 * @param newPath
227 * 新文件夹路径 如: e:\\hello
228 */
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(newPath
246 + "/" + (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 oldPath
271 * 源文件路径 如:c:\\hello.java
272 * @param newPath
273 * 新文件路径 如:e:\\hello.java
274 */
275 public void moveFile(String oldPath, String newPath) {
276 copyFile(oldPath, newPath);
277 deleteFile(oldPath);
278 }
279
280 /**
281 * 移动文件夹
282 *
283 * @param oldPath
284 * 原文件夹路径 如:c:\\hongten
285 * @param newPath
286 * 新文件夹路径 如:e:\\hongten
287 */
288 public void moveFolder(String oldPath, String newPath) {
289 copyFolder(oldPath, newPath);
290 deleteFolder(oldPath);
291 }
292
293 /**
294 * 获得系统根目录绝对路径
295 *
296 * @return
297 */
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 }

现在有时间把这些东西整理出来,给大家分享一下……

原文地址:https://www.cnblogs.com/hongten/p/java_file.html