• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is also a large variety of forums, including an I/O one.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic