• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

API to Duplicate/Move Files in Filesystem

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Sheriff
Posts: 3061
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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. :-)
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I do some of my very best work in water. Like this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic