JAva fileファイル操作operate file of java

34259 ワード

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:\
ewFolder
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 }


今はこれらのものを整理して、みんなに分かち合う時間があります......