Renaming and Moving Files:

D

Danger_Duck

So like, I know that when you copy a file, you do something along the
lines of:

FileChannel ic = new FileInputStream("source.txt").getChannel();
FileChannel oc = new FileOutputStream("target.txt").getChannel();
ic.transferTo(0, ic.size(), oc);
ic.close();
oc.close();

However, I have no intention of copying a file-at the end of my
program, I'd like to rename one file and move another. How do I
accomplish this?

By the way, I tried something along the lines of:

res.renameTo(new File(resultsLocation.concat("/" + test.getName() +
"_results.txt")));

so could someone explain the proper way to use the method or recommend
a better one?

Thanks!

(Again, just how to MOVE and RENAME a file on the hardisk through
Java)
-The Duck
 
T

tomaszewski.p

So like, I know that when you copy a file, you do something along the
lines of:

FileChannel ic = new FileInputStream("source.txt").getChannel();
FileChannel oc = new FileOutputStream("target.txt").getChannel();
ic.transferTo(0, ic.size(), oc);
ic.close();
oc.close();

However, I have no intention of copying a file-at the end of my
program, I'd like to rename one file and move another. How do I
accomplish this?

By the way, I tried something along the lines of:

res.renameTo(new File(resultsLocation.concat("/" + test.getName() +
"_results.txt")));

so could someone explain the proper way to use the method or recommend
a better one?

Thanks!

(Again, just how to MOVE and RENAME a file on the hardisk through
Java)
-The Duck

Try to use java.io.File.renameTo(File) method.
Note that moving file "might not succeed if a file with the
destination abstract pathname already exists" (http://java.sun.com/
javase/6/docs/api/java/io/File.html).

Przemek
 
D

Dave Miller

Danger_Duck said:
So like, I know that when you copy a file, you do something along the
lines of:

FileChannel ic = new FileInputStream("source.txt").getChannel();
FileChannel oc = new FileOutputStream("target.txt").getChannel();
ic.transferTo(0, ic.size(), oc);
ic.close();
oc.close();

However, I have no intention of copying a file-at the end of my
program, I'd like to rename one file and move another. How do I
accomplish this?

By the way, I tried something along the lines of:

res.renameTo(new File(resultsLocation.concat("/" + test.getName() +
"_results.txt")));

so could someone explain the proper way to use the method or recommend
a better one?

Thanks!

(Again, just how to MOVE and RENAME a file on the hardisk through
Java)
-The Duck
You might want to look at File - it has builtins that do what you want:

File file = new File("path to the file");
file. \\delete(), renameTo(), etc.
 
D

Danger_Duck

     So the first ten lines of your message are just starchy
filler, right?




     Well, that'd be the way to rename a file, assuming res is a File
and resultsLocation is something with a concat() method that returns
a String or a URI.  What happened when you tried it?


     To rename a file, see above.  To move a file -- well, you'd
better describe what you mean by "move."

When I tried (res is the existing file),
res.renameTo(new File(resultsLocation.concat("/" + test.getName() +
"_results.txt")))

I indeed found a new file with the name I specified. Unfortunately, it
was blank, as the 0kb size confirmed. That's why I either need help
with renameTo or a better method.

By the way, res already exists (with contents and 1kb in size), and
was opened using:
File res = new File(resultsLocation.concat("/results.txt"));

I'm on a Windows XP by the way, if that matters...
 
T

tomaszewski.p

When I tried (res is the existing file),
res.renameTo(new File(resultsLocation.concat("/" + test.getName() +


I indeed found a new file with the name I specified. Unfortunately, it
was blank, as the 0kb size confirmed. That's why I either need help
with renameTo or a better method.

By the way, res already exists (with contents and 1kb in size), and
was opened using:
File res = new File(resultsLocation.concat("/results.txt"));

I'm on a Windows XP by the way, if that matters...

Try to use java.io.File.exists() method to ensure that source file
exists and target does not.
I am not not sure about XP, but maybe there is problem with file
separator character: '/'.

Przemek
 
D

Danger_Duck

Try to use java.io.File.exists() method to ensure that source file
exists and target does not.
I am not not sure about XP, but maybe there is problem with file
separator character: '/'.

Przemek

Hmm, I'll try "\\" instead and see how it goes...
 
D

Danger_Duck

Try to use java.io.File.exists() method to ensure that source file
exists and target does not.
I am not not sure about XP, but maybe there is problem with file
separator character: '/'.

Przemek

Meh, still blank :(

Have you used this before? If so, could you give me an example so I
can compare what I'm doing to what works? Thanks.
 
R

Roedy Green

However, I have no intention of copying a file-at the end of my
program, I'd like to rename one file and move another. How do I
accomplish this?

here is how I do it.

File tempFile = HunkIO.createTempFile( "temp", ".tmp", file );

Sun has a similar method (see
http://mindprod.com/jgloss/file.html#TEMPFILES) but I like my own
better. see http://mindprod.com/products1.html#HUNKIO for mine.

Then you write to the tempfile. When you are done and you are sure
all worked, you delete the original and rename your temp to the
original.


in.close();
out.close();
file.delete();
tempFile.renameTo( file );

To see this in action have a look at the CSV source code.
http://mindprod.com/products1.html#CSV

For this to work you want the temp file on the same drive and ideally
the same directory as the final output.
 
D

Danger_Duck

here is how I do it.  

 File tempFile = HunkIO.createTempFile( "temp", ".tmp", file );

Sun has a similar method (seehttp://mindprod.com/jgloss/file.html#TEMPFILES)  but I like my own
better.  seehttp://mindprod.com/products1.html#HUNKIOfor mine.

Then you write to the tempfile.  When you are done and you are sure
all worked, you delete the original and rename your temp to the
original.

            in.close();
            out.close();
            file.delete();
            tempFile.renameTo( file );

To see this in action have a look at the CSV source code.http://mindprod.com/products1.html#CSV

For this to work you want the temp file on the same drive and ideally
the same directory as the final output.

Found the problem-not quite sure why:
I had a BufferedReader that was using the file to be renamed to read
and do things with the lines.
I never closed it, so I guess it was still open?

Anyway, putting the .close() method into my code made renameTo(...)
work just fine.

Thanks again for everyone's help!
 
A

Arne Vajhøj

Eric said:
To rename a file, see above. To move a file -- well, you'd
better describe what you mean by "move."

Traditionally move and rename is the same except that:
- rename only works within same disk/filesystem
- move works across different disk/filesystem

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top