プログラムを整理しようと思い、rename候補のファイル名がすでに存在してたら、ファイ名の末尾の数字(つまり分)に1を足して別のファイル名にしてrenameするということをやりました。
ファイルが存在するかしないかをwhileの条件式にいれ、ファイルが存在する場合はFileの名前を上書きしようとしましたが、Javaではそれが許されていない。ファイルクラスのオブジェクトは一旦作成すると変更不可の様子。
困ったので
while((new File(folderpath + filename+".wmv")).exists()){
のように作成したファイルクラスのオブジェクト名を特に指定せずに用いるという技で乗り切りました。
また同様にStringクラスも上書きができないのでStringBufferクラスを使用。
あとFileクラスのrenameToメッソッドを使ってファイル名を置き換えてもファイルクラスのgetName()でファイル名を取得しても置き換わらないことがわかりました。(これはなんでか理解できていないが、Javaの試験問題にもでるほど常識な様子)
あれやこれやでようやく整理ができました。
次回はリストのファイルを読み込んで一括変換させるようにします。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package filerename;
import java.io.File;
import java.util.Calendar;
/**
*
* @author nao
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//setup initial variable
String folderpath = new String("C:\\Documents and Settings\\nao\\デスクトップ\\test\\");
String inputString = new String("2004/04/251631 2009_01_19_22_34_10.wmv");
//インプットのStringを整形
// "/"を削除
String afterReplace1 = inputString.replaceAll("/", "");
// 空白を削除
String afterReplace = afterReplace1.replaceAll(" ", "");
System.out.println("after replace string:"+afterReplace);
//最初から4文字目を抜き出す(年数部分)
String Syear = new String( afterReplace.substring( 0, 4 ) );
//5,6文字目を抜き出す(月部分)
String Smonth = new String( afterReplace.substring( 4, 6 ) );
//7,8文字目を抜き出す(日数部分)
String Sday = new String( afterReplace.substring( 6, 8 ) );
//9,10を抜き出す(時間部分)
String Shour = new String( afterReplace.substring( 8, 10 ) );
//11,12を抜き出す(分部分)
String Smin = new String( afterReplace.substring( 10, 12 ) );
//残りを抜き出す(元ファイル名)
String orgname = new String( afterReplace.substring( 12, 35 ) );
System.out.println("Year:"+Syear);
System.out.println("Month:"+Smonth);
System.out.println("Day:"+Sday);
System.out.println("Hour:"+Shour);
System.out.println("Min:"+Smin);
//StringをInegerへ変換
int year = Integer.parseInt(Syear);
int month = Integer.parseInt(Smonth);
int day = Integer.parseInt(Sday);
int hour = Integer.parseInt(Shour);
int min = Integer.parseInt(Smin) ;
System.out.println("元ファイル名:"+orgname);
//変更前ファイル名
File orgFile = new File(folderpath + orgname);
boolean orgfileext = orgFile.exists();
System.out.println(orgFile + (orgfileext? " is found " : " is missing "));
//変更後のファイル名
StringBuffer filename = new StringBuffer(Syear+"_"+Smonth+"_"+Sday+"_"+Shour+"_"+Smin);
//変更後のファイル名がすでにないか確認して変更
System.out.println("Rename candidate is "+filename+".wmv");
while((new File(folderpath + filename+".wmv")).exists()){
System.out.println(folderpath + filename +" exists");
min = min + 1;
filename.replace(14,16, String.valueOf(min) );
}
File newFile = new File(folderpath + filename+".wmv");
if(orgfileext){
System.out.println("Start Rename to "+newFile.getName()+" from "+orgFile.getName());
boolean rename = orgFile.renameTo(newFile);
if(rename){
//ファイル名変更成功
System.out.println("Rename Successful");
//日付を設定 (月は設定したい月-1が必要)
Calendar newcalendar = Calendar.getInstance();
newcalendar.set(year, month-1, day, hour, min, 00);
System.out.println(newcalendar.getTime());
//更新日付を変更
boolean lastTime=newFile.setLastModified(newcalendar.getTimeInMillis());
System.out.println("Last Modified: "+lastTime);
System.out.println(newFile.lastModified());
}
else{
//ファイル名変更失敗
System.out.println("Fail to rename");
}
}
else{
boolean rename = false;
System.out.println(orgFile +" not exists.");
}
}
}
ファイルが存在するかしないかをwhileの条件式にいれ、ファイルが存在する場合はFileの名前を上書きしようとしましたが、Javaではそれが許されていない。ファイルクラスのオブジェクトは一旦作成すると変更不可の様子。
困ったので
while((new File(folderpath + filename+".wmv")).exists()){
のように作成したファイルクラスのオブジェクト名を特に指定せずに用いるという技で乗り切りました。
また同様にStringクラスも上書きができないのでStringBufferクラスを使用。
あとFileクラスのrenameToメッソッドを使ってファイル名を置き換えてもファイルクラスのgetName()でファイル名を取得しても置き換わらないことがわかりました。(これはなんでか理解できていないが、Javaの試験問題にもでるほど常識な様子)
あれやこれやでようやく整理ができました。
次回はリストのファイルを読み込んで一括変換させるようにします。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package filerename;
import java.io.File;
import java.util.Calendar;
/**
*
* @author nao
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//setup initial variable
String folderpath = new String("C:\\Documents and Settings\\nao\\デスクトップ\\test\\");
String inputString = new String("2004/04/251631 2009_01_19_22_34_10.wmv");
//インプットのStringを整形
// "/"を削除
String afterReplace1 = inputString.replaceAll("/", "");
// 空白を削除
String afterReplace = afterReplace1.replaceAll(" ", "");
System.out.println("after replace string:"+afterReplace);
//最初から4文字目を抜き出す(年数部分)
String Syear = new String( afterReplace.substring( 0, 4 ) );
//5,6文字目を抜き出す(月部分)
String Smonth = new String( afterReplace.substring( 4, 6 ) );
//7,8文字目を抜き出す(日数部分)
String Sday = new String( afterReplace.substring( 6, 8 ) );
//9,10を抜き出す(時間部分)
String Shour = new String( afterReplace.substring( 8, 10 ) );
//11,12を抜き出す(分部分)
String Smin = new String( afterReplace.substring( 10, 12 ) );
//残りを抜き出す(元ファイル名)
String orgname = new String( afterReplace.substring( 12, 35 ) );
System.out.println("Year:"+Syear);
System.out.println("Month:"+Smonth);
System.out.println("Day:"+Sday);
System.out.println("Hour:"+Shour);
System.out.println("Min:"+Smin);
//StringをInegerへ変換
int year = Integer.parseInt(Syear);
int month = Integer.parseInt(Smonth);
int day = Integer.parseInt(Sday);
int hour = Integer.parseInt(Shour);
int min = Integer.parseInt(Smin) ;
System.out.println("元ファイル名:"+orgname);
//変更前ファイル名
File orgFile = new File(folderpath + orgname);
boolean orgfileext = orgFile.exists();
System.out.println(orgFile + (orgfileext? " is found " : " is missing "));
//変更後のファイル名
StringBuffer filename = new StringBuffer(Syear+"_"+Smonth+"_"+Sday+"_"+Shour+"_"+Smin);
//変更後のファイル名がすでにないか確認して変更
System.out.println("Rename candidate is "+filename+".wmv");
while((new File(folderpath + filename+".wmv")).exists()){
System.out.println(folderpath + filename +" exists");
min = min + 1;
filename.replace(14,16, String.valueOf(min) );
}
File newFile = new File(folderpath + filename+".wmv");
if(orgfileext){
System.out.println("Start Rename to "+newFile.getName()+" from "+orgFile.getName());
boolean rename = orgFile.renameTo(newFile);
if(rename){
//ファイル名変更成功
System.out.println("Rename Successful");
//日付を設定 (月は設定したい月-1が必要)
Calendar newcalendar = Calendar.getInstance();
newcalendar.set(year, month-1, day, hour, min, 00);
System.out.println(newcalendar.getTime());
//更新日付を変更
boolean lastTime=newFile.setLastModified(newcalendar.getTimeInMillis());
System.out.println("Last Modified: "+lastTime);
System.out.println(newFile.lastModified());
}
else{
//ファイル名変更失敗
System.out.println("Fail to rename");
}
}
else{
boolean rename = false;
System.out.println(orgFile +" not exists.");
}
}
}
コメント