Java write file read and write operations (IO stream, character stream)

Source: Internet
Author: User

Package Copyfile;import java.io.*;p ublic class Copy {public static void main (string[] args) throws IOException {CopyFile ( "D:/new/a.txt", "D:/new/b.txt", true);//oldpath,newpath, does not overwrite the preceding}public static void CopyFile (String oldpth,string Newpath,boolean add) throws Ioexception{filereader fr = null; FileWriter FW = null;try {///instantiate the file, and determine if the file exists with the files Oldfile=new file (oldpth); if (Oldfile.exists ()) {//Initialize file input and output stream fr=new FileReader (oldpth); fw=new FileWriter (Newpath,add);//define an array to hold read data char[] buffer=new char[10];int Length;while (True) { int len=fr.read (buffer);//When the file is read, return-1, otherwise return the read file length if (len==-1) break;fw.write (buffer);} System.out.println ("OK");}} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Finally{fr.close (); Fw.close ();}}}

  

Java write file read and write operations (IO stream, character stream)

Related Article

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.