Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java问题中的wc命令计算行数_Java_Wc - Fatal编程技术网

使用java问题中的wc命令计算行数

使用java问题中的wc命令计算行数,java,wc,Java,Wc,我想使用java计算文件的行数,我使用的代码是: String[] wcCommand = new String[]{"/usr/bin/wc","-l" , filePath}; Process wcProcess=Runtime.getRuntime().exec(wcCommand); wcProcess.waitFor(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader( wcProces

我想使用java计算文件的行数,我使用的代码是:

String[] wcCommand = new String[]{"/usr/bin/wc","-l" , filePath};
Process wcProcess=Runtime.getRuntime().exec(wcCommand);
wcProcess.waitFor();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader( wcProcess.getInputStream()));
String numberOfRowsline;
while((numberOfRowsline = bufferedReader.readLine()) != null) {
      System.out.print(numberOfRowsline);
}
如果我有一个5000000行的大文件,那么这个命令返回的数字是错误的。为什么会发生这种情况?当我直接从终端执行命令时,返回的值是正确的

java的输出是:3288079/PathToFile


terminal:5000000/PathToFile的输出

代码返回什么?请显示从shell和程序运行的
wc
的一些实际输出。撇开我认为您可以在Java中轻松完成上述操作而不产生进程这一事实不谈,我无法复制您的问题(无可否认,使用Scala控制台)什么数据类型是
numberOfRowsline
请尝试打印错误流