java文件操作删除某一目录下指定类型的文件,代码如何实现
4个回答
展开全部
package com.test1;
import java.io.File;
public class Test {
public static void main(String args[]) {
try {
deletefile("C:\\aa");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void deletefile(String delpath) throws Exception {
File file = new File(delpath);
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File delfile = new File(delpath + "\\" + filelist[i]);
if (!delfile.isDirectory()) {
if(delfile.getName().endsWith("txt")){
delfile.delete();
System.out.println(delfile.getAbsolutePath() + "删除文件成功");
}
} else if (delfile.isDirectory()) {
deletefile(delpath + "\\" + filelist[i]);
}
}
}
}
import java.io.File;
public class Test {
public static void main(String args[]) {
try {
deletefile("C:\\aa");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void deletefile(String delpath) throws Exception {
File file = new File(delpath);
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File delfile = new File(delpath + "\\" + filelist[i]);
if (!delfile.isDirectory()) {
if(delfile.getName().endsWith("txt")){
delfile.delete();
System.out.println(delfile.getAbsolutePath() + "删除文件成功");
}
} else if (delfile.isDirectory()) {
deletefile(delpath + "\\" + filelist[i]);
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public static void main(String[] args) {
File file = new File("D://");
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
String fileName = files[i].getName();
if (fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0) {
if(fileName.endsWith("txt")) {
System.out.println(fileName);
files[i].deleteOnExit();
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package reptile;
import java.io.File;
import java.io.FilenameFilter;
import javax.swing.filechooser.FileFilter;
public class TestDelete
{
public static void main(String[] args){
File f = new File("D:/Books/oracle 11g从入门到精通");
//FilenameFilter filter = new FileFilter();
File[] files = f.listFiles();
for(int i = 0 ; i < files.length; i++){
if(files[i].getName().endsWith(".txt")){
files[i].delete();
}
}
}
}
import java.io.File;
import java.io.FilenameFilter;
import javax.swing.filechooser.FileFilter;
public class TestDelete
{
public static void main(String[] args){
File f = new File("D:/Books/oracle 11g从入门到精通");
//FilenameFilter filter = new FileFilter();
File[] files = f.listFiles();
for(int i = 0 ; i < files.length; i++){
if(files[i].getName().endsWith(".txt")){
files[i].delete();
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询