java实现读取、删除文件夹下的文件
作者:hebedich 发布时间:2021-12-06 20:07:48
标签:java,读取,删除,文件夹
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;
}
}
以上所述就是本文的全部内容了,希望大家能够喜欢。


猜你喜欢
- 文件上传是网站非常常用的功能,直接使用Servlet获取上传文件还得解析请求参数,比较麻烦,所以一般选择采用apache的开源工具,comm
- #define Testusing System;namespace Wrox.ProCSharp.ParameterTestSample.
- 使用 WebView 时,我们通常会重写以下方法:shouldOverrideUrlLoading() onPageStarted()onP
- 前言:最近准备研究一下图片缓存框架,基于这个想法觉得还是先了解有关图片缓存的基础知识,今天重点学习一下Bitmap、BitmapFactor
- public void CutToF(Stream stream)
- 详解Java中HashSet和TreeSet的区别1. HashSetHashSet有以下特点:不能保证元素的排列顺序,顺序有可能发生变化不
- 1、先看一下项目目录:2、新建一个AS项目,创建如上图所示的目录结构,然后添加内容:(1)修改添加布局文件:activity_main.xm
- 一、委托1、什么是委托委托是面向对象的、类型安全的,是引用类型。使用delegate关键字进行定义。委托的本质就是一个类,继承自System
- 上一篇中我们介绍了自定义实现BaseAdapter的普通实现布局,然而上一章也说了普通实现的方式效率会很低,而且对系统开销也很大,所以,那样
- Android内部没有控件来直接显示文档,跳转WPS或其他第三方文档App体验性不好,使用腾讯X5内核能很好的解决的这一问题。一、下载腾讯X
- 本文实例讲述了C#使用HtmlAgilityPack抓取糗事百科内容的方法。分享给大家供大家参考。具体实现方法如下:Console.Writ
- 这篇文章主要介绍了Springboot2.0处理自定义异常并返回json,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考
- 前文传送门:Netty分布式FastThreadLocal的set方法实现逻辑剖析recycler的使用这一小节开始学习recycler相关
- 一、为什么要有泛型?我们在写一些方法时可能会方法名相同,参数类型不同的方法,这种叫做重载。如果只是因为参数类型不同里面做的业务逻辑都是相同的
- HttpWebRequest 是一个Http 请求类,继承于 WebRequest。WebRequest 是一个抽象类,能够对统一资源标识符
- 重要属性1、scale: 小数点后的位数。如将1.234构建为BigDecimal,scale属性则为32、RoundingMode(对照数
- 我们经常需要对我们的开发的软件做各种测试, 软件对系统资源的使用情况更是不可少, 目前有多个监控工具, 相比JProfiler对系统资源尤其
- 写在前面:接下来很长一段时间的文章主要会记录一些项目中实际遇到的问题及对应的解决方案,在相应代码分析时会直指问题所在,不会将无关的流程代码贴
- 我们知道,多线程是Android开发中必现的场景,很多原生API和开源项目都有多线程的内容,这里简单总结和探讨一下常见的多线程切换方式。我们
- IDEA自动跳出括号并且补全分号(类似eclipse的功能)跳括号外头去ctrl shift enter叫做 Complete Curren