Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file copying

Status
Not open for further replies.

fenris

Programmer
May 20, 1999
824
CA
I have looked on the internet in a couple of places (namely java.sun.com) to find some examples on how to copy files from one place to another on a local machine. I would prefer not to use calls to the local operating system to do it as it would affect protability. I have seen some methods that involve using the buffered input/output streams to read chunks of the file at a time. <br><br>I need something that is efficient and fast because I will be copying several hundred files of 5mb in average length between directories.<br><br>Any suggestions or input would be greatly appreciated. Thanks <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br> I am interested in Mining Software, as well as Genetic Algorithms.
 
I guess you might read in a file, and then save it as another file- you'd need to have a name-generating method. But you already knew that- what exactly do you need to know? <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence."
 
What I want is to be able to copy files efficently over a network drive, maybe using multiple threads. Here is what I have so far and it works and suggestions for improvement would be appreciated:<br><br>import java.io.*;<br><br><br>public class CopyFile<br>{<br> public static void main(String[] args) throws IOException<br> {<br> new CopyFile(args);<br> }<br><br> public CopyFile(String args[]) throws IOException<br> {<br> byte[] buff = new byte[1048576];<br> BufferedInputStream bis;<br> BufferedOutputStream bos;<br><br> <br><br> if (args.length &lt; 2)<br> System.out.println(&quot;Command Syntax: CopyFile &lt;source&gt;&lt;dest&gt;\n\n&lt;source&gt;\tThe source File name (path optional)\n&lt;dest&gt;\tThe destination file name (path optional)&quot;);<br> else<br> try<br> {<br> bis = new BufferedInputStream(new FileInputStream(args[0])); //source<br> bos = new BufferedOutputStream(new FileOutputStream(args[1])); //destination<br><br> //determine the size of the buffer to allocate<br> //File source = new File(args[0]);<br> //int fileLength = (int) source.length();<br> int size;<br> <br> while ((size = bis.read(buff)) &gt; -1){bos.write(buff,0,size);}<br><br> bis.close();<br> bos.close();<br> <br> }<br> catch(FileNotFoundException fnfe){System.out.println(args[0] + &quot; does not exist&quot;);}<br> catch(IOException ioe){System.out.println(&quot;Error Reading/Writing the File&quot;);}<br> <br> <br> <br><br> }<br><br>}<br><br> <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br> I am interested in Mining Software, as well as Genetic Algorithms.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top