Write a method to copy the file

Source: Internet
Author: User

In Java to implement a copy within a file, you need to create a new file method:

File File=new file ("Wubin.txt");

And without this file, you need to create this file:

File.createnewfile ();

Of course, you can create directly in the file stream directly:

FileInputStream fis=new FileInputStream ("Wubin.txt");

This means creating a Wubin.txt file in this directory, then creating a inputstreamreader to get the content, and then using a buffered bufferreader to do a speed optimization:

InputStreamReader isr=new InputStreamReader (FIS);

bufferedreader br=new BufferedReader (ISR);

Finally, the Read method of BR is used.

Well, if we're going to give the read to a new file, that is, create a file that will give the file the contents of a new file:

FileOutputStream fos=new FileOutputStream ("Wubin1.txt");

outputstreamwriter opw=new outputstreamwriter (FOS);

bufferedwriter bw=new bufferedwriter (OPW);

Finally, the BW write method is written in.

The problem comes, responsible for a file can also, if it is more than a file to be responsible, very cumbersome, and troublesome,

So I wrote a copy of the method, so that the operation of the simple words, define a new class, in the class, define a new method copy{}:

Class copa{

public void copy (File file,file file1,string s1,string s2) {

try {

file=new file (S1);

file.createnewfile ();

file1=new File (S2);

file1.createnewfile ();

} catch (IOException E2) {

e2.printstacktrace ();

}

try {

fileinputstream fis=new fileinputstream (file);

inputstreamreader isr=new inputstreamreader (FIS);

BufferedReader br= new BufferedReader (ISR);

//shangmians read out, here is the write

fileoutputstream fos=new fileoutputstream (file1);

outputstreamwriter opw=new outputstreamwriter (FOS);

bufferedwriter bw=new bufferedwriter (OPW);

char[] b=new char[100];

try {

Br.read (b);

// System.out.println (b);

} catch (IOException e) {

e.printstacktrace ();

}

try {

Bw.write (new String (b));

} catch (IOException E1) {

e1.printstacktrace ();

}

try {

bw.close ();

br.close ();

isr.close ();

fis.close ();

} catch (IOException e) {

e.printstacktrace ();

}

} catch (FileNotFoundException e) {

e.printstacktrace ();

}

}

}

Novice to explore, new method copy, defined two folder File,file1, defined two file name: S1,S2

To invoke in the Main method:

File File=new file ("");

File File3=new file ("");

Copa Copa=new Copa ();

Copa.copy (file, File3, "Jixixin.txt", "chengxia.txt");

System.out.println ("successfully created");

After that, a successful copy is made, and a encapsulated method is implemented to make the file copy simple.

Write a method to copy the file

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.