Copy files with FileInputStream and FileOutputStream __eclipse

Source: Internet
Author: User
Tags array length
Create a new Abc.txt file in E:\JavaIO and write some text inside it. Now to do is, in Eclipse, with FileInputStream and

FileOutputStream the Read () and write () methods, implement, abc.txt the copy of the file to create a new Iouitl.java, the code is as follows:

Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;
public class Iouitl {public
	static void CopyFile (File src,file des) throws ioexception{
		if (!src.exists ()) {
			throw new IllegalArgumentException (src+ "original file does not exist");
		}
		if (!src.isfile ()) {
			throw new IllegalArgumentException (src+ "not a file")
		;
		FileInputStream in = new FileInputStream (SRC);
		The file does not exist directly created, the file is deleted and created
		fileoutputstream out = new FileOutputStream (DES);
		byte[] buf = new byte[8*1024];
		int b;
		In.read (buf,0,buf.length) will read data in BUF array, starting at 0 to array length while
		((B=in.read (buf,0,buf.length))!=-1) {
			Out.write (buf, 0, b);
			Out.flush ();
		}
		In.close ();
	}

Then define a Test.java class to test:

Import Java.io.File;
Import java.io.IOException;
Import Java.io.RandomAccessFile;
Import java.util.ArrayList;
Import Java.util.Arrays;

public class Test2  {public
	static void Main (string[] args) {
		file src = new File ("E:\\javaio\\abc.txt");
		File des = new file ("E:\\javaio\\abc1.txt");
			
	    try {
			iouitl.copyfile (src, des);
		} catch (IOException e) {
			//TODO auto-generated catch
			block E.printstacktrace ();}}


Run the results, open the file directory, you can see the Abc1.txt file, the contents of the same as abc.txt, the implementation of the copy




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.