栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 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;


public class TestReadFile {
	
	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;
	}
	
	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();
		}
	}
	
	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("nt相对路径=" + 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;
	}

	
	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;
	}
}

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/151610.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号