How to copy an image file.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moizpalitanawala
    New Member
    • Jul 2008
    • 14

    How to copy an image file.

    Hello friends,
    I want to copy files of extensio .bmp , .png, .tif , etc.

    when I execute the following code i get errors in the file copied.

    FileReader fr=new FileReader("D:/image.bmp");
    BufferedReader br=new BufferedReader( fr);
    FileReader fw=new FileWriter("D:/copyImage.bmp") ;
    String s;
    while((s=br.rea dLine())!=null)
    {
    fw.write(s);
    fw.write(System .getProperty("l ine.separator") );
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Don't use Readers and Writers for binary files; use InputStreams and OutputStreams
    instead. The first pair handles characters and do quite a bit of conversion (UTF-8
    etc.) The second pair handles simple bytes and don't do anything with them.

    kind regards,

    Jos

    Comment

    • sukatoa
      Contributor
      • Nov 2007
      • 539

      #3
      You may also try FileChannel class together with FileInputStream and FileOutputStrea m.

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Note, by the way, there's nothing special about this being an "image" file. You question was general to any binary file: for example, how to copy .class files.

        Comment

        • moizpalitanawala
          New Member
          • Jul 2008
          • 14

          #5
          Thanks for your guidance.
          My problem for copying images is solved.
          I had used this code for doing that.

          FileInputStream fis=new FileInputStream (new File("D:/image.bmp"));
          FileOutputStrea m fos=new FilePutputStrea m(new FIle("D:/copyImage.bmp") );
          int c;
          while((c=fis.re ad())!=-1)
          {
          fos.write(c);
          }

          But one more problem arises. The Speed is very slow. When I use this to copy more than 1000-1200 files i.e. more than 200mb It takes around 4-5 min.
          spending this much time is not feasible for me.

          Is there any other way by which copying of files becomes faster.

          Moiz

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            That way will be painfully slow -- you are copying the file directly, one byte at a time. Take a look at the InputStream/OutputStream methods that take an array of bytes. 8K is a good array size -- your code should be 8 thousand times faster!

            Sukatoa's suggestion to use file channels is excellent as well. Did you follow his links and read?

            Comment

            • sagarsuman00
              New Member
              • Sep 2017
              • 1

              #7
              Originally posted by moizpalitanawal a
              Thanks for your guidance.
              My problem for copying images is solved.
              I had used this code for doing that.

              FileInputStream fis=new FileInputStream (new File("D:/image.bmp"));
              FileOutputStrea m fos=new FilePutputStrea m(new FIle("D:/copyImage.bmp") );
              int c;
              while((c=fis.re ad())!=-1)
              {
              fos.write(c);
              }

              But one more problem arises. The Speed is very slow. When I use this to copy more than 1000-1200 files i.e. more than 200mb It takes around 4-5 min.
              spending this much time is not feasible for me.

              Is there any other way by which copying of files becomes faster.

              Moiz


              if the size of the file is odd bytes (suppose 51 bytes), how it will successfully copy using integers (because integer is of 4 bytes)?

              Comment

              Working...