【发布时间】:2017-01-17 19:05:59
【问题描述】:
我想在 Java 中使用 awk 命令行。为此,我有这段代码(我在教程中看到了这段代码):
Map map = new HashMap();
map.put("file", new File(fileDirectory));
CommandLine cmdLine = new CommandLine("awk");
cmdLine.addArgument("{print $1}", false);
cmdLine.addArgument("${file}");
cmdLine.setSubstitutionMap(map);
System.out.println(cmdLine.toString());
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
ExecuteWatchdog watchdog = new ExecuteWatchdog(10000);
DefaultExecutor executor = new DefaultExecutor();
executor.setWatchdog(watchdog);
executor.execute(cmdLine, resultHandler);
resultHandler.waitFor();
在本例中,我的代码打印文件的第一列。
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
我想将输出打印到一个文件中,但我想不通。
【问题讨论】:
-
这似乎是一个 XY 问题。 Java 拥有读取文件和从每一行提取第一个标记所需的所有工具