如何在Java中的命令行参数(文件路径)中转义斜杠? [英] How to escape slashes in command line argument (file path) in Java?

查看:259
本文介绍了如何在Java中的命令行参数(文件路径)中转义斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,我正在Windows下使用NetBeans进行开发。我也在运行JDK 1.8。



程序通过命令行接受几个参数。一个参数是文件路径。用户可能键入 -i C:\test 。我如何逃避斜线?没有什么似乎正常工作。

  public class Test {

public static void main(String [] args)throws FileNotFoundException,ParseException {
//模拟命令行执行
String [] arguments = new String [] {-i C:\test};

//创建选项对象
选项options = new Options();

//添加选项输入目录路径。
options.addOption(i,input,true,指定输入目录路径);

//创建解析器
CommandLineParser parser = new GnuParser();

//解析命令行
CommandLine cmd = parser.parse(options,arguments);

String inputDirectory = cmd.getOptionValue(i);
转义字符串

//发送无效正则表达式:错误的内部错误
escaped = inputDirectory.replaceAll(\\,\\\\\)的错误;

//这不工作
文件文件=新文件(转义);
集合<文件>文件= FileUtils.listFiles(file,null,false);

//这个工程
文件file2 =新文件(C:\\test);
集合<文件> files2 = FileUtils.listFiles(file2,null,false );
}
}

我尝试replaceAll,但是像代码,它不编译,它返回一个无效的正则表达式错误。



我知道最好的做法是使用File.separator,但我真的没有线索可以将其应用于命令行参数,也许用户可能键入相对路径,用户引用的文件路径也可以在任何驱动器上。



我逃避了背部睫毛,以便我可以使用FileUtils循环遍历每个文件?



非常感谢您的帮助。

解决方案

更改替换

  escaped = inputDirectory.replaceAll(\\ \\\\); 

  escaped = inputDirectory.replaceAll(Pattern.quote(\\),Matcher.quoteReplacement(\\\\)); 

由于您正在模拟程序中的参数,请考虑

 -i C:\test

实际上是标签(即\t)在 C:和<$ c之间$ c> est



正确的方法是:

 -i C:\\test


Please note I'm developing this using NetBeans under Windows. I'm also running JDK 1.8.

The program takes a few arguments, via the command-line. One argument is a file path. The user might type -i C:\test. How do I escape the slash? Nothing seems to work right.

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<File> files = FileUtils.listFiles(file, null, false);

        // This works
        File file2 = new File ("C:\\test");
        Collection<File> files2 = FileUtils.listFiles(file2, null, false);
    }
}

I tried replaceAll but, like it says in the code, it does not compile and it returns an invalid regular expression error.

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.

How do I escape the backslashes so that I can loop through each file with FileUtils?

Thank you very much for any help.

解决方案

Change your replacement

escaped = inputDirectory.replaceAll("\\", "\\\\");

to

escaped = inputDirectory.replaceAll(Pattern.quote("\\"), Matcher.quoteReplacement("\\\\"));

Since you are mimicking the argument within your program, take in account that

 "-i C:\test"

Will actually be a tab (i.e \t) in between C: and est

the correct way would've been:

 "-i C:\\test"

这篇关于如何在Java中的命令行参数(文件路径)中转义斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