Move and Rename file using Java -
i want move , rename file using java. tried code fails rename: please, thank you
public class moveandrenamefile { public moveandrenamefile(){ //current date , time dateformat dateformat = new simpledateformat("yyyy/mm/dd hh:mm:ss"); date date = new date(); { file file = new file("c:\\foldera\\client.pdf"); file newfile = new file(("c:\\folderb\\clientx.pdf")); if(file.renameto(newfile)+dateformat.format(date)){ system.out.println("file rename success");; }else{ system.out.println("file rename failed"); } }
the file i/o api changed , improved considerably java 7. 1 of problems legacy (pre java 7) file api that:
• rename method didn't work consistently across platforms
the nio.2 api (file api introduced java 7) way of renaming files using files.move
:
files.move(file, newfile, standardcopyoption.replace_existing);
the section mapping java.io.file functionality java.nio.file in legacy file i/o code replace old file operations new ones.
Comments
Post a Comment