2
I have a method that copies files from one folder to another, then deletes the file. It suits me perfectly, but I wonder if there’s any way to do it without having to resort to InputStream
and OutputStream
, because I’ve had some problems with writing using these classes.
I saw that in java 8 there are functions that facilitate file operations using the class Files
. It is possible to do this operation of "move files" more simply and directly, using other methods, such as the class Files
?
Follow the current code:
private static void copiarArquivos() throws IOException {
File src = new File(".");
String dstPath = "C:\\java\\";
File dst;
File[] files = src.listFiles();
for (File f : files) {
String fileName = f.getName();
if (fileName.contains("File")) {
dst = new File(dstPath + fileName);
InputStream in = new FileInputStream(f);
OutputStream out = new FileOutputStream(dst);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
f.delete();
}
}
}
I don’t know if you want to know anything specific, but in any Java you can do better than that if you just want to move the files, you can let the operating system do it without copying even a byte.
– Maniero
@mustache using the File class itself? Honestly I have no idea how it does, I thought it was the Files class, this new package that was added in the newer versions. I edited the question to not just close to java 8 or the cited class.
– user28595
java.nio.file.Files
has amove
since version 7, it doesn’t suit you?– Sorack