I have a method that copies files from one folder to another, deleting the file then. It even caters me perfectly, but I would like to know if there is any way to do this without having to use InputStream
and OutputStream
, since I already had some problems with writing using these classes.
I saw that in Java 8 there are functions that facilitate file operations using the Files
class. Is it possible to do this "move files" operation more simply and directly, using other methods, such as the Files
class?
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();
}
}
}