Java 更改父目录和子目录中的文件名
我是一名Java初学者,尝试使用文件和目录。我想创建一个程序,在其中我可以自动更改文件名,同时在所有子目录中搜索无效的文件名。实际上,我正试图将大量文件加载到服务器上,但服务器设置不允许文件名包含特殊字符。首先,我能够编写代码,如果我将路径传递到某个目录,它将重命名该目录中所有名称无效的文件: 公共类重命名{Java 更改父目录和子目录中的文件名,java,file-io,Java,File Io,我是一名Java初学者,尝试使用文件和目录。我想创建一个程序,在其中我可以自动更改文件名,同时在所有子目录中搜索无效的文件名。实际上,我正试图将大量文件加载到服务器上,但服务器设置不允许文件名包含特殊字符。首先,我能够编写代码,如果我将路径传递到某个目录,它将重命名该目录中所有名称无效的文件: 公共类重命名{ public static String baseLoc = "C:/Users/Developer/Desktop/.../Data Cleanup"; public static v
public static String baseLoc = "C:/Users/Developer/Desktop/.../Data Cleanup";
public static void main(String[] args) {
//LinkedList<File> fileList = new LinkedList<File>();
File obj = new File(baseLoc);
int count = 0;
for (File file: obj.listFiles())
{
String origName = file.getName();
if (origName.contains("&") || origName.contains("#") || origName.contains("@"))
{
System.out.println("Original name: "+origName);
origName = origName.replaceAll("&", "_and_");
origName = origName.replaceAll("@", "_at_");
String newName = origName.replaceAll("#", "_");
System.out.println("New Name: "+newName);
String newLoc = baseLoc+"/"+newName;
File newFile = new File(newLoc);
System.out.println(file.renameTo(newFile));
count++;
}
}
}
公共静态字符串baseLoc=“C:/Users/Developer/Desktop/../Data Cleanup”;
公共静态void main(字符串[]args){
//LinkedList fileList=新建LinkedList();
文件obj=新文件(baseLoc);
整数计数=0;
对于(文件:obj.listFiles())
{
字符串origName=file.getName();
if(origName.contains(“&”)| | origName.contains(“#”)| | origName.contains(“@”))
{
System.out.println(“原始名称:“+origName”);
origName=origName.replaceAll(&“,”和“);
origName=origName.replaceAll(“@”和“_at”);
字符串newName=origName.replaceAll(“#”和“”);
System.out.println(“新名称:“+newName”);
字符串newLoc=baseLoc+“/”+newName;
文件newFile=新文件(newLoc);
System.out.println(file.renameTo(newFile));
计数++;
}
}
}
}
现在我想做同样的事情,但只是这次我希望所有的文件都被重命名,即使是在子目录中。有人能告诉我如何做到这一点吗?将代码移动到实用程序方法(例如,
public void renameAll(File f){}
)。有一个检查文件是否为目录的条件,并使用其内容递归调用方法。然后做你现在正在做的事情
public void renameAll(File[] files){
for(File f: files){
if(f.isDirectory){
renameAll(f.listFiles());
}
rename(f);
}
}
public void rename(File f){ }
将您拥有的代码移动到实用程序方法(例如,
public void renameAll(文件f){}
)。有一个检查文件是否为目录的条件,并使用其内容递归调用方法。然后做你现在正在做的事情
public void renameAll(File[] files){
for(File f: files){
if(f.isDirectory){
renameAll(f.listFiles());
}
rename(f);
}
}
public void rename(File f){ }
递归是你的朋友
/**Removes 'invalid' characters (&,#,@) from pathnames in the given folder, and subfolders, and returns the number of files renamed*/
public int renameDirectory(File base){
//LinkedList<File> fileList = new LinkedList<File>();
int count=0;//count the renamed files in this directory + its sub. You wanted to do this?
//Process each file in this folder.
for (File file: base.listFiles()){
String origName = file.getName();
File resultFile=file;
if (origName.contains("&") || origName.contains("#") || origName.contains("@")){
//I would replace the if statement with origName.matches(".*[&#@].*") or similar, shorter but more error prone.
System.out.println("Original name: "+origName);
origName = origName.replaceAll("&", "_and_");
origName = origName.replaceAll("@", "_at_");
String newName = origName.replaceAll("#", "_");
System.out.println("New Name: "+newName);
String newLoc = baseLoc+File.separator+newName;//having "/" hardcoded is not cross-platform.
File newFile = new File(newLoc);
System.out.println(file.renameTo(newFile));
count++;
resultFile=newFile;//not sure if you could do file=newFile, tired
}
//if this 'file' in the base folder is a directory, process the directory
if(resultFile.isDirectory()){//or similar function
count+=renameDirectory(resultFile);
}
}
return count;
}
/**从给定文件夹和子文件夹的路径名中删除“无效”字符(&、#、@),并返回重命名的文件数*/
公共int重命名目录(文件库){
//LinkedList fileList=新建LinkedList();
int count=0;//对该目录及其子目录中重命名的文件进行计数。是否要执行此操作?
//处理此文件夹中的每个文件。
对于(文件:base.listFiles()){
字符串origName=file.getName();
文件结果文件=文件;
if(origName.contains(“&”)| | origName.contains(“#”)| | origName.contains(“@”)){
//我会用origName.matches(“.[@].*)或类似的、更短但更容易出错的语句替换if语句。
System.out.println(“原始名称:“+origName”);
origName=origName.replaceAll(&“,”和“);
origName=origName.replaceAll(“@”和“_at”);
字符串newName=origName.replaceAll(“#”和“”);
System.out.println(“新名称:“+newName”);
字符串newLoc=baseLoc+File.separator+newName;//硬编码为“/”不跨平台。
文件newFile=新文件(newLoc);
System.out.println(file.renameTo(newFile));
计数++;
resultFile=newFile;//不确定是否可以执行file=newFile,累了
}
//如果基本文件夹中的此“文件”是一个目录,请处理该目录
if(resultFile.isDirectory()){//或类似函数
count+=重命名目录(resultFile);
}
}
返回计数;
}
递归是你的朋友
/**Removes 'invalid' characters (&,#,@) from pathnames in the given folder, and subfolders, and returns the number of files renamed*/
public int renameDirectory(File base){
//LinkedList<File> fileList = new LinkedList<File>();
int count=0;//count the renamed files in this directory + its sub. You wanted to do this?
//Process each file in this folder.
for (File file: base.listFiles()){
String origName = file.getName();
File resultFile=file;
if (origName.contains("&") || origName.contains("#") || origName.contains("@")){
//I would replace the if statement with origName.matches(".*[&#@].*") or similar, shorter but more error prone.
System.out.println("Original name: "+origName);
origName = origName.replaceAll("&", "_and_");
origName = origName.replaceAll("@", "_at_");
String newName = origName.replaceAll("#", "_");
System.out.println("New Name: "+newName);
String newLoc = baseLoc+File.separator+newName;//having "/" hardcoded is not cross-platform.
File newFile = new File(newLoc);
System.out.println(file.renameTo(newFile));
count++;
resultFile=newFile;//not sure if you could do file=newFile, tired
}
//if this 'file' in the base folder is a directory, process the directory
if(resultFile.isDirectory()){//or similar function
count+=renameDirectory(resultFile);
}
}
return count;
}
/**从给定文件夹和子文件夹的路径名中删除“无效”字符(&、#、@),并返回重命名的文件数*/
公共int重命名目录(文件库){
//LinkedList fileList=新建LinkedList();
int count=0;//对该目录及其子目录中重命名的文件进行计数。是否要执行此操作?
//处理此文件夹中的每个文件。
对于(文件:base.listFiles()){
字符串origName=file.getName();
文件结果文件=文件;
if(origName.contains(“&”)| | origName.contains(“#”)| | origName.contains(“@”)){
//我会用origName.matches(“.[@].*)或类似的、更短但更容易出错的语句替换if语句。
System.out.println(“原始名称:“+origName”);
origName=origName.replaceAll(&“,”和“);
origName=origName.replaceAll(“@”和“_at”);
字符串newName=origName.replaceAll(“#”和“”);
System.out.println(“新名称:“+newName”);
字符串newLoc=baseLoc+File.separator+newName;//硬编码为“/”不跨平台。
文件newFile=新文件(newLoc);
System.out.println(file.renameTo(newFile));
计数++;
resultFile=newFile;//不确定是否可以执行file=newFile,累了
}
//如果基本文件夹中的此“文件”是一个目录,请处理该目录
if(resultFile.isDirectory()){//或类似函数
count+=重命名目录(resultFile);
}
}
返回计数;
}
您可能必须使用递归-请参阅,您可能必须使用递归-请参阅,谢谢。它帮助我理解了文件的功能。但是,我无法从main()传递baseLoc并将其修改到递归函数中的新位置。抱歉,我对一个琐碎的问题感到困惑。谢谢你的帮助。:)谢谢它帮助我理解了文件的功能。但是,我无法从main()传递baseLoc并将其修改到递归函数中的新位置。抱歉,我对一个琐碎的问题感到困惑。谢谢你的帮助。:)