| Author |
problem in renaming a file with timestamp
|
gaurav kumar
Greenhorn
Joined: Apr 20, 2006
Posts: 16
|
|
Hi i m trying to rename a text file with the current timestamp appended to its previous name. But the 'toRename' method which i m using is failing to do so. Though the same file is being renamed with some other String value. The code i m using is as under: File file = new File("D:\\project\\vision\\files"); String [] flName = file.list(); //for each order file in the directory for(int i=0;i<flName.length;i++) { System.out.println("****filename****"+flName[i]); File file1 = new File("D:\\project\\vision\\files"+"\\"+flName[i]); Timestamp tmstamp = new Timestamp(System.currentTimeMillis()); boolean bool = file1.renameTo(new File("D:\\project\\vision\\files"+"\\"+tmstamp.toString)); System.out.println("****"+bool); }
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16479
|
|
|
What is the file name that you are proposing to use for the new name?
|
 |
gaurav kumar
Greenhorn
Joined: Apr 20, 2006
Posts: 16
|
|
|
i just want to append the current timestamp value to file name. For that i m using the 'toString' method of the timestamp but it doesn't seems to be working.
|
 |
Hanuma Deepak Muvvala
Ranch Hand
Joined: Nov 20, 2005
Posts: 157
|
|
Hi, it might be the case such that the toString method of timestamp will generate special characters like : which the file name wont accept in windows os ,so replace all special characters in that with some _ and rename ,then your problem may be solved.
|
 |
Aalok Pandit
Greenhorn
Joined: Sep 26, 2005
Posts: 15
|
|
gaurav, as already pointed out, the problem is of the character ':' which is not acceptable in windows. the code works fine on Unix. also, you might want to change the line
boolean bool = file1.renameTo(new File("D:\\project\\vision\\files"+"\\"+tmstamp.toString));
to otherwise you may end up giving the same name to all files. [ July 18, 2006: Message edited by: Aalok Pandit ] [ July 18, 2006: Message edited by: Aalok Pandit ]
|
 |
gaurav kumar
Greenhorn
Joined: Apr 20, 2006
Posts: 16
|
|
|
thanx a ton!!! it solved my problem.
|
 |
 |
|
|
subject: problem in renaming a file with timestamp
|
|
|