Move file list between folders

Asked

Viewed 2,545 times

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.

  • @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.

  • java.nio.file.Files has a move since version 7, it doesn’t suit you?

2 answers

4


The first big change to make is just move the file and not copy it, that’s a huge waste of resources. You can just ask the operating system to rearrange your file system organization without even copying, or even moving one byte from it, just change its metadata to indicate that it is in another folder. You don’t need to know how it works, just know which method to call.

private static void copiarArquivos() throws IOException {
    File src = new File(".");
    String dstPath = "C:\\java\\";
    for (File f : src.listFiles()) {
        String fileName = f.getName();
        if (fileName.contains("File")) Files.move(f.toPath(), Paths.get(dstPath, fileName), REPLACE_EXISTING);
    }
}

I put in the Github for future reference.

Java 8 still allows use streams (examples) and the code can become more declarative. I do not always like, I think this is simple and understandable.

You can kill two variables, but I left.

  • The curiosity title, instead of copying with the same name and rename later, I can rename by changing inside the Path.get?

  • It’s not copying anything, it’s moving.

  • So it’s okay to move under a different name?

  • 1

    No problem at all. Moving means changing the file system metadata, does not touch the content, so you change the path There is no distinction between the name of the file and the folder, what you are doing is changing the name of an object, part of the name is its last name, that is, it is the folder. Renaming is just moving, to the same or another folder.

  • Just a small correction (I could only test kkk today), the method move wait 3 parameters, being 2 of the Path type and an optional third of the type CopyOption. How filename is String, it was necessary to exchange for f.toPath() to function properly.

  • @diegofm True, I did without paying attention to these details. I am correcting.

Show 1 more comment

4

Just out of curiosity in versions prior to 7.

Renaming:

import java.io.File;

public class Movendo {

  public static void main(String[] args) {
    try {
      File arquivo = new File("C:/pasta1/arquivo.txt");

      if (arquivo.renameTo(new File("C:/pastab/" + arquivo.getName()))) {
        System.out.println("Arquivo movido com sucesso!");
      } else {
        System.out.println("Falha ao mover arquivo!");
      }
    } catch (Exception e) {
      System.out.println("Falha ao mover arquivo!");
    }
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.