Good evening. 3 days already trying to figure out how to move files in java. There is a task: to read the list of files from the specified directory, sort by the word entered from the keyboard and move the sorted files to the specified directory. This method returns a list of files from the directory.

public static String[] getFilesList(File fl) { String[] listFiles = fl.list(); for (int i = 0; i < listFiles.length; i++) { } return listFiles; } 

Next, the cycle sorting is implemented:

 for (int i = 0; i < arr.length; i++) { if (arr[i].indexOf(inpFilterName) > 0) 

And now the biggest problem - in the same cycle I want to immediately move the files to the specified path. Google prompted to use the renameTo () method of the File class. I tried to implement it like this:

  File dir = new File(); dir.renameTo() 

But I cannot understand what to pass to it as an argument, and will it work if we have a file name as input and not a file? In general, I am completely confused and really looking forward to help!

    2 answers 2

    I would venture to suggest that by sorting you meant filtering - i.e. move only those files whose names contain the specified inpFilterName string. In this case, you can do the following:

     File destFolder = new File("output"); // это папка, в которую будем перемещать File[] files = fl.listFiles(); // получаем непосредственно файлы, не просто имена for (File file : files) { if (file.getName().indexOf(inpFilterName) != -1) { file.renameTo(new File(destFolder, file.getName())); } } 

    By the way, notice the difference in if conditions. Perhaps you wrote exactly what you wanted, but just in case: your condition will choose strings in which the required substring is at index 1 or more. Those. "test123" .indexOf ("test") will not pass this condition. String#indexOf() returns -1 on failure, not 0.

    • > Ie. "test123" .indexOf ("test") this condition will not skip Skips. My condition is> 0, i.e. not -1. Thanks for the implementation example, I thought about it. One question: We get the files directly! I aim to use this program for sorting mp3. What are the consequences and what will happen with performance if a couple of dozen mp3 files are placed in the array? You can, of course, first use the list of names for sorting, and make a selection from it .. and only then move it .. I do not quite understand how it will look. - trierra
    • I beg your pardon, the documentation has explained everything. - trierra
    • Everything works great! Thank you very much! - trierra

    Check this code!
    I myself wrote.
    In this code, first renameTo - moves the folder to another directory and then sorts, that is, removes unnecessary files from the folder after moving.

     import java.io.File; import java.io.IOException; public class File_remove { private static String to = ""; public static void main(String[] args) throws IOException { String from = "c:/from"; to = "c:/to"; File From = new File(from); if (!From.exists()) { System.out.println(to + " does not exist!"); return; } File To = new File(to); if (!To.delete()){ System.out.println("You can't move!"); return; } From.renameTo(To); Check(To.list(), "inputFilterName"); } public static void Check(String[] arr, String inpFilterName) { for (int i = 0; i < arr.length; i++) { if (arr[i].indexOf(inpFilterName) < 0){ if (!(new File(to + File.separator + arr[i]).delete())){ System.out.println("You can't delete the file!"); } } } } } 
    • 2
      for efforts 5, and for code 2. we learn to name classes, variables, methods correctly - jmu