• Post Reply Bookmark Topic Watch Topic
  • New Topic

API to Duplicate/Move Files in Filesystem  RSS feed

 
Prasant Jagannath
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a program that needs to take several files and copy and move them to another location in the filesystem, how would I do that? There is a clone method in the File class, but I think that just creates another File handle, not a duplicate file.
Thanks....
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a FileInputStream to read the old file and a FileOutputStream to write the new one. If you just want to move a file as opposed to copying it, you could use File.renameTo(File) instead.
 
Prasant Jagannath
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought of those two options, but I thought there was a better way.....
In any case, when I considered the renameTo option, I was wondering if that would actually work, since I wasnt sure if Java just rewrote the filename, or if it rewrote the file system (since the filesystem has to be able to locate the file).
Oh, and I forgot to say thanks.
[ January 09, 2003: Message edited by: Prasant Jagannath ]
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

since I wasnt sure if Java just rewrote the filename, or if it rewrote the file system

if say Java just rewrote the filename and OS doesn't reflect it. whats the use of having renameTo()??? right?? so it has to be done at OS level. its similar to ask "if a file is REALLY created at OS level when we say createFile() or its just within JVM?"...
and btw, i am also unable to think easier way for moving files...you can build basic API to do this and then build a wrapper around that object that provides easier way for transfering files where u don't have to do certain things explicitly but i guess that won't be applicable much here (i hope u get me here)...
hope this helps.
regards
maulin
 
Prasant Jagannath
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I wrote a very simple class to do this here it is if anyone wants it:
<code>
import java.io.*;
public class FileActions {

/**
* Simple method to copy a file from one location to the other
* @param theFile The file to be copied
* @param dest The destination directory encapsulated in a File object
* @return Returns the success of the operation
*/
public static boolean fileCopy(File theFile, File dest){
FileReader fIn;
FileWriter fOut;
// if the destination is the same as the origin, dont do anything
if(theFile.getPath().equals(dest.getName())){
return false;
}
try{
fIn = new FileReader(theFile);
fOut = new FileWriter(new File(dest, theFile.getName()));
int data = fIn.read();
while(data != -1){
fOut.write(data);
data = fIn.read();
}
fIn.close();
fOut.close();
}catch(IOException e){
return false;
}
return true;
}
/**
* Method to move a file to a new location. File does not exist at
* old location.
* @param theFile The file to be moved.
* @param dest The destination directory encapsulated in a File object
* @return Returns the success of the operation.
*/
public static boolean fileMove(File theFile, File dest){
File temp = new File(dest, theFile.getName());
return theFile.renameTo(temp);
}

}
</code>
 
Prasant Jagannath
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I have to warn that the above only copies characters for text and stuff, it wont work for image or object data.
More suspiciously, I cant seem to get the move working, I get a security violation. Its like Java wont allow me to delete or tamper with the original file. Im developing on windows, so I closed all open windows pertaining to that file, closed the dos prompt with the file listing, emptied the recycle bin, and in general anything related to it. And it still wont allow me to get to it the security.checkWrite method screws me over continually.
How the heck do I overcome this security violation? No other program is using this file, Im the only user on this system, and this thing belongs to me. WTF?
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not only but also, how could this be used to move a jpg or wav file, i wonder?
good luck with the security.checkWrite issue, at present i have no idea but am sure someone else does.
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
maybe things need to be synchronized in case anything is simultaneously trying to access the file? just a wild stab in the dark i'm afraid. :-)
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you are on a shared filesystem? Are you sure you have the necessary access to write or delete a file? Are you running from an Applet context, which usually restricts the operations you're allowed to perform? What exactly does the error message say?
 
Spencer J Lee
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

not only but also, how could this be used to move a jpg or wav file, i wonder?


Couldn't this be done by reading/writing bytes instead of characters? I believe read()/write() and related methods for Reader/Writer classes handle characters, but read()/write() and related methods for InputStream/OutputStream classes handle bytes.

On a related note, is there an optimal method to read from one stream and write to another stream? I mean reading and writing one byte at a time just doesn't seem right. Is it preferable, in terms of performance, to read and write, say, 1024 bytes or some other size? Is this the kind of situation where I can use the PipedInputStream and PipedOutputStream?

Thanks,
Spencer
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!