简体   繁体   中英

Runtime.getRuntime.exec() problem with copying files

I want to copy a file using this :

Process process = Runtime.getRuntime()
     .exec("cmd.exe /c  copy  C:\test1\toto.PDF  C:\test2\toto.PDF");

When i execute the command manually, it works, but when i tried to do it from my IDE, nothing happened. can someone tell me what is wrong with this please.

thanks.

You will need to double the backslashes; \\t by itself translates to a tab character.

(You were rather unlucky here, as if your path had been different, you might have got a compiler error that gave you a hint.)

Your immediate error is that \\t is a tab character. You forgot to double the backslashes, so the file names got mangled. However, as others have suggested, use Commons IO-Utils to do the copy.

Here is a longer article about pitfalls with runtime.exec Is copy a built-in of cmd.exe, or a separat executable?

I would cut the string into parts, to avoid misinterpretation of blanks/tabs:

"cmd.exe", "/c", "copy", "C:\test1\toto.PDF", "C:\test2\toto.PDF"

But this is all very platform dependend. You should read the file with java and write it to the target location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM