java删除指定文件

package th.deletePic.service;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JOptionPane;

@SuppressWarnings("serial")
public class DeletePic {

@SuppressWarnings("unchecked")
public static File[] getFiles(String dir, String s) {
File file = new File(dir);

s = s.replace('.', '#');
s = s.replaceAll("#", "\\\\.");
s = s.replace('*', '#');
s = s.replaceAll("#", ".*");
s = s.replace('?', '#');
s = s.replaceAll("#", ".?");
s = "^" + s + "$";

Pattern p = Pattern.compile(s);
ArrayList list = filePattern(file, p);
if (list == null || list.size() == 0) {
return null;
}
File[] rtn = new File[list.size()];
list.toArray(rtn);
return rtn;
}

@SuppressWarnings("unchecked")
private static ArrayList filePattern(File file, Pattern p) {
if (file == null) {
return null;
} else if (file.isFile()) {
Matcher fMatcher = p.matcher(file.getName());
if (fMatcher.matches()) {
ArrayList list = new ArrayList();
list.add(file);
return list;
}
} else if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null && files.length > 0) {
ArrayList list = new ArrayList();
for (int i = 0; i < files.length; i++) {
ArrayList rlist = filePattern(files[i], p);
if (rlist != null) {
list.addAll(rlist);
}
}
return list;
}
}
return null;
}

@SuppressWarnings("unchecked")
private static String getFilePath() {
ResultSet rs = null;
Connection con = null;
String sql = "select top 1 key_value from TBL_SYSTEM_CONFIG where key_name='Jour_Picture'";

String contextPath = System.getProperty("user.dir");
InputStream inputStream = null;
try {
inputStream = new BufferedInputStream(new FileInputStream(
contextPath + "/databaseConnection.properties"));
// inputStream = new BufferedInputStream(new FileInputStream(contextPath+"/bin/com/th/deletePic/resource/databaseConnection.properties"));
} catch (FileNotFoundException e2) {
JOptionPane.showMessageDialog(null, "配置文件读取失败!", "错误提示", JOptionPane.ERROR_MESSAGE);
}
Properties p = new Properties();
String connectionUrl = "";
try {
p.load(inputStream);
connectionUrl += p.getProperty("url") + ";database="
+ p.getProperty("database") + ";user="
+ p.getProperty("username") + ";password="
+ p.getProperty("password") + ";";
} catch (IOException e1) {
e1.printStackTrace();
}
String str = "";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl);
rs = con.prepareStatement(sql).executeQuery();

while (rs.next()) {
str = rs.getString(1);
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "数据库连接异常!", "错误提示", JOptionPane.ERROR_MESSAGE);
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, "系统异常,请联系系统员", "错误提示", JOptionPane.ERROR_MESSAGE);
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return str;
}

public static void main(String[] args) {

String filePath = DeletePic.getFilePath();
String month = Calendar.getInstance().get(Calendar.MONTH) + "";
if (month.length() < 2) {
month = "0" + month;
}
String filename = Calendar.getInstance().get(Calendar.YEAR) + ""
+ month + "*.jpg";
File[] files = DeletePic.getFiles(filePath, filename);
if (files != null) {
for (File f : files) {
File delFile = new File(filePath + "/" + f.getName());
delFile.delete();
}
}

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值