【发布时间】:2013-04-18 20:17:07
【问题描述】:
尊敬的所有 JLine 用户,
我最近正在开发一个使用 JLine 的控制台应用程序,以提供命令和文件名补全。
它与 FileNameCompleter 配合得很好,但是我无法正确获取完整的文件名。
我的代码如下:
List<Completer> loadCompleter =
Arrays.asList(
new StringsCompleter(commands),
new FileNameCompleter(),
new NullCompleter()
);
console.addCompleter(new ArgumentCompleter(loadCompleter));
while ((line = console.readLine()) != null) {
line = line.trim();
// here I print out the line in char.
char[] result = line.toCharArray();
for (int i = 0; i < result.length; i ++) {
System.out.println(result[i] + " : " + (int)result[i]);
}
}
在我的代码的最后一部分,我打印出我从控制台得到的行,例如,我收到了
myCommand test\new\test.txt
输出是 myCommand testnewtest.txt
由于某种原因,反斜杠消失了,我从来没有得到正确的文件路径。 当我在类似 Unix 的系统中进行测试时,这不是问题,因为正斜杠似乎没问题。
谁能帮助我获取完整文件名的正确方法?非常感谢。
硅。
【问题讨论】:
-
见student.cs.uwaterloo.ca/~cs132/Weekly/W03/FilePaths.html,其中有一个重要说明
A backslash — like is used in a file path — can't be used directly in a Java String. Java uses the backslash to mean “the next character means something special”. Doubling the backslash in Java indicates that “the next character (the second backslash) should be inserted into the String”. -
您的代码没有问题...只是用户需要在Java字符串中输入路径为“test\\new\\test.txt”的文件名,而不是Java来解释它正确。我猜这对用户来说很麻烦。如果是这样,您可以在打印出来之前将用户字符串输入中出现的任何“\”替换为“\\”。
-
您不能直接从 Java 输入中获取 \,因为正如您之前的评论中提到的,\ 被解释为其他内容,例如\n, \t.
-
因为用户从FileCompleter获取文件名,所以我不认为他们喜欢将光标移回并添加额外的\,也许我需要在Jline中修补一些东西,以添加额外的\文件完成者。
-
@zyzyis 问题解决了吗?
标签: java parsing console terminal jline