网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 
中国网管联盟
Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道
 当前位置: > bitsCN.com > JAVA > 核心技术 > 高级编程 > java读取文件夹下的所有文件夹和文件  

java读取文件夹下的所有文件夹和文件

2007-06-29  作者:bitsCN整理  来源:收集整理  点评 投稿 收藏

以下是实现的代码:


package com.borland.samples.welcome;

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

public class ReadFile {
public ReadFile() {}

/**
* 删除某个文件夹下的所有文件夹和文件
* @param delpath String
* @throws FileNotFoundException
* @throws IOException
* @return boolean
*/
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++) {

网管论坛bbs_bitsCN_com


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

/**
* 删除某个文件夹下的所有文件夹和文件
* @param delpath String
* @throws FileNotFoundException
* @throws IOException
* @return boolean
*/
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());
网管联盟bitsCN_com

System.out.println("absolutepath=" + file.getAbsolutePath());
System.out.println("name=" + file.getName());

}
else if (file.isDirectory()) {
System.out.println("文件夹");
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("name=" + 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("D:/file");
//deletefile("D:/file");
}
catch (FileNotFoundException ex) {
}
catch (IOException ex) {
}
System.out.println("ok");
网管联盟bitsCN@com

}

网管u家u.bitscn@com


TAGs
 上一篇:JAVA中把WORD文档直接转换成html   下一篇:用Java编写Oracle数据库存储过程
java读取文件夹下的所有文件夹和文件 评论:
loading.. 评论加载中…
评论:请自觉遵守互联网相关政策法规,评论不得超过250字。

验证码: 注册用户
本类热门排行:
最新推荐文章:
网管论坛交流: