欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-04 18:50:42
java实现读取、删除文件夹下的文件 package test.com; import java.io.file; import java.io.fil...

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

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