我在这里结束了我的智慧.我确信这很简单,我很可能在理解java和流时遇到很大漏洞.我认为有这么多课程,我试图通过API来弄清楚我何时以及如何使用大量的输入/输出流,我有点不知所措.
我刚刚了解了apache commons库的存在(自学java失败),我正在尝试将我的一些Runtime.getRuntime().exec转换为使用commons - exec.已经修复了一些每6个月一次这个问题,然后消除了exec的风格问题.
代码执行perl脚本,并在GUI运行时从GUI中显示stdout.
调用代码在swingworker内部.
我迷路了如何使用pumpStreamHandler ...无论如何这里是旧代码:
String pl_cmd = "perl script.pl"
Process p_pl = Runtime.getRuntime().exec( pl_cmd );
BufferedReader br_pl = new BufferedReader( new InputStreamReader( p_pl.getInputStream() ) );
stdout = br_pl.readLine();
while ( stdout != null )
{
output.displayln( stdout );
stdout = br_pl.readLine();
}
Run Code Online (Sandbox Code Playgroud)
我想这是我得到的复制粘贴代码,我很久以前还不完全理解.上面我假设正在执行该过程,然后抓取输出流(通过"getInputStream"?),将其放入缓冲读取器,然后将循环,直到缓冲区为空.
我没有得到的是为什么这里不需要'waitfor'样式命令?是否可能有一段时间缓冲区将为空,退出循环,并在进程仍在进行时继续?当我运行它时,情况似乎并非如此.
无论如何,我试图使用commons exec获得相同的行为,基本上再次从谷歌找到的代码:
DefaultExecuteResultHandler rh = new DefaultExecuteResultHandler();
ExecuteWatchdog wd = new ExecuteWatchdog( ExecuteWatchdog.INFINITE_TIMEOUT );
Executor exec = new DefaultExecutor();
ByteArrayOutputStream out = new ByteArrayOutputStream();
PumpStreamHandler psh = new PumpStreamHandler( out ); …
Run Code Online (Sandbox Code Playgroud) 我正在使用apache commons配置XMLConfiguration对象和XPATH表达式引擎来查询XML文件.我是xpath和apache commons的新手,我的语法有问题.
xml文件如下所示:
<attrs>
<attr name="my_name" val="the_val"/>
<attr name="my_name2" val="the_val2"/>
</attrs>
Run Code Online (Sandbox Code Playgroud)
我想要做的基本上是公共循环遍历所有属性并读取每行的名称和val.我可以解决所有问题的唯一方法是使用name的值再次查询xml.这种感觉对我来说不对,有没有更好的方法呢?
List<String> names = config.getList("attrs/attr/@name");
for( String name : names )
{
String val = config.getString("attrs/attr[@name='" +name +"']/@val" );
System.out.println("name:" +name +" VAL:" +val);
}
Run Code Online (Sandbox Code Playgroud)
此外转换顶部到String,我不确定正确的方法来处理它.