How To Copy The File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • karthickkuchanur
    New Member
    • Dec 2007
    • 156

    How To Copy The File

    Dear All ,
    May i know how to copy the file from source to destination folder in java
    if it is any class available in java

    Or i have read the Source File from content and write it into a destination folder file
    if it is any class for copying the files means it very useful for me
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Open the API specs for the File class and check the methods there.

    Comment

    • karthickkuchanur
      New Member
      • Dec 2007
      • 156

      #3
      Originally posted by r035198x
      Open the API specs for the File class and check the methods there.
      There no such method to copy the file from source to destination

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by karthickkuchanu r
        There no such method to copy the file from source to destination
        If reading that little bit of API documentation didn't give you that tickling feeling
        of curiosity to read more then programming is absolutely not for you.

        kind regards,

        Jos

        Comment

        • karthickkuchanur
          New Member
          • Dec 2007
          • 156

          #5
          Originally posted by JosAH
          If reading that little bit of API documentation didn't give you that tickling feeling
          of curiosity to read more then programming is absolutely not for you.

          kind regards,

          Jos
          Iam more confident that iam not able to do with file class iam right

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by karthickkuchanu r
            Iam more confident that iam not able to do with file class iam right
            Ok, you're about sure that you can't copy a file by just using the File class. And
            now what? Are you simply going to wait until somebody else takes you by the
            hand and solves the problem for you? If so, programming is not for you. Go and
            read the API documentation; the solution is in there; hint: FileInputStream and
            FileOutputStrea m.

            Jos

            Comment

            • chaarmann
              Recognized Expert Contributor
              • Nov 2007
              • 785

              #7
              Originally posted by karthickkuchanu r
              Iam more confident that iam not able to do with file class iam right
              There is no ready-made method that copies a file, as far as I know.
              Maybe because it's so short and easy to do it yourself.

              More hints:
              If you already have defined the fileInputStream and fileOutputStrea m and the "byte[] buffer = new byte[4096]", and "int bytesRead", then you need only 2 lines more of source code to copy the file, using a while-loop. hint: read about read() and write() methods of the streams!

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by karthickkuchanu r
                Iam more confident that iam not able to do with file class iam right
                There is a method called renameTo. What does it do? How can you use it to simulate a copy in a few steps?

                Comment

                • karthickkuchanur
                  New Member
                  • Dec 2007
                  • 156

                  #9
                  Originally posted by chaarmann
                  There is no ready-made method that copies a file, as far as I know.
                  Maybe because it's so short and easy to do it yourself.

                  More hints:
                  If you already have defined the fileInputStream and fileOutputStrea m and the "byte[] buffer = new byte[4096]", and "int bytesRead", then you need only 2 lines more of source code to copy the file, using a while-loop. hint: read about read() and write() methods of the streams!
                  Thanks a lot
                  Code:
                  public class MovingFile{
                   
                    public static void main(String[] args) throws IOException{
                   
                  	  
                  	        File srFile = new File("/home/check/Backup/comm.jar");
                  	        File dtFile = new File("/home/check/Screen Shots/comm.jar");
                  	       
                  	        if (srFile.exists()) {
                  	         InputStream inputStream = new FileInputStream(srFile);
                  	         
                  	         if (!dtFile.exists()) {
                  	          OutputStream outputStream = new FileOutputStream(dtFile);
                  	          
                  	        byte[] buf = new byte[1024];
                  	        int len;
                  	       
                  	        while ((len = inputStream.read(buf)) > 0) {
                  	         outputStream.write(buf, 0, len);
                  	        }
                  	        outputStream.close();
                  	       
                  	         } else {
                  	          System.out.println("Designation File already Exist...");
                  	         }
                  	       inputStream.close();
                  	      
                  	        } else {
                  	         System.out.println("Source File is not Exist...");
                  	        }
                  	         
                  	    }
                  }
                  Last edited by karthickkuchanur; Nov 6 '08, 04:38 AM. Reason: changeds in code

                  Comment

                  Working...