i want to move a single file from a folder. i tried my level best. but the all the files from the folder is moving.
pls anyone is there to solve my problem.

public static synchronized void move(File src, File dest) throws FileNotFoundException, IOException {
copy(src, dest);
src.delete();
}



public static synchronized void copy(File src, File dest) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);



byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}

this is my coding but its not working fine.
can u give me another coding pls.

Edited 3 Years Ago by happygeek: fixed formatting

byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}

Personally, I don't use very often this kind of way to read files, but why don't you try what masijade has suggested with renaming the file. Do you have access to the JAva API?

As far as the functioning of that code it should be okay, but the complaint is that all files were getting "moved". This will definately only "move" (actually copy and delete, which isn't the same thing) one file, so, obviously, we would need the code from which you are calling this.

A few points to this code, however.

Your copy method should use a finally method to close the streams (otherwise you will, not might leak open file descriptors).

I really have no idea why you would synchronize these two methods, there is absolutely (and I mean absolutely) no reason to.

You should simply use the rename() method of file rather than copying the file and deleteing it (this loses all "history" information on the file, and will change the files accessability and ownership specs, whereas rename doesn't).

If you insist on copying and deleteing the file, then at least look into using the transferFrom or transferTo method of FileChannel.

Edit: Another point about rename versus copying. When you copy the file, you must read and write every byte, of course, which takes time (more time as the file gets larger). Rename simply (where applicable) modifies the OSes references to the file, which is only a few bytes worth of actual data manipulation, and it takes the same few milliseconds to perform for a 100 byte as it would for a 1 GB file. The only time where this doesn't apply (at least usually, there are probably other instances as well, but this is the most common one) is when the file need to be moved across file systems. Then the file must be read and written, so check the return of the rename method, and if it failed, then do something like this.

k masijade i understand ur explanations,
but i need to keep the original file in the same folder and i have to save the modified file into another folder.
at this time i ddont know how to do.
can u help me yaar.
advance thanks
hidash.....

If you "have to keep it in the same folder", then why are you doing

src.delete();

And why did you ask how to move a file, when you want to copy it, in that case.

I don't know what your problem is now. If you want to write a file to some place other than where you are "reading" info from, then open a FileOutputStream to that location. What's the problem?

my goal is , i am opening a file from a subfolder and doing some modifications and after closing the file i want to save the modified file outside the subfolder with different name.
i think u will understand my problem.
now, help me by sending some example codings.
advance thanks
hidash.

Like I said, simply open the fileoutputstream to the place where you want to put the file. There is no reason to create a new file alongside the old file, then "move" it to where you want it, simply create the fileoutputstream where you want the new file from the very beginning.

And, as far as "moving" the "copied/edited" version of the original file, renameTo() still works.

And none of your posts have addressed the original question yet, which was how you "move" a single file as oppossed to "all of the files", as you put it. I would say that's pretty easy. Perform whatever action it is that you're performing to "move" the files on only one file, rather than all of them.

As far as posting code for moving a file, here

File a = new File("/tmp/bogus.sh");
a.renameTo(new File("/var/tmp/moved.sh"));
This article has been dead for over six months. Start a new discussion instead.