Java中使用cmd复制文件的问题

发布于 2024-11-04 09:42:01 字数 430 浏览 3 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春有你 2024-11-11 09:42:01

您没有读取 copy 命令生成的输出。

使用 ProcessBuilder 生成子进程时,子进程生成的输出将写入缓冲区。如果不读取该缓冲区,它最终会被填满。当它填满时,copy 命令无法再向其中写入任何内容,因此会被操作系统阻止。然后,它被迫等待,直到通过读取缓冲区在缓冲区中腾出空间。

我用 20 个文件运行了你的代码,发现它确实挂起了。

解决问题的一种方法是将输出从 copy 重定向到 NULcopy 的大部分输出是它已复制的所有文件的列表,您可能不太关心它。要执行此重定向,请将分配给 command[2] 的行修改为以下内容:

command[2] = "copy C:\\output\\html\\*.txt C:\\output\\ >NUL 2>NUL";

但是,如果复制文件时出现问题,您可能不会知道这一点。

或者,您可以读取 copy 命令生成的输出。以下代码将其发送到 System.out,但您可以轻松地将其发送到其他地方,或者如果您愿意,可以完全忽略它:

String[] command = { "cmd", "/c", "copy C:\\output\\html\\*.txt C:\\output\\" };
ProcessBuilder copyFiles = new ProcessBuilder(command);
copyFiles.redirectErrorStream(true);
p = copyFiles.start();

// The InputStream we get from the Process reads from the standard output
// of the process (and also the standard error, by virtue of the line
// copyFiles.redirectErrorStream(true) ).
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
do {
    line = reader.readLine();
    if (line != null) { System.out.println(line); }
} while (line != null);
reader.close();

p.waitFor();

我使用相同的 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 to NUL. Most of the output from copy 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 to command[2] to the following:

command[2] = "copy C:\\output\\html\\*.txt C:\\output\\ >NUL 2>NUL";

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 to System.out, but you can easily send it elsewhere or completely ignore it if you wish:

String[] command = { "cmd", "/c", "copy C:\\output\\html\\*.txt C:\\output\\" };
ProcessBuilder copyFiles = new ProcessBuilder(command);
copyFiles.redirectErrorStream(true);
p = copyFiles.start();

// The InputStream we get from the Process reads from the standard output
// of the process (and also the standard error, by virtue of the line
// copyFiles.redirectErrorStream(true) ).
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
do {
    line = reader.readLine();
    if (line != null) { System.out.println(line); }
} while (line != null);
reader.close();

p.waitFor();

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 of copy to NUL, but you wouldn't add the 2>NUL, since that redirects standard error to NUL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文