需求: 数据库里保存的文件路径中包含的文件名字可能和svn上文件名存在大小写不统一的情况,windows下是没有问题的,但系统现在要迁到Linux下,文件太多,不可能手动一个一个来修改,所以本人写了一个类,实现了可以查询需要修改的文件,可以按提示自己选择是否要修改,实现了修改前自动备份文件到指定的目录.还列出了修改前和修改后文件名的对比,修改了哪些文件名,一目了然.需要的连接数据库jar包请看附件(jtds-1.2.2.jar)
package com.jobcn;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ReadFileUpdate {
private static Connection connection = null;
private static Statement statement = null;
private static ResultSet rs =null;
/**
* @param args
*/
public static void main(String[] args) {
ReadFileUpdate.CompareToData("D:\\FTP\\", "tableName","filedName","E:\\");
}
/*
* ftpName:文件路径 tableName:表名 fieldName:字段名
* tragetPath: 目标路径(为安全起见,要备份原文件夹,会自动创建这个文件夹并拷备里面的文件)
*/
public static void CompareToData(String ftpName, String tableName,String fieldName,String tragetPath) {
String dbConnectionString = "jdbc:jtds:sqlserver://172.16.88.229:1433/jobcn_boss_train";
String user = "***";
String password = "***";
String sql = "select " + fieldName + " from " + tableName + "";
connection = ReadFileUpdate.getConnection(dbConnectionString, user,password);
rs = ReadFileUpdate.getResultSet(sql);
// 读取文件名
File file = new File(ftpName);
File[] lf = file.listFiles();
List<String> dblist = new ArrayList<String>();// 数据库里读的
List<String> filelist = new ArrayList<String>();// 文件中读的
try {
for (int i = 0; i < lf.length; i++) {
filelist.add(lf[i].getName());
}
while (rs.next()) {
String AdderessName = rs.getString(fieldName);
// 截取名字
String fileExt = AdderessName.substring(AdderessName.lastIndexOf("/") + 1, AdderessName.length());
if ("".equals(fileExt) || fileExt == null || fileExt == "NULL") {
} else {
dblist.add(fileExt);
}
}
//关闭所有连接
ReadFileUpdate.closeConnection(connection,statement,rs);
// 移除相同的名字,以文件为主
filelist.removeAll(dblist);
//循环打印出不同的名字
for (String strs : filelist) {
System.out.println("需要修改名字的文件是:"+strs);//文件的名字
}
if(filelist.size()==0){
System.out.println("您不需要修改名字");//文件的名字
}else{
// 转变之前的名字
String beforeFileName = "";
String beforeDBName = "";
// HasMap,存放文件与数据库名一一对应的名字
//HashMap<String, String> hashMap = new HashMap<String, String>();
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("请输入(Y:修改,N:不修改),Y/N?");
String s = input.next();
if(s.equalsIgnoreCase("Y")){
//改名字前先备份原来的数据
File file1 = new File(ftpName);//原来的路径
File file2 = new File(tragetPath);//目标路径
ReadFileUpdate.copy(file1, file2);
for (int i = 0; i < filelist.size(); i++) {
beforeFileName = filelist.get(i);
for (int j = 0; j < dblist.size(); j++) {
beforeDBName = dblist.get(j);
//忽略大小写比较一次
if (filelist.get(i).equalsIgnoreCase(dblist.get(j))) {
//不忽略大小写比较一次,如果不相同,则说明需要修改名字
if (!beforeFileName.equals(beforeDBName)) {
//hashMap.put(beforeFileName, beforeDBName);
//修改名字
File file3 = new File(ftpName+beforeFileName);
//修改成从数据库里查出的名字
File file4 = new File(ftpName+beforeDBName);
if(file3.exists()){
if(file3.renameTo(file4)){
System.out.println("文件:"+beforeFileName+"修改名字成功!修改后的名字是:"+beforeDBName);
}
}
}
}
}
}
}else{
System.out.println("您没有选择修改以上文件的名字!");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 获得连接
public static Connection getConnection(String dbConnectionString,
String user, String password) {
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
ReadFileUpdate.connection = DriverManager.getConnection(
dbConnectionString, user, password);
return ReadFileUpdate.connection;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
// 取得数据集
public static ResultSet getResultSet(String sql) {
try {
statement = connection.createStatement();
return statement.executeQuery(sql);
} catch (Exception e) {
e.printStackTrace();
System.err.println(sql);
return null;
}
}
//关闭连接
public static void closeConnection(Connection conn,Statement statement,ResultSet rs)
{
if (rs != null)
{
try{
rs.close();
rs = null;
}
catch(Exception e){
e.printStackTrace();
}
}
if (statement != null)
{
try{
statement.close();
statement = null;
}
catch(Exception e){
e.printStackTrace();
}
}
if (conn != null)
{
try{
conn.close();
conn = null;
}
catch(Exception e){
e.printStackTrace();
}
}
}
//拷贝一个目录或者文件到指定路径下
public static void copy(File source, File target) {
File tarpath = new File(target, source.getName());
if (source.isDirectory()) {
tarpath.mkdir();
File[] dir = source.listFiles();
for (int i = 0; i < dir.length; i++) {
//运用递归拷贝
copy(dir[i], tarpath);
}
System.out.println("原文件夹已成功备份到:"+target);
} else {
try {
InputStream is = new FileInputStream(source);
OutputStream os = new FileOutputStream(tarpath);
byte[] buf = new byte[1024];
int len = 0;
while ((len = is.read(buf)) != -1) {
os.write(buf, 0, len);
}
is.close();
os.flush();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}