java实现读取、删除文件夹下的文件

 更新时间:2015年05月28日 16:25:26   投稿:hebedich  
本文给大家分享的是java实现读取、删除文件夹下的文件,其中File.delete()用于删除“某个文件或者空目录”!所以要删除某个目录及其中的所有文件和子目录,要进行递归删除,有需要的小伙伴可以参考下。

java实现读取、删除文件夹下的文件

package test.com;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
 
public class ReadFile {
  public ReadFile() {
  }
  /**
   * 读取某个文件夹下的所有文件
   */
  public static boolean readfile(String filepath) throws FileNotFoundException, IOException {
      try {
 
          File file = new File(filepath);
          if (!file.isDirectory()) {
//              System.out.println("文件");
//              System.out.println("path=" + file.getPath());
//              System.out.println("absolutepath=" + file.getAbsolutePath());
              System.out.println(file.getName());
 
          } else if (file.isDirectory()) {
              String[] filelist = file.list();
              for (int i = 0; i < filelist.length; i++) {
                  File readfile = new File(filepath + "\\" + filelist[i]);
                  if (!readfile.isDirectory()) {
//                      System.out.println("path=" + readfile.getPath());
//                      System.out.println("absolutepath="
//                              + readfile.getAbsolutePath());
                      System.out.println(readfile.getName());
 
                  } else if (readfile.isDirectory()) {
                      readfile(filepath + "\\" + filelist[i]);
                  }
              }
 
          }
 
      } catch (FileNotFoundException e) {
          System.out.println("readfile()  Exception:" + e.getMessage());
      }
      return true;
  }
 
  /**
   * 删除某个文件夹下的所有文件夹和文件
   */
   
   
  /*public static boolean deletefile(String delpath)
          throws FileNotFoundException, IOException {
      try {
 
          File file = new File(delpath);
          if (!file.isDirectory()) {
              System.out.println("1");
              file.delete();
          } else if (file.isDirectory()) {
              System.out.println("2");
              String[] filelist = file.list();
              for (int i = 0; i < filelist.length; i++) {
                  File delfile = new File(delpath + "\\" + filelist[i]);
                  if (!delfile.isDirectory()) {
                      System.out.println("path=" + delfile.getPath());
                      System.out.println("absolutepath="
                              + delfile.getAbsolutePath());
                      System.out.println("name=" + delfile.getName());
                      delfile.delete();
                      System.out.println("删除文件成功");
                  } else if (delfile.isDirectory()) {
                      deletefile(delpath + "\\" + filelist[i]);
                  }
              }
              file.delete();
 
          }
 
      } catch (FileNotFoundException e) {
          System.out.println("deletefile()  Exception:" + e.getMessage());
      }
      return true;
  }*/
   
  public static void main(String[] args) {
      try {
          readfile("C:\\Users\\SW\\Desktop\\SKJ_H25補正\\004_RCAG\\003_SKJ");
          // deletefile("D:/file");
      } catch (FileNotFoundException ex) {
      } catch (IOException ex) {
      }
      System.out.println("ok");
  }
 
}

方法二:

package otherstudy;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * @ClassName: TestReadFile
 * @CreateTime: Aug 1, 2014 11:42:44 AM
 * @Author: mayi
 * @Description: 读取和删除文件夹下的所有文件
 *
 */
