• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Read and Write

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i read a jpg file from the local disk and write it to another location

Thanks & Regards
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am writing a sample code to copy files of smaler size. Customise the programe to your need...


import java.io.*;

public class FileCopy{
public static void main(String args[]) throws Exception{
String srcLoc = "C:\\test\\test.txt";
String destLoc = "C:\\test1\\test.txt";


File srcfile = new File(srcLoc);
File destFile = new File(destLoc);


System.out.println("Copying file......");

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcfile));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
int totalsize = bis.available();
byte[] b = new byte[totalsize];
bis.read(b,0, totalsize);
bos.write(b,0,totalsize);
bos.flush();
bis.close();
bos.close();
System.out.println("File Copied....");
}
}




-Arun
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not convinced that code works in all cases -- availible for FileInputStream doesn't claim to return the entire length of the file, so I think you're going to need to write a loop. Also, it's a bit "overbuffered": you buffer the input and output stream and then put a third byte array between them -- that's three buffers! Here are two solutions from Java Almanac, the second uses FileChannels from the "new IO":

http://javaalmanac.com/egs/java.io/CopyFile.html
http://javaalmanac.com/egs/java.nio/File2File.html
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I totally agree Jeff Albertson. Hence if you see my first line of statment i have written this code works only for smaller file size...
Thanks for the links for quick references....
    Bookmark Topic Watch Topic
  • New Topic