How to Move a file in Java?
•Hello!
We are developing a app in Java for a customer.
The app creates a zip file that might be pretty big once a day.
This file is later fetched and removed by an other app.
In the spec the customer has this demand:
The zip file is first delivered to temporary folder Temp on the server.
To prohibit premature reading the zip file is then moved (by Move or Rename, not Copy) to output folder Output, lying beside Temp folder on the server.
My first question is:
I guess I then should to this with:
oldFile.renameTo(new File("newFile"))
Please correct me if I'm wrong.
My second question is how would I do if I should copy the file?
My guess is something like:
FileOutputStream output = new FileOutputStream(newFile);
FileInputStream input = new FileInputStream(oldFile);
byte[] buffer = new byte[2048];
int i = 0;
while ((i = input.read(buffer)) != -1)
{
output.write(buffer,0,i);
}
input.close();
output.close();
...or is there a smarter way?
My third question is
Is the expression "Move" something from C?
I guess then that in Java
oldFile.renameTo(new File("newFile"))
must be equal to that?
Best regards
Fredrik
