title: Remove whitespace from all filenames in directory - Java
body: <p>I have directory of images and I want to rename the files by removing all of the whitespace in the name.</p>
  
  <p>So let's say I have a file name called " f il ena me .png" (I plan on checking all of the file names in the directory). How might I remove all of the spaces and rename the image so that the correct file name (for this specific case) is "filename.png".</p>
  
  <p>So far I have tried the following code and it actually deletes the image in the directory (I'm testing it on one image in the directory currently).</p>
  
  <pre><code>public static void removeWhiteSpace (File IBFolder) {
      // For clarification:
      // File IBFolder = new File("path/containing/images/folder/here");
      String oldName;
      String newName;
      String temp;
      for (File old : IBFolder.listFiles()) {
          oldName = old.getName();
          temp = oldName.replaceAll(" ", "");
          // I have also tried:
          // temp = oldName.replaceAll("//s", "");
          temp = temp.split(".png")[0];
          newName = temp + ".png";
          System.out.println(newName);
          old.renameTo(new File(newName));
      }
  }
  </code></pre>