Answer to Question #63985 in Java | JSP | JSF for hamod

Question #63985
write a java code segment to rename the file "wet.bin" to "dry.bin" iff the file is found on directory named "d:/avc/win/hadit/",otherwise you should create this file and instruct jvm to delete this new file upon termination
1
Expert's answer
2016-12-13T11:10:08-0500
String filePathSrc ="d:/avc/win/hadit/wet.bin";
String filePathDes ="d:/avc/win/hadit/dry.bin";

File fileSrc = new File(filePathSrc);
File fileDes = new File(filePathDes);

Boolean result;
try {
if (fileSrc.exists()){
result= fileSrc.renameTo(fileDes);
if(result) {
System.out.printf("File \"%s\" was rename to \"%s\"...%n",filePathSrc,filePathDes);
}
} else {
result = fileSrc.createNewFile();
if(result) {
System.out.printf("File \"%s\" was create...%n",filePathSrc);
}
fileSrc.deleteOnExit();
System.out.printf("File \"%s\" will be delete on termination...%n",filePathSrc);
}
} catch (NullPointerException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS