Java中使用cmd复制文件的问题
我正在尝试使用 copy
命令将一堆具有特定扩展名的文件从一个文件夹复制到另一个文件夹,这是我正在做的事情,
String[] command = new String[3];
command[0] = "cmd";
command[1] = "/c";
command[2] = "copy C:\\output\\html\\*.txt C:\\output\\";
ProcessBuilder copyFiles = new ProcessBuilder(command);
p = copyFiles.start();
p.waitFor();
问题是,此代码适用于小于 5 个文件左右,但当文件数量较多时(即使是 15 个文件)就会停止响应!并且文件也没有被复制! 我不知道问题是什么,如果有人能提供帮助,我会很高兴! :)
I'm trying to copy a bunch of files with a specific extension from one folder to another using the copy
command, heres wat im doing,
String[] command = new String[3];
command[0] = "cmd";
command[1] = "/c";
command[2] = "copy C:\\output\\html\\*.txt C:\\output\\";
ProcessBuilder copyFiles = new ProcessBuilder(command);
p = copyFiles.start();
p.waitFor();
the thing is, this code works fine for files less than some 5 or so, but just stops responding wen the number of files are more (even for 15 files) !! and the files are not copied either!!
I dont know what the problem is, will be glad if someone could help! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有读取
copy
命令生成的输出。使用 ProcessBuilder 生成子进程时,子进程生成的输出将写入缓冲区。如果不读取该缓冲区,它最终会被填满。当它填满时,
copy
命令无法再向其中写入任何内容,因此会被操作系统阻止。然后,它被迫等待,直到通过读取缓冲区在缓冲区中腾出空间。我用 20 个文件运行了你的代码,发现它确实挂起了。
解决问题的一种方法是将输出从
copy
重定向到NUL
。copy
的大部分输出是它已复制的所有文件的列表,您可能不太关心它。要执行此重定向,请将分配给command[2]
的行修改为以下内容:但是,如果复制文件时出现问题,您可能不会知道这一点。
或者,您可以读取
copy
命令生成的输出。以下代码将其发送到 System.out,但您可以轻松地将其发送到其他地方,或者如果您愿意,可以完全忽略它:我使用相同的 20 个文件对每种方法进行了快速测试,并且两种方法都没有挂起。
编辑:您可能还想尝试“混合”方法,即丢弃
copy
写入标准输出的内容(例如,它正在复制的文件列表),但使用第二个读取写入标准错误的内容(例如错误消息)的方法。为此,您需要添加>NUL
,它将copy
的标准输出重定向到NUL
,但您不会添加2>NUL
,因为它将标准错误重定向到NUL
。You're not reading the output the
copy
command is generating.When spawning a child process using ProcessBuilder, output generated by your child process gets written to a buffer. If this buffer isn't read from, it eventually fills up. When it fills up, the
copy
command can't write any more to it and so is blocked by the operating system. It is then forced to wait until space is made in the buffer by reading from it.I ran your code with 20 files and I found that it did indeed hang.
One way to solve your problem is to redirect the output from
copy
toNUL
. Most of the output fromcopy
is a list of all the files it has copied, which you probably don't care too much for. To do this redirection, modify the line that assigns tocommand[2]
to the following:However, if there is a problem copying files, you might not know about it if you do this.
Alternatively, you can read the output that the
copy
command generates. The following code sends it toSystem.out
, but you can easily send it elsewhere or completely ignore it if you wish:I gave each approach a quick test with the same 20 files and neither approach hung.
EDIT: You might also want to try a 'hybrid' approach, by throwing away what
copy
writes to standard output (e.g. the list of files it's copying) but using the second approach to read in what it writes to standard error (e.g. error messages). To do this, you'd add the>NUL
, which redirects the standard output ofcopy
toNUL
, but you wouldn't add the2>NUL
, since that redirects standard error toNUL
.