首页 > 试题广场 >

编写代码删除某文件目录下的所有子目录和文件,并输出目录的名字

[问答题]
编写代码删除某文件目录下的所有子目录和文件,并输出目录的名字,路径以及文件的内容。
本题目解体思路:(1)查询当前路径下文件夹和文件(2)递归调用文件夹下的子文件夹和文件(3)读出文件里面的内容(4)删除文件接着删除文件夹
/****
* 查询指定目录下的文件和文件夹
* @param args
* @throws IOException
*/
public static void queryFile(String strPath) throws IOException {
File file = new File(strPath);
File[] tempList = file.listFiles();
System.out.println("该目录下对象个数:" + tempList.length);
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
readFile(tempList[i]);
System.out.println("文     件:" + tempList[i]);
}
if (tempList[i].isDirectory()) {
// 如果是文件夹
queryFile(tempList[i].getPath());
System.out.println("文件夹:" + tempList[i]);
}
tempList[i].delete();
}
}

public static void readFile(File f) throws IOException {
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String content = "";// 用来显示文件内容
while ((content = br.readLine()) != null) {
System.out.println(content);
}
}
发表于 2016-01-11 15:24:56 回复(0)
更多回答
public class DelFile{
    public static void main(String[] args){
        String url ="/url/file";//某文件目录
        File dir = new File(url);
        String[] fileList = dir.list();//得到所有的文件和目录名称
        File[] files = dir.listFiles();//得到所有的文件,以方便读取
    for(String fl : fileList){
        if(fl.isDirectory()){
            System.out.println("子目录:" + fl );//输出目录的名字
        fl.delete();
    }
    }
    for(File f : files){
        System.out.println("文件:" + f.getName());
        readFile(f);
        f.delete();
    }
}
public void readFile(Flie f){
    FileReader fr = new FileReader(f);
    BufferedReader bf = new BufferedReader(fr);
    String content = "";//用来显示文件内容
    while((content = br.readLine() ) !=  null){
        System.ount.println(content);
    //此处省略了异常和关闭文件流的处理
    }
}
}
编辑于 2016-01-12 11:39:23 回复(2)
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main{
public static void main(String[] args) throws IOException {
FileOutputStream out;

File fileFirst=new File("d://File");
if(!fileFirst.exists()){
fileFirst.mkdirs();
}

File fileSecond=new File("d://File//FileSecond");
if(!fileSecond.exists()){
fileSecond.mkdirs();
}
File fileSeconds1=new File("d://File//FileSeconds1.txt");
if(!fileSeconds1.exists()){
fileSeconds1.createNewFile();
out=new FileOutputStream(fileSeconds1);
String content1="龙,我也爱你";
byte[] content1b=content1.getBytes();
out.write(content1b);

}
File fileSeconds2=new File("d://File//FileSeconds2.txt");
if(!fileSeconds2.exists()){
//System.out.println(fileSeconds2.createNewFile());
fileSeconds2.createNewFile();
out=new FileOutputStream(fileSeconds2);
String content="我爱你,甜";
byte[] contentbs=content.getBytes();
out.write(contentbs);
}
File fileThird=new File("d://File//FileSecond//FileThird");
if(!fileThird.exists()){
fileThird.mkdir();
}
showDir(fileFirst);



}

private static void showDir(File file) {
// TODO Auto-generated method stub
System.out.println("目录的名字:"+file.getName());
System.out.println("目录的路径:"+file.getAbsolutePath());
File[] files=file.listFiles();
for(File file1:files){
if(file1.isDirectory()){
showDir(file1);

}else{
System.out.println(file1);
try {
BufferedInputStream input=new BufferedInputStream(new FileInputStream(file1));
ByteArrayOutputStream output=new ByteArrayOutputStream();
byte[] content=new byte[1024];
while(input.read(content)!=-1){
output.write(content);
}
System.out.println(output);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
file1.delete();
}

}

}
发表于 2014-11-10 15:32:34 回复(0)