本文主要记录了使用java创建目录,复制目录,遍历目录及文件,删除目录等。
Java comes with two create directory methods :
1. File(‘directory’).mkdir()new File("C:\\Directory1").mkdir();
Create a directory named “Directory1″
2. File(‘directories’).mkdirs()new File("C:\\Directory2\\Sub2\\Sub-Sub2").mkdirs()
Create a directory named “Directory2 and all its sub-directories “Sub2″ and “Sub-Sub2″ in one shot.
To delete a directory, you can simply use the
File.delete(), but the directory must be empty in order to delete it.
Often times, you may require to perform
recursive delete in a directory, which means all it’s sub-directories and files should be delete as well.
完整代码如下:
- package org.hnrsc.directory;
-
-
import java.io.File;
-
import java.io.FileInputStream;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
import java.io.InputStream;
-
import java.io.OutputStream;
-
-
public class JavaDirectory {
-
-
public static void main(String[] args){
-
System.out.println("------------create directory--------------");
-
//create directory
-
File file = new File("C:\\folder1");
-
if(file.mkdir()){
-
System.out.println("Directory is created.");
-
}else{
-
System.out.println("Failed to create directory.");
-
}
-
-
//create directories
-
File files = new File("C:\\folder2\\folder2-1\\folder2-3");
-
if(files.mkdirs()){
-
System.out.println("Multiple directories are created");
-
}else{
-
System.out.println("Failed to create multiple directories.");
-
}
-
-
System.out.println("-----------copy folder---------------");
-
File srcFolder = new File("C:\\logs");
-
File destFolder = new File("C:\\logs-bak");
-
-
//make sure source folder exists
-
if(!srcFolder.exists()){
-
System.out.println("Directory does not exists.");
-
System.exit(0);
-
}else{
-
try{
-
copyFolder(srcFolder,destFolder);
-
}catch(IOException e){
-
e.printStackTrace();
-
System.exit(0);
-
}
-
}
-
System.out.println("-----------traverse directory---------------");
-
traverseDir(new File("C:\\logs-bak"));
-
System.out.println("-----------delete node--------------");
-
deleteNode(new File("C:\\logs-bak"));
-
}
-
-
private static void copyFolder(File src, File dest) throws IOException {
-
-
if(src.isDirectory()){
-
//if diirectory not exists ,create it
-
if(!dest.exists()){
-
dest.mkdir();
-
System.out.println("Directory copied from " + src +" to " + dest);
-
}
-
//list all the directory contents
-
String[] files = src.list();
-
-
for(String file :files){
-
//construct the src and dest file structure
-
File srcFile = new File(src,file);
-
File destFile = new File(dest,file);
-
-
copyFolder(srcFile,destFile);
-
}
-
}else{
-
//if file, then copy it use bytes stream to support all file types
-
InputStream in = new FileInputStream(src);
-
OutputStream out = new FileOutputStream(dest);
-
-
byte[] buffer = new byte[1024];
-
int len;
-
while((len = in.read(buffer)) > 0){
-
out.write(buffer,0,len);
-
}
-
in.close();
-
out.close();
-
}
-
}
-
-
public static void traverseDir(File node){
-
System.out.println(node.getAbsoluteFile());
-
-
if(node.isDirectory()){
-
String[] subnode = node.list();
-
for(String filename : subnode){
-
traverseDir(new File(node,filename));
-
}
-
}
-
}
-
-
public static void deleteNode(File node){
-
-
if(node.isDirectory()){
-
-
//directory is empty, then delete it
-
if(node.list().length == 0){
-
node.delete();
-
System.out.println("Directory is deleted: "+ node.getAbsolutePath());
-
}else{
-
//list all the directory contents
-
String files[] = node.list();
-
for(String temp:files){
-
//construct the file structure
-
File fileDelete = new File(node,temp);
-
-
//recursive delete
-
deleteNode(fileDelete);
-
}
-
-
//check the dir again , if empty then delete
-
if(node.list().length == 0){
-
node.delete();
-
System.out.println("Directory is deleted: "+ node.getAbsolutePath());
-
}
-
}
-
}else{
-
//if file then delete it
-
node.delete();
-
System.out.println("File is deleted : " + node.getAbsolutePath());
-
}
-
}
-
}
阅读(2799) | 评论(0) | 转发(0) |