The instance describes Java IO file replication, and the instance describes javaio

Source: Internet
Author: User

The instance describes Java IO file replication, and the instance describes javaio

IO streams are mainly divided into two categories: byte streams and byte streams.

Note:

1. audio files, images, and videos (with a wide range) are throttled by words

2. if it involves only the text, the ghost stream is used.

Use byte streams to copy text content (other files can also be used)

The Code is as follows:

Import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; public class CopyFile {public static void main (String [] args) {try {File inFile = new File ("src.txt "); // source File InputStream in = new FileInputStream (inFile); // File input stream File outFile = new File ("tar.txt"); // target File OutputStream out = new FileOutputStream (outFile); // file output stream byte [] buff = new byte [1024]; // create a buffer, allocate a memory of 1024 bytes to the buff int num = 0; while (num = in. read (buff ))! =-1) {// determines whether the maximum number of bytes read each time is 1024 out. write (buff, 0, num); // out. write (buff); // This can also be done, but the file content may be incorrect out. flush (); // refresh the buffer String s = new String (buff); System. out. println ("File Content copied"); System. out. println ("file copy content \ n" + s);} in. close (); // close the input stream out. close (); // close the output stream // inFile. delete (); // delete the source file // outFile. renameTo (new File ("reName.txt"); // rename the output File} catch (FileNotFoundException e) {e. printStackTrace (); // print the location and cause of the exception information in the program on the command line} catch (IOException e) {e. printStackTrace ();}}}

Use the text stream to copy text content (text files only)

The Code is as follows:

Import java. io. bufferedReader; import java. io. bufferedWriter; import java. io. file; import java. io. fileNotFoundException; import java. io. fileReader; import java. io. fileWriter; import java. io. IOException; public class CopyFile {public static void main (String [] args) {try {File inFile = new File ("D:/src.txt "); // The source file FileReader fileReader = new FileReader (inFile); // read the object metadata stream BufferedReader bufferedReader = new Buff EredReader (fileReader); // put the read shard stream into the cache File outFile = new File ("D:/tar.txt "); // target file FileWriter fileWrite = new FileWriter (outFile); // write the BufferedWriter bufferedWriter BufferedWriter = new BufferedWriter (fileWrite ); // put the stream into the cache String s = ""; String str = ""; while (s = bufferedReader. readLine ())! = Null) {// checks whether the object's primary stream has been read. bufferedWriter. write (s); // write the bufferedWriter file to the cached bytes stream. newLine (); // read by line. In case of a line break, the bufferedWriter line feed is used. flush (); // refresh the cache str + = s;} System. out. println ("file copy content \ n" + str); // close the input/output stream fileReader. close (); bufferedReader. close (); fileWrite. close (); bufferedWriter. close (); // inFile. delete (); // delete the source file // outFile. renameTo (new File ("reName.txt"); // rename the output File} catch (FileNotFoundException e) {e. printStackTrace (); // print the location and cause of the exception information in the program on the command line} catch (IOException e) {e. printStackTrace ();}}}

Note:

If you want to copy the text content by appending the file content instead of overwriting it, modify the following:

OutputStream out = new FileOutputStream (outFile, true); // byte stream
FileWriter fileWrite = new FileWriter (outFile, true); // delimiter stream

If you want to specify the cache area size in the response stream, modify the following:

BufferedWriter bufferedWriter = new BufferedWriter (fileWrite, 1024 );

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.