2 Replies Latest reply on Mar 14, 2008 8:08 AM by 807591

    Copying a file help please!

      Hello all, im trying to find a way to backup a file using command prompt.
      Runtime.getRuntime().exec("copy c:\\test.test c:\\users\\xxx\\my documents\\testBackup.test);{code}
      i get this error
      Exception in thread "main" java.io.IOException: Cannot run program "Copy": CreateProcess error=2, The system cannot find the file specified
           at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
           at java.lang.Runtime.exec(Runtime.java:593)
           at java.lang.Runtime.exec(Runtime.java:431)
           at java.lang.Runtime.exec(Runtime.java:328)
           at NewClass.main(NewClass.java:7)
      Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
           at java.lang.ProcessImpl.create(Native Method)
           at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
           at java.lang.ProcessImpl.start(ProcessImpl.java:30)
           at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
           ... 4 more
      {code}Runtime.getRuntime().exec("notepad c:\\test.test");{code}
      works perfectly any ideas??                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
        • 1. Re: Copying a file help please!
          Runtime.exec executes an executable file. Notepad.exe is one. Copy.exe does not exist, it's a shell command. So you're have to run something like "cmd copy ..." - and make sure you read up on any command line arguments for cmd you could use.

          All in all, copying a file like that is a pretty asinine idea altogether. Doing the same thing platform-independantly using Java ist just a matter of a few lines of code.
          • 2. Re: Copying a file help please!
            /* FileCopy.java */
            import java.io.*;
            public class FileCopy
              public static void main(String[] args)
                if (args.length != 2) {
                  System.out.println("java FileCopy inputfile outputfile");
                try {
                  FileInputStream in = new FileInputStream(args[0]);
                  FileOutputStream out = new FileOutputStream(args[1]);
                  byte[] buf = new byte[4096];
                  int len;
                  while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                } catch (IOException e) {