2011-02-01 21 views
0
File file= new File("C:\\Documents and Settings\\Administrator\\Desktop\\ajay\\abc.csv"); 
Timestamp ts=new Timestamp(new Date().getTime()); 
String str= ts.toString(); 
String st="C:\\Documents and Settings\\Administrator\\Desktop\\ajay\\abc\\"+str+".csv"; 
System.out.println(new Date().getTime()); 
boolean b=file.renameTo(new File(st)); 
System.out.println(b); 

このコードスニペットでは、ファイルの名前を変更しようとしましたが、エラーが見つかりません。Javaでファイルの名前を変更

+5

のようなものを使用しますので、私たちはすべてです。どのようなエラーが出ますか? File.exists()や他のメソッドを使って、あなたのプログラムがやっていることを正当性をチェックしようとしましたか? –

+0

code {}タグを使用してください。このコードを実行するとエラーが発生します。 –

+0

エラーは、bの値がfalseで、ファイルの名前が変更されていないことを意味します。 –

答えて

4

getTime().toString()はコロンで文字列を返しますか?それはファイル名では不正です。

+0

Thnxのようになります。あなたはどんな方法を提案することができますか? –

+4

文字列str =新しいSimpleDateFormat( "HH_mm_ss")。書式(新しい日付());あなたのタイムスタンプのフォーマットは23_59_11 –

+0

です。返される日付形式を変更するか、不正なファイル名の文字を手動で置き換えてください。 – Merijn

1

あなたがこの方法で例えば文字列からコロンを削除することができます

 String time = "12:12:12"; 
     String time2 = time.replace(":", ""); 

出力すること:121212

1

私は

final File file= new File("C:\\Documents and Settings\\Administrator\\Desktop\\ajay\\abc.csv"); 
final Calendar cal = Calendar.getInstance(); 
cal.setTime(new Date()); 
final StringBuilder str = new StringBuilder(); 
str.append(cal.get(Calendar.YEAR)); 
str.append(cal.get(Calendar.MONTH)); 
str.append(cal.get(Calendar.DATE)); 
final String st="C:\\Documents and Settings\\Administrator\\Desktop\\ajay\\abc"+str+".csv"; 
System.out.println(new Date().getTime()); 
final boolean b = file.renameTo(new File(st)); 
System.out.println(b); 
+0

'SimpleDateFormat'は日付を書式設定するのが簡単です。 – BalusC

+0

@BalusCあなたは正しいです。 – flash

+0

@flashはあなたのコードを試しましたが、問題が存在します。 –

関連する問題