10 Replies Latest reply: Jun 12, 2007 8:34 AM by 807605 RSS

    copying a file

    807605
      can anyone please let me know how to copy a file from location(i.e., d: drive) to a network drive (present on some system connected thru lan). And I am working on linux environment.
        • 1. Re: copying a file
          807605
          To be able to copy successfully, the other computer needs to have given you the permissions to write into the shared folder. Then use the java io package to copy. For instance you can create a FileOutputStream object to the file as
          File file = new File("//smb/myworkgroup/remotecomputer/remoteshare/folder/file.txt");
          FileOutputStream fos = new FileOutputStream(file);
          //etc
          fos.close();
          • 2. Re: copying a file
            807605
            public class copy {
            public static void main(String[] args) throws IOException {
            File inputFile = new File("//smb/SVR1/example/team/test.txt");
            File outputFile = new File("/home/test/test123/java_test_pgms/file.txt");

            FileReader in = new FileReader(inputFile);
            FileWriter out = new FileWriter(outputFile);
            int c;

            while ((c = in.read()) != -1)
            out.write(c);

            in.close();
            out.close();
            }
            }

            getting an exception


            Exception in thread "main" java.io.FileNotFoundException
            • 3. Re: copying a file
              800322
              So some file doesn't exist. Did you read the message?
              • 4. Re: copying a file
                807605
                Hello

                File inputFile = new
                File("//smb/SVR1/example/team/test.txt");
                Here the folders smb, SVR1, example, team should exist in the disk. test.txt,
                if not exist will be created. Similarly for the outputFile.
                • 5. Re: copying a file
                  807605
                  hi

                  Here the smb means a network drive(samba drive) which is located on different system, it is not in the local system.
                  • 6. Re: copying a file
                    807605
                    I'm no linux expert, but "//smb/SVR1/example/team/test.txt" looks like a very strange file name. Surely you would mount the share (Samba or otherwise) and then access it with a normal looking file name like "/mnt/smb/SVR1/example/team/test.txt".

                    Have you tried copying some data to the test.txt file from the command line? What commands do you use for that?

                    [Edit] java.io.File on a Windows platform accepts names like \\host\smb\SVR1\example\team\test.txt
                    • 7. Re: copying a file
                      807605
                      \\host\smb\SVR1\example\team\test.txt
                      does'nt work when we use it as a parameter in File object
                      • 8. Re: copying a file
                        807605
                        The Windows-share-filename-magic does not work in Linux.

                        Using Samba you can access shared forders and mount them, and access them with the same syntax as if they were local.

                        When the remote shared folder is mounted to, say, /mnt/smb1, then you can access the file using the name
                        /mnt/smb1/SVR1/example/team/test.txt
                        • 9. Re: copying a file
                          807605
                          And to mount the samba share, do
                          smbmount //smb/<remotecomputer>/<remoteshare> <mount point> -o username=<your user name>
                          then press return or enter. You may be prompted for a password

                          for example
                          smbmount //smb/link120/sandman/jukebox /mnt/media -o username=root
                          • 10. Re: copying a file
                            807605
                            >
                            FileReader in = new FileReader(inputFile);
                            FileWriter out = new FileWriter(outputFile);
                            int c;
                            while ((c = in.read()) != -1)
                            out.write(c);
                            in.close();
                            out.close();
                            }
                            }
                            I've tried it a couple of ways and it seems be be a lot faster to do it in bufferfulls as in (say)
                            byte buff= new byte[2048];
                            int len;
                            while((len = in.read(buf)) > 0)
                               out.write(buf, 0, len);
                            It a pity they didn't put a copy method into OutputStream and Writer, in my opinion.