Edit file name method - run time complexity I wrote a function in Java that edit file name, and replace each space char into dash char.
Currently I iterate all the files in a specific directory, iterate in each file name, creating a new file name, and replace the file in the directory.


  I guess that the current complexity is \$O(N*M)\$ with  
  
  
  \$N\$ being the number of files in directory and  
  \$M\$ being the number of chars in each file.
  


Can the run-time-complexity be improved?

public static void editSpace(String source, String target) {

    // Source directory where all the files are there
    File dir = new File(source);
    File[] directoryListing = dir.listFiles();

    // Iterate in each file in the directory
    for (File file : directoryListing) {

        String childName = file.getName();
        String childNameNew = "";

        // Iterate in each file name and change every space char to dash char
        for (int i = 0; i &lt; childName.length(); i++) {

            if (childName.charAt(i) == ' ') {
                childNameNew += "-";
            } else {
                childNameNew += childName.charAt(i);
            }
        }

        // Update the new directory of the child
        String childDir = target + "\\" + childNameNew;

        // Renaming the file and moving it to a new location
        if (!(childNameNew.equals(""))
                &amp;&amp; (file.renameTo(new File(childDir)))) {

            // If file copied successfully then delete the original file .
            file.delete();

            // Print message
            System.out.println(childName + " File moved successfully to "
                    + childDir);
        }

        // Moving failed
        else {
            // Print message
            System.out.println(childName + " Failed to move the file to "
                    + childDir);
        }
    }

}
Hello and Welcome to Code Review. The runtime complexity in terms of reading characters (n files * m characters) for substitution cannot be improved, you can use the String replace(char oldChar, char newChar) method:

String childName = file.getName();
String childNameNew = "";
for (int i = 0; i &lt; childName.length(); i++) {
    if (childName.charAt(i) == ' ') {
        childNameNew += "-";
    } else {
        childNameNew += childName.charAt(i);
    }
}


You can substitute your block with one line: 

String childNameNew = childName.replace(' ', '-');


From Java 7 it is discouraged using File renameTo method, you can use from class Files the move method and you can write the mv file part of your code in this way:

// Calculate oldPath and newPath for your file with Paths.get method
try {
    Files.move(oldPath, newPath, StandardCopyOption.REPLACE_EXISTING);  
    System.out.println(oldPath + " File moved successfully to " + newPath);
} catch (IOException e) {
    System.out.println(oldPath + " Failed moved the file to " + newPath);   
}