public class TestReadFile {
	/**
	 * 获取工程的WebRoot的绝对路径
	 * @return
	 */
	String getProjectPath(){
		//得到形如"/d:/${workspace}/${projectName}/WebRoot/WEB-INF/classes/"的 路径
    String path=this.getClass().getResource("/").getPath();
    //从路径字符串中取出工程路径
    path=path.substring(1, path.indexOf("WEB-INF/classes"));
    System.out.println("工程路径:"+path);
    return path;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		TestReadFile trf=new TestReadFile();
		String xmlPath = trf.getProjectPath()+ "testDocs";
		try {
			readAllFile(xmlPath);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 读取指定路径文件夹下的所有文件
	 * @param filepath
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static boolean readAllFile(String filepath)
			throws FileNotFoundException, IOException {
		try {
			File file = new File(filepath);
			if (!file.isDirectory()) {
				System.out.println("\n文件信息:");
				System.out.println("\t相对路径=" + file.getPath());
				System.out.println("\t绝对路径=" + file.getAbsolutePath());
				System.out.println("\t文件全名=" + file.getName());

			} else if (file.isDirectory()) {
				System.out.println("\n文件夹内文件列表信息:");
				File[] fileList = file.listFiles();
				for (int i = 0; i < fileList.length; i++) {
					File readfile = fileList[i];
					if (!readfile.isDirectory()) {
						System.out.println("\n\t相对路径=" + readfile.getPath());
						System.out.println("\t绝对路径=" + readfile.getAbsolutePath());
						System.out.println("\t文件全名=" + readfile.getName());
					} else if (readfile.isDirectory()) {
						readAllFile(fileList[i].getPath());
					}
				}
			}
		} catch (FileNotFoundException e) {
			System.out.println("readfile()  Exception:" + e.getMessage());
		}
		return true;
	}

	/**
	 * 删除某个文件夹下的所有文件夹和文件
	 * @param delpath
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static boolean deleteFile(String delpath)
			throws FileNotFoundException, IOException {
		try {
			File file = new File(delpath);
			if (!file.isDirectory()) {
				System.out.println("1");
				file.delete();
			} else if (file.isDirectory()) {
				System.out.println("2");
				File[] fileList = file.listFiles();
				for (int i = 0; i < fileList.length; i++) {
					File delfile = fileList[i];
					if (!delfile.isDirectory()) {
						System.out.println("相对路径=" + delfile.getPath());
						System.out.println("绝对路径=" + delfile.getAbsolutePath());
						System.out.println("文件全名=" + delfile.getName());
						delfile.delete();
						System.out.println("删除文件成功");
					} else if (delfile.isDirectory()) {
						deleteFile(fileList[i].getPath());
					}
				}
				file.delete();
			}
		} catch (FileNotFoundException e) {
			System.out.println("deletefile()  Exception:" + e.getMessage());
		}
		return true;
	}
}

以上所述就是本文的全部内容了,希望大家能够喜欢。

相关文章

  • Java代码里如何拼接SQL语句到mybatis的xml

    Java代码里如何拼接SQL语句到mybatis的xml

    这篇文章主要介绍了Java代码里拼接SQL语句到mybatis的xml操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • 详解解密Java中的类型转换问题

    详解解密Java中的类型转换问题

    这篇文章主要介绍了Java中的类型转换问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • java 线程池如何执行策略又拒绝哪些策略

    java 线程池如何执行策略又拒绝哪些策略

    这篇文章主要介绍了java 线程池如何执行策略又拒绝哪些策略,文章通过线程池的执行方法 execute() 展开全篇内容,需要的小伙伴可以参考一下
    2022-05-05
  • Java中的同步非阻塞IO模型详解

    Java中的同步非阻塞IO模型详解

    这篇文章主要介绍了Java中的同步非阻塞IO模型详解,同步非阻塞IO模型,我们能够知道,用户线程一直发送请求,内核一直都能都够返回 ,直到内核完成准备数据、数据拷贝的工作,并且返回成功的指示,在此过程中用户线程不是阻塞的状态,需要的朋友可以参考下
    2024-01-01
  • Java异常简介和架构_动力节点Java学院整理

    Java异常简介和架构_动力节点Java学院整理

    这篇文章主要分享了Java异常简介和架构,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • Java利用数组随机抽取幸运观众如何实现

    Java利用数组随机抽取幸运观众如何实现

    这篇文章主要介绍了Java利用数组随机抽取幸运观众如何实现,需要的朋友可以参考下
    2014-02-02
  • Java数组队列概念与用法实例分析

    Java数组队列概念与用法实例分析

    这篇文章主要介绍了Java数组队列概念与用法,结合实例形式分析了Java数组队列相关概念、原理、用法及操作注意事项,需要的朋友可以参考下
    2020-03-03
  • spring @Conditional的使用与扩展源码分析

    spring @Conditional的使用与扩展源码分析

    这篇文章主要介绍了spring @Conditional的使用与扩展,这里需要注意如果Condition返回的是false,那么spirng就不会对方法或类进行解析,具体源码分析跟随小编一起看看吧
    2022-03-03
  • Mybatis查询返回Map<String,Object>类型的实现

    Mybatis查询返回Map<String,Object>类型的实现

    本文主要介绍了Mybatis查询返回Map<String,Object>类型的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • java编写汽车租赁系统

    java编写汽车租赁系统

    这篇文章主要为大家详细介绍了java编写汽车租赁系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02

最新评论