title: How to escape slashes in command line argument (file path) in Java?
body: <p>Please note I'm developing this using NetBeans under Windows. I'm also running JDK 1.8.</p>
  
  <p>The program takes a few arguments, via the command-line. One argument is a file path. The user might type <code>-i C:\test</code>. How do I escape the slash? Nothing seems to work right. </p>
  
  <pre><code>public class Test {
  
      public static void main(String[] args) throws FileNotFoundException, ParseException {
          // Simulate command line execution
          String[] arguments = new String[] { "-i C:\test" };
  
          // Create Options object
          Options options = new Options();
  
          // Add options input directory path.
          options.addOption("i", "input", true, "Specify the input directory path");
  
          // Create the parser
          CommandLineParser parser = new GnuParser();
  
          // Parse the command line
          CommandLine cmd = parser.parse(options, arguments);
  
          String inputDirectory = cmd.getOptionValue("i");
          String escaped;
  
          // Gives error of "invalid regular expression: Unexpected internal error
          escaped = inputDirectory.replaceAll("\\", "\\\\");
  
          // This does not work
          File file = new File (escaped);
          Collection&lt;File&gt; files = FileUtils.listFiles(file, null, false);
  
          // This works
          File file2 = new File ("C:\\test");
          Collection&lt;File&gt; files2 = FileUtils.listFiles(file2, null, false);
      }
  }
  </code></pre>
  
  <p>I tried replaceAll but, like it says in the code, it does not compile and it returns an invalid regular expression error.</p>
  
  <p>I know best practice is to use File.separator, but I honestly have no clue how I can apply it to a command line argument. Maybe the user might type a relative path. The file path the user references could be on any drive, as well.</p>
  
  <p>How do I escape the backslashes so that I can loop through each file with FileUtils?</p>
  
  <p>Thank you very much for any help.</p>