public void extract(destFileName,mExtractToDir )
 {
String fileName = new String();
String destFileName = new String();
//do our own buffering; reuse the same
buffer byte[] buffer = new byte[16384];
try
 {
ZipFile archive = new ZipFile( mZipFile );
for ( Enumeration e = archive.entries(); e.hasMoreElements(); )
 {
//get the next entry in the archive
ZipEntry entry = (ZipEntry)e.nextElement();
if ( ! entry.isDirectory() )
 {
fileName = entry.getName();
fileName = fileName.replace('/', File.separatorChar);
destFileName = mExtractToDir + fileName;
File destFile = new File(destFileName);
//create the destination path, if needed
String parent = destFile.getParent();
if ( parent != null )
 {
File parentFile = new File(parent);
if ( ! parentFile.exists() )
 {
//create the chain of subdirs to the file
parentFile.mkdirs();
}
}
//get a stream of the archive entry's bytes
InputStream in = archive.getInputStream(entry);
//open a stream to the destination
file OutputStream out = new FileOutputStream(destFileName);
//Repeat reading into buffer and writing buffer to file,
//until done. Count will always be # bytes read, until //EOF when it is -1.
int count;
while ( (count = in.read(buffer)) != -1 )
 {
out.write(buffer, 0, count );
} in.close(); out.close();
}
}
}
catch( ZipException ze )
 {
ze.printStackTrace();
}
catch ( NullPointerException npe )
 {
npe.printStackTrace();
}
catch ( IOException ioe )
 {
ioe.printStackTrace();
}
catch ( SecurityException se )
 {
se.printStackTrace();
}
} 将某一文件夹的内容清空:
public void cleanImportDirectory(String iPath)
 {
try
 {
File theFile = new File(iPath);
File allFiles[] = theFile.listFiles();
for (int i = 0; i < allFiles.length; i++)
 {
if(allFiles[i].isDirectory())
 {
cleanImportDirectory(allFiles[i].toString());
allFiles[i].delete();
}
else
 {
allFiles[i].delete();
}
}
}
catch (NullPointerException npe)
 {
mLogger.severe(iPath + " did not exist and was not cleaned!!");
}
} 将某一文件夹的内容移到指定文件夹:
private void copyCourse(String iInFilePath, String iOutFilePath) throws IOException
 {
try
 {
String inDirName = iInFilePath;
inDirName.replace('/', java.io.File.separatorChar);
File tempFile = new File(inDirName);
File[] fileNames = tempFile.listFiles();
String outDirName = iOutFilePath;
outDirName = outDirName.replace('/', java.io.File.separatorChar);
File tempDir = new File(outDirName);
tempDir.mkdirs();
for (int i = 0; i < fileNames.length; i++)
 {
String tempString = outDirName + java.io.File.separatorChar + fileNames[i].getName();
if(fileNames[i].isDirectory())
 {
File dirToCreate = new File(tempString);
dirToCreate.mkdirs();
copyCourse(fileNames[i].getAbsolutePath(), tempString);
}
else
 {
BufferedInputStream in = new BufferedInputStream(new FileInputStream(fileNames[i]));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tempString));
int c;
while((c = in.read()) != -1)
 {
out.write(c);
}
in.close();
out.close();
}
}
}
catch (IOException IOE)
 {
IOE.printStackTrace();
}
 
 2 /** *//**
3 * //FileOperate.java
4 * 文件的各种操作
5 * 杨彩 http://blog.sina.com.cn/m/yangcai
6 * 文件操作 1.0
7 */
8
9 //package common;
10
11 import java.io.*;
12
13 public class FileOperate
 14 {
15 static boolean exitnow=false;
16 static String aa,bb;
 17 public FileOperate() {
18 }
19
 20 /** *//**
21 * 新建目录
22 */
 23 public void newFolder(String folderPath) {
24 try
 25 {
26 String filePath = folderPath;
27 filePath = filePath.toString();
28 File myFilePath = new File(filePath);
29 if(!myFilePath.exists())
 30 {
31 myFilePath.mkdir();
32 }
33 System.out.println("新建目录操作 成功执行");
34 }
35 catch(Exception e)
 36 {
37 System.out.println("新建目录操作出错");
38 e.printStackTrace();
39 }
40 }
41
 42 /** *//**
43 * 新建文件
44 */
45 public void newFile(String filePathAndName, String fileContent)
 46 {
47
48 try
 49 {
50 String filePath = filePathAndName;
51 filePath = filePath.toString();
52 File myFilePath = new File(filePath);
53 if (!myFilePath.exists())
 54 {
55 myFilePath.createNewFile();
56 }
57 FileWriter resultFile = new FileWriter(myFilePath);
58 PrintWriter myFile = new PrintWriter(resultFile);
59 String strContent = fileContent;
60 myFile.println(strContent);
61 resultFile.close();
62 System.out.println("新建文件操作 成功执行");
63 }
 64 catch (Exception e) {
65 System.out.println("新建目录操作出错");
66 e.printStackTrace();
67
68 }
69
70 }
71
 72 /** *//**
73 * 删除文件
74 */
 75 public void delFile(String filePathAndName) {
 76 try {
77 String filePath = filePathAndName;
78 filePath = filePath.toString();
79 File myDelFile = new File(filePath);
80 myDelFile.delete();
81 System.out.println("删除文件操作 成功执行");
82 }
 83 catch (Exception e) {
84 System.out.println("删除文件操作出错");
85 e.printStackTrace();
86
87 }
88
89 }
90
 91 /** *//**
92 * 删除文件夹
93 */
94 public void delFolder(String folderPath)
 95 {
96 try
 97 {
98 delAllFile(folderPath); //删除完里面所有内容
99 String filePath = folderPath;
100 filePath = filePath.toString();
101 File myFilePath = new File(filePath);
102 myFilePath.delete(); //删除空文件夹
103 System.out.println("删除文件夹操作 成功执行");
104 }
105 catch (Exception e)
 106 {
107 System.out.println("删除文件夹操作出错");
108 e.printStackTrace();
109
110 }
111
112 }
113
 114 /** *//**
115 * 删除文件夹里面的所有文件
116 * @param path String 文件夹路径 如 c:/fqf
117 */
118 public void delAllFile(String path)
 119 {
120 File file = new File(path);
121 if(!file.exists())
 122 {
123 return;
124 }
125 if(!file.isDirectory())
 126 {
127 return;
128 }
129 String[] tempList = file.list();
130 File temp = null;
131 for (int i = 0; i < tempList.length; i++)
 132 {
133 if(path.endsWith(File.separator))
 134 {
135 temp = new File(path + tempList[i]);
136 }
137 else
 138 {
139 temp = new File(path + File.separator + tempList[i]);
140 }
141 if (temp.isFile())
 142 {
143 temp.delete();
144 }
145 if (temp.isDirectory())
 146 {
147 delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件
148 delFolder(path+"/"+ tempList[i]);//再删除空文件夹
149 }
150 }
151 System.out.println("删除文件操作 成功执行");
152 }
153
 154 /** *//**
155 * 复制单个文件
156 * @param oldPath String 原文件路径 如:c:/fqf.txt
157 * @param newPath String 复制后路径 如:f:/fqf.txt
158 */
 159 public void copyFile(String oldPath, String newPath) {
 160 try {
161 int bytesum = 0;
162 int byteread = 0;
163 File oldfile = new File(oldPath);
164 if (oldfile.exists())
 165 { //文件存在时
166 InputStream inStream = new FileInputStream(oldPath); //读入原文件
167 FileOutputStream fs = new FileOutputStream(newPath);
168 byte[] buffer = new byte[1444];
169 int length;
 170 while ( (byteread = inStream.read(buffer)) != -1) {
171 bytesum += byteread; //字节数 文件大小
172 System.out.println(bytesum);
173 fs.write(buffer, 0, byteread);
174 }
175 inStream.close();
176 }
177 System.out.println("删除文件夹操作 成功执行");
178 }
 179 catch (Exception e) {
180 System.out.println("复制单个文件操作出错");
181 e.printStackTrace();
182
183 }
184
185 }
186
 187 /** *//**
188 * 复制整个文件夹内容
189 * @param oldPath String 原文件路径 如:c:/fqf
190 * @param newPath String 复制后路径 如:f:/fqf/ff
191 */
 192 public void copyFolder(String oldPath, String newPath) {
193
194 try
 195 {
196 (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹
197 File a=new File(oldPath);
198 String[] file=a.list();
199 File temp=null;
200 for (int i = 0; i < file.length; i++)
 201 {
202 if(oldPath.endsWith(File.separator))
 203 {
204 temp=new File(oldPath+file[i]);
205 }
 206 else {
207 temp=new File(oldPath+File.separator+file[i]);
208 }
209
210 if(temp.isFile())
 211 {
212 FileInputStream input = new FileInputStream(temp);
213 FileOutputStream output = new FileOutputStream(newPath + "/" +
214 (temp.getName()).toString());
215 byte[] b = new byte[1024 * 5];
216 int len;
217 while ( (len = input.read(b)) != -1)
 218 {
219 output.write(b, 0, len);
220 }
221 output.flush();
222 output.close();
223 input.close();
224 }
225 if(temp.isDirectory())
 226 {//如果是子文件夹
227 copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);
228 }
229 }
230 System.out.println("复制文件夹操作 成功执行");
231 }
 232 catch (Exception e) {
233 System.out.println("复制整个文件夹内容操作出错");
234 e.printStackTrace();
235
236 }
237
238 }
239
 240 /** *//**
241 * 移动文件到指定目录
242 * @param oldPath String 如:c:/fqf.txt
243 * @param newPath String 如:d:/fqf.txt
244 */
 245 public void moveFile(String oldPath, String newPath) {
246 copyFile(oldPath, newPath);
247 delFile(oldPath);
248
249 }
250
 251 /** *//**
252 * 移动文件到指定目录
253 * @param oldPath String 如:c:/fqf.txt
254 * @param newPath String 如:d:/fqf.txt
255 */
 256 public void moveFolder(String oldPath, String newPath) {
257 copyFolder(oldPath, newPath);
258 delFolder(oldPath);
259
260 }
261
262 public static void main(String args[])
 263 {
264 System.out.println("使用此功能请按[1] 功能一:新建目录");
265 System.out.println("使用此功能请按[2] 功能二:新建文件");
266 System.out.println("使用此功能请按[3] 功能三:删除文件");
267 System.out.println("使用此功能请按[4] 功能四:删除文件夹");
268 System.out.println("使用此功能请按[5] 功能五:删除文件夹里面的所有文件");
269 System.out.println("使用此功能请按[6] 功能六:复制文件");
270 System.out.println("使用此功能请按[7] 功能七:复制文件夹的所有内容");
271 System.out.println("使用此功能请按[8] 功能八:移动文件到指定目录");
272 System.out.println("使用此功能请按[9] 功能九:移动文件夹到指定目录");
273 System.out.println("使用此功能请按[10] 退出程序");
274
275 while(!exitnow)
 276 {
277 FileOperate fo=new FileOperate();
278 try
 279 {
280 BufferedReader Bin=new BufferedReader(new InputStreamReader(System.in));
281 String a=Bin.readLine();
282 int b=Integer.parseInt(a);
283
284 switch(b)
 285 {
286 case 1:System.out.println("你选择了功能一 请输入目录名");
287 aa=Bin.readLine();
288 fo.newFolder(aa);
289 break;
290 case 2:System.out.println("你选择了功能二 请输入文件名");
291 aa=Bin.readLine();
292 System.out.println("请输入在"+aa+"中的内容");
293 bb=Bin.readLine();
294 fo.newFile(aa,bb);
295 break;
296 case 3:System.out.println("你选择了功能三 请输入文件名");
297 aa=Bin.readLine();
298 fo.delFile(aa);
299 break;
300 case 4:System.out.println("你选择了功能四 请输入文件名");
301 aa=Bin.readLine();
302 fo.delFolder(aa);
303 break;
304 case 5:System.out.println("你选择了功能五 请输入文件名");
305 aa=Bin.readLine();
306 fo.delAllFile(aa);
307 break;
308 case 6:System.out.println("你选择了功能六 请输入文件名");
309 aa=Bin.readLine();
310 System.out.println("请输入目标文件名");
311 bb=Bin.readLine();
312 fo.copyFile(aa,bb);
313 break;
314 case 7:System.out.println("你选择了功能七 请输入源文件名");
315 aa=Bin.readLine();
316 System.out.println("请输入目标文件名");
317 bb=Bin.readLine();
318 fo.copyFolder(aa,bb);
319 break;
320 case 8:System.out.println("你选择了功能八 请输入源文件名");
321 aa=Bin.readLine();
322 System.out.println("请输入目标文件名");
323 bb=Bin.readLine();
324 fo.moveFile(aa,bb);
325 break;
326 case 9:System.out.println("你选择了功能九 请输入源文件名");
327 aa=Bin.readLine();
328 System.out.println("请输入目标文件名");
329 bb=Bin.readLine();
330 fo.moveFolder(aa,bb);
331 break;
332 case 10:exitnow=true;
333 System.out.println("程序结束,请退出");
334 break;
335 default:System.out.println("输入错误.请输入1-10之间的数");
336 }
337
338
339 System.out.println("请重新选择功能");
340
341
342 }
343 catch(Exception e)
 344 {
345 System.out.println("输入错误字符或程序出错");
346 }
347
348 }
349 }
350 }
|
|
|
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
23 | 24 | 25 | 26 | 27 | 28 | 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 | 1 | 2 | 3 | 4 | 5 |
|
导航
统计
- 随笔: 32
- 文章: 427
- 评论: 144
- 引用: 0
常用链接
留言簿(5)
随笔档案
文章分类
文章档案
java
工具
朋友
搜索
积分与排名
最新评论

阅读排行榜
评论排行榜
|
|