java read delete files
来源:互联网 发布:java线程等待唤醒 编辑:程序博客网 时间:2024/04/29 05:54
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++) {
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());
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");
}
}
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++) {
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());
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");
}
}
- java read delete files
- delete files
- Java libraries to read and write PDF files
- java code to read xls and xlsx files
- How to read and write JSON files in Java(Gson)
- read and write files
- [OpenCV] Write & Read files
- Delete untracked files
- Read Excel files from Python
- Boost filesystem 之 read files
- Read Large Files in Python
- pandas read data from files
- Rman delete expried backup files
- Backup / delete event log files
- [CodeProject C#]Read/Write Config files
- Read binary files more efficiently using C#
- perl: read in files in several ways
- Android: read files from assets in NDK
- FCKeditor 2.3 在ASP.NET中的设置和使用http://blog.licns.com/blog/article.asp?id=173
- 谈谈各自使用框架是用什么写的
- 一分钟学会使用CBitmapButton
- JPA--转Obtaining Access to an Entity Manager
- 白领男人的10种短命生活方式[转帖]
- java read delete files
- WIN32中颜色值(COLORREF)与.NET中颜色值(Color)的转换
- javascript技术
- IT公司速查手册 近日被上海公安勒令关闭
- WebClient的研究笔记(一)--认识WebClient
- 实现对全局程序集缓存的操作
- 使用const提高函数的健壮性
- 如何用最低的成本成就最高流量系列 各种线上推广手段的投入产出比数字公布(IreSearch.cn/龚文祥)(推荐帖)
- svn提交数据失败(提示 svn:MKACTIVITY ... 403 Forbidden )