What is the established way of copying a file/dir into another dir in
Java?
I was looking into 'java.io' package, but could only find a renameTo()
method. It looks like it only renames the file or at the moves the
same file to the destination dir.
Please advice
Thanks
Sahil
Just write code that does it.
public static void copy(String fromname, String toname) throws
IOException {
InputStream is = new FileInputStream(fromname);
OutputStream os = new FileOutputStream(toname);
byte[] b = new byte[100000];
int n;
while((n = is.read(b)) >= 0) {
os.write(b, 0, n);
}
is.close();
os.close();
}
or similar.
Arne
If you want to copy streams, it's better to use IOUtils provided by Apache
Commons instead of hardcoded while and read:
IOUtils.copy(InputStream input, OutputStream output)
http://commons.apache.org/io/api-1.4/org/apache/commons/io/IOUtils.html#copy%28java.io.InputStream,
%20java.io.OutputStream%29
And to move file/dir into another dir, you could use:
File from = new File("foo");
File to = new File("destDir");
from.renameTo(new File(to, from.getName()));
Aeris
Because?
Arne
Not hardcoded and shared code, shorter, more maintainable, well-tested code,
not reinvent the wheel, probably more efficient, support, regular bug fix
and improvments...
And certainly many many other reasons that using a lib (particularly Apache
Commons) should be a reflex
> Arne
Invoking as static method is just as hardcoded as inline code.
> probably more efficient,
Certainly not.
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
...
public static int copy(InputStream input, OutputStream output)
throws IOException {
long count = copyLarge(input, output);
if (count > Integer.MAX_VALUE) {
return -1;
}
return (int) count;
}
public static long copyLarge(InputStream input, OutputStream output)
throws IOException {
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
long count = 0;
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
> and shared code, shorter, more maintainable, well-tested code,
> not reinvent the wheel,
> support, regular bug fix
> and improvments...
> And certainly many many other reasons that using a lib (particularly Apache
> Commons) should be a reflex
All good arguments. For non-trivial stuff.
But one has to stop at some point.
Replacing 5 lines of code with a simple while loop with a
library call does not have that large benefits.
If the developers having to touch the code can not figure
those 5 lines out, then the project is doomed no matter what.
I would still pick the library call if it were in the
standard Java library, because it cost nothing.
But an external library actually do carry cost.
For anybody except the most hopeless developers more cost
than maintaining a while loop.
Obviously if that particular library are used many
other places in the app, then the cost drops to zero.
But I would not use it this case to justify adding the
library to the project.
Arne
> All good arguments. For non-trivial stuff.
>
> But one has to stop at some point.
>
> Replacing 5 lines of code with a simple while loop with a
> library call does not have that large benefits.
>
> If the developers having to touch the code can not figure
> those 5 lines out, then the project is doomed no matter what.
>
> I would still pick the library call if it were in the
> standard Java library, because it cost nothing.
>
> But an external library actually do carry cost.
>
> For anybody except the most hopeless developers more cost
> than maintaining a while loop.
>
> Obviously if that particular library are used many
> other places in the app, then the cost drops to zero.
>
> But I would not use it this case to justify adding the
> library to the project.
>
> Arne
But in many projects, even the smallest, a large part of very dirty code
began with "a few lines of code that it doesn't matter if it's dirty".
And today, with tools like Maven, Ant or Ivy, cost to add a lib is very
insignificant compare to benefits
Including Apache Commons IO is just as simple as 5 XML lines, twice less
than your example code
And for example, there is FileUtils too in this lib, with all the needs to
do the wanted job, in a very very better way than your example (file not
found handling, null origin or destination, same origin and destination,
write permission deny, not a directory for destination, ...)
public static void copyFileToDirectory(File srcFile,
File destDir)
public static void copyFile(File srcFile,
File destFile)
public static void copyDirectoryToDirectory(File srcDir,
File destDir)
public static void copyDirectory(File srcDir,
File destDir)
http://commons.apache.org/io/api-
release/org/apache/commons/io/FileUtils.html