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); 

In this code snippet I try to rename the file but I'm unable to find the error in it.

5
  • 5
    So are we all. What error do you get ? Have you tried using File.exists() and other methods to sanity check what your program is doing ? Commented Feb 1, 2011 at 10:49
  • please use code {} tag and also provide errors that you get when run this code. Commented Feb 1, 2011 at 10:49
  • The error is that the value of b is false which means the file has not been renamed. Commented Feb 1, 2011 at 10:52
  • 1
    Why are you (ab)using java.sql.Timestamp here? What's the functional requirement? You might have misunderstood its purpose. Commented Feb 1, 2011 at 10:59
  • The requirement is that i have to read a CSV file and rename it with a name which includes current time along with the original file name. For example if the name of original file is abc.csv then renamed file should be something like abc20110201.csv Commented Feb 2, 2011 at 5:54

3 Answers 3

4

Won't getTime().toString() return a string with colons in it? That would be illegal in a filename.

Sign up to request clarification or add additional context in comments.

3 Comments

Thnx. Can u suggest any way out??
String str = new SimpleDateFormat("HH_mm_ss").format(new Date()); would format your timestamp as 23_59_11
Yup. Either change the date format it returns, or manually replace illegal filename characters.
1

you can remove colon from String for example with this method :

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

output be : 121212

Comments

1

I would use something like

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); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.