Javaでファイル数の名前を変更するコードに取り組んでいます。にファイルのリストがあります.txt
。私のプログラムがドキュメントの名前とその新しい名前を取得するファイル。現在動作しません。コンパイルして実行しますが、ファイルの名前を変更しません。
これが私のコードです:
public static void rename(String ol, String ne){
File oldfile =new File(ol);
File newfile =new File(ne);
int t=0;
if( oldfile.isFile() && oldfile.canRead()){
if (newfile.exists()){
t++;
ne = ne.substring(0,ne.lastIndexOf('.')) + " (" + t + ")" +
ne.substring(ne.lastIndexOf('.')) ;
rename(ol,ne);
}
if(oldfile.renameTo(newfile))
System.out.println("Rename succesful");
else
System.out.println("Rename failed" + " - " + ol + " " + ne);
}else
System.out.println("CANNOT Rename " + oldfile + " because read/write issues. Check
if File exists" );
}
public static void main(String[] args) throws IOException
{
ReadFile ren = new ReadFile("List of Founds.txt");
String r[] = ren.OpenFile();
for(int j=0; j<ReadFile.numberOfLines; j++){
String pdfOldName = r[j].substring(0,r[j].lastIndexOf('.'));
String pdfNewName = r[j].substring((r[j].lastIndexOf('.') + 4));
rename(pdfOldName, pdfNewName);
}
}
これは「ファウンドのリスト」.txt
ファイルで、古い名前が左側にあり、新しい名前が右側にあります。
test.pdf.txt ayo1
test2.pdf.txt ayo2
test3.pdf.txt ayo3