我用JAVA写了一个重命名的程序,可是运行后去不能将文件重命名,程序如下:
import java.io.*;
public class TakeFilePathAndName {
public TakeFilePathAndName ()
{
}
public static void main(String[] args) {
// This is the path where the file's name you want to take.
String path = "E:\\jiang1\\32128420100204";
getFile(path);
}
private static void getFile(String path){
// get file list where the path has
File file = new File(path);
// get the folder list
File[] array = file.listFiles();
for(int i=0;i<array.length;i++){
if(array[i].isFile()){
// only take file name
File newFile = new File("E:\\jiang1\\32128420100204\\"+array[i].getName());
//File oldFile = new File("E:\\jiang1\\32128420100204\\"+array[i].getName()+"_20100204");
String[] ss = new String[20];
ss = array[i].getName().split("\\.");
String b="E:\\jiang1\\32128420100204\\"+ss[0]+"_20100204.ixf";
File oldFile = new File(b);
//System.out.println(ss[0]);
if(oldFile.renameTo(newFile))
//System.out.println(array[i].getName()+"_20100204");
// take file path and name
//System.out.println("#####" + array[i]);
// take file path and name
//System.out.println("*****" + array[i].getPath());
System.out.println(b);
}else if(array[i].isDirectory()){
getFile(array[i].getPath());
}
}
}
}
我想知道错在哪里了
import java.io.*;
public class TakeFilePathAndName {
public TakeFilePathAndName ()
{
}
public static void main(String[] args) {
// This is the path where the file's name you want to take.
String path = "E:\\jiang1\\32128420100204";
getFile(path);
}
private static void getFile(String path){
// get file list where the path has
File file = new File(path);
// get the folder list
File[] array = file.listFiles();
for(int i=0;i<array.length;i++){
if(array[i].isFile()){
// only take file name
File newFile = new File("E:\\jiang1\\32128420100204\\"+array[i].getName());
//File oldFile = new File("E:\\jiang1\\32128420100204\\"+array[i].getName()+"_20100204");
String[] ss = new String[20];
ss = array[i].getName().split("\\.");
String b="E:\\jiang1\\32128420100204\\"+ss[0]+"_20100204.ixf";
File oldFile = new File(b);
//System.out.println(ss[0]);
if(oldFile.renameTo(newFile))
//System.out.println(array[i].getName()+"_20100204");
// take file path and name
//System.out.println("#####" + array[i]);
// take file path and name
//System.out.println("*****" + array[i].getPath());
System.out.println(b);
}else if(array[i].isDirectory()){
getFile(array[i].getPath());
}
}
}
}
我想知道错在哪里了
而newFile是要改的文件,而oldFile是新的文件;
所以应该是:newFile.renameTo(oldFile);
写了一个程序,可以运行成功;
package Gaiming;import java.io.*;public class Gaiming {
public static void getFile(String path, String gaiming){
File file = new File(path);
File[] file02 = file.listFiles();
for(File file03 : file02){
if(file03.isFile()&&file03.getName().equals("a.txt")){
String[] str = new String[26];
str = file03.getName().split("\\.");
String str02 = str[0];
String str03 = str[1];
String str05 = str02+gaiming+"."+str03;
File file05 = new File(path,str05);
if(file03.renameTo(file05)){
System.out.println(str05);
}else{
System.out.println("重命名失败");
}
}else{
continue;
}
}
}
public static void main(String[] args){
String path = "E:\\";
String gaiming = "123";
getFile(path, gaiming);
}
}
输出:a123.txt;
应该是if(newFile.renameTo(oldFile))
测试通过,楼主给分吧~