• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to move a file ? (and a few more java IO ques)

 
Ong CherYee
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Firstly, i'm new here, so good day to you all. Good to have found this friendly java website

Firstly, I have to move a file from 1 directory to another.
Then I have to rename the file according to its timestamp. (in the yymmddmmss format. From year all the way down to seconds)

How do I move the file and get the file's timestamp?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Have a look at the javadocs of the java.io.File class. It has a renameTo method (which actually moves a file, as long as the new location is part of the same file system), and a getLastModified (or similar) method which tells you when the file was last changed.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is also a large variety of forums, including an I/O one.
 
Lukasz Bajzel
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As mentioned by other ranchers, renameTo() is a tricky option.

Per the API documentation "Many aspects of the behavior of this method (renameTo() )are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful."

So it is the responsibility of the programmer to make sure that the rename really works. we would recommend to write your own code to move the file rather than relying on renameTo() method.

Sincerly,
Your friends at www.javaadvice.com
www.javaadvice.com - The place where your questions are answered directly.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Lukasz Bajzel:
we would recommend to write your own code to move the file rather than relying on renameTo() method.


You might recommend that, but that doesn't make it good advice. renameTo(), if it works, will move the file using the operating system's own APIs, and is going to be far more efficient than anything you can cook up. Furthermore, it will probably succeed in moving a file that's larger than the free space on the disk, whereas your own copy-to-move version won't.

Now, it's true that renameTo() isn't guaranteed to work, but if it fails, it returns "false". Therefore a prudent thing to do looks something like



Where "copyTheFile()" is a brute-force copy of the data from one location to another.
 
Rob Spoor
Sheriff
Posts: 20819
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
Have a look at the javadocs of the java.io.File class. It has a renameTo method (which actually moves a file, as long as the new location is part of the same file system), and a getLastModified (or similar) method which tells you when the file was last changed.

I've worked with it as well, and tests on a Windows network have been successful when moving files from one local partition to another local partition, as well as from a local partition to a network share.
 
Lukasz Bajzel
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we are in complete agreement with the replies. We just meant to say that while renameTo() is the default option available, make sure to verify that it succeeds.

The below code can be a platform independent option to perform a copy operation.

import java.io.*;

boolean fileCopied = false;
InputStream in = null;
OutputStream out = null;
String sepr = System.getProperty("file.separator");
try {
in = new FileInputStream(path+sepr+srcFileName);
File newFile = new File(path+sepr+targetFileName);
out = new FileOutputStream(path+sepr+targetFileName);
byte[] buffer = new byte[2048];
while (true) {
synchronized (buffer) {
int length = in.read(buffer);
if (length != -1) {
out.write(buffer, 0, length);
}
else
break;
}
}

File oldFile = new File(path+sepr+srcFileName);
fileCopied = newFile.exists();
}
catch(Throwable t){
//handle exceptions here.
}
finally {
try{
if (in != null) { in.close();}
if (out != null) { out.close(); }
}catch(Throwable t1){}
}
return fileCopied;

Still, note that this is the code to do a copy operation. (It doen't move any file)


Hope this is more clear now.

Sincerly,
Your friends at www.javaadvice.com
www.javaadvice.com - The place where your questions are answered directly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!