How to copy text files in Java I summed up 14 kinds (by character read 4, read by byte 8 kinds!?? )

Source: Internet
Author: User

How to copy files in Java

If you read by character, there can be 4 kinds, basic 2 kinds, efficient 2 kinds, efficient special 1 kinds

The No. 0 Type:

public class Copyfiledemo {public static void main (string[] args) throws exception{//packaging data source BufferedReader reader = new Buff Eredreader (New FileReader ("A.txt"));//package destination BufferedWriter writer = new BufferedWriter (New FileWriter ("B.txt"));// Read and write data string line = Null;while ((Line=reader.readline ())!=null) {writer.write (lines); Writer.newline ();//Add New row, Otherwise, the contents of the file will not be wrapped Writer.flush ();} Release resources Writer.close (); Writer.close ();}}


First: Join buffer buffered read one character array at a time

public class Copyfiletest {public static void main (string[] args) throws exception{//encapsulated data source BufferedReader reader = new Buff Eredreader (New FileReader ("A.txt"));//package destination BufferedWriter writer = new BufferedWriter (New FileWriter ("B.txt"));// One of two ways to read one character array at a time char[] chs = new Char[1024];int Len=0;while ((Len=reader.read (CHS))!=-1) {writer.write (Chs,0,len); Writer.flush ();//Remember to refresh}//release Resources writer.close (); Reader.close ();}}

Second: Add buffer buffered read one character at a time

public class Copyfiletest {public static void main (string[] args) throws exception{//encapsulated data source BufferedReader reader = new Buff Eredreader (New FileReader ("A.txt"));//package destination BufferedWriter writer = new BufferedWriter (New FileWriter ("B.txt"));// Reads one character at a time, int ch=0;while ((Ch=reader.read ())!=-1) {writer.write (ch);} Release resources Writer.close (); Reader.close ();}}

The third type: normal one-time reading of a character array

public class Bufferedcopyfiletest {public static void main (string[] args) throws exception{//encapsulated data source FileReader reader = new FileReader ("A.txt");//package destination FileWriter writer = new FileWriter ("B.txt");//read one character array at a time char[] chs = new Char[1024];int len= 0;while (Len=reader.read (CHS))!=-1) {writer.write (Chs,0,len); Writer.flush ();} Writer.close (); Reader.close ();}}

Third: Normal read one character at a time

public class Bufferedcopyfiletest {public static void main (string[] args) throws exception{//encapsulated data source FileReader reader = new FileReader ("A.txt");//package destination FileWriter writer = new FileWriter ("B.txt");//read one character at a time int ch=0;while ((Ch=reader.read ())! =-1) {writer.write (ch);} Writer.close (); Reader.close ();}}

If you read in bytes, you can have 8

First: Convert the byte stream to a character stream to read one of the characters at a time

public class Copy {public static void main (string[] args) throws Exception {//Encapsulated data source (converts a byte stream to a character stream) InputStreamReader ISR = new InputStreamReader (New FileInputStream ("A.txt"));//Package destination (convert byte stream to character stream) OutputStreamWriter OSW = new OutputStreamWriter ( New FileOutputStream ("B.txt"));//read one character at a time int ch=0;while ((Ch=isr.read ())!=-1) {osw.write (ch);} Close resource Osw.close (); Isr.close ();}}

Second: Convert a byte stream to a character stream to read an array of characters at a time

public class Copy {public static void main (string[] args) throws Exception {//Encapsulated data source (converts a byte stream to a character stream) InputStreamReader ISR = new InputStreamReader (New FileInputStream ("A.txt"));//Package destination (convert byte stream to character stream) OutputStreamWriter OSW = new OutputStreamWriter ( New FileOutputStream ("B.txt"));//read one character array at a time char[] chs = new Char[1024];int Len=0;while ((Len=isr.read (CHS))!=-1) { Osw.write (Chs,0,len); Osw.flush ();} Close resource Osw.close (); Isr.close ();}}
The third type: efficient byte stream reads and writes an array of bytes at a time:

public static void Method4 (String srcstring, String deststring)                  throws IOException {              Bufferedinputstream bis = NE W Bufferedinputstream (New FileInputStream (                      srcstring));              Bufferedoutputstream BOS = new Bufferedoutputstream (                      new FileOutputStream (deststring));                    byte[] bys = new byte[1024];              int len = 0;              while (len = Bis.read (bys))! =-1) {                  bos.write (bys, 0, Len);              }                    Bos.close ();              Bis.close ();          }  

Fourth: Efficient byte stream reads one character at a time:

public static void Method3 (String srcstring, String deststring)                  throws IOException {              Bufferedinputstream bis = NE W Bufferedinputstream (New FileInputStream (                      srcstring));              Bufferedoutputstream BOS = new Bufferedoutputstream (                      new FileOutputStream (deststring));                    int by = 0;              while (by = Bis.read ())! =-1) {                  bos.write (by);                    }                    Bos.close ();              Bis.close ();          }  

Fifth: Ordinary byte-stream objects read one array of bytes at a time

public static void Method2 (String srcstring, String deststring)                  throws IOException {              FileInputStream fis = new Fil Einputstream (srcstring);              FileOutputStream fos = new FileOutputStream (deststring);                    byte[] bys = new byte[1024];              int len = 0;              while (len = Fis.read (bys))! =-1) {                  fos.write (bys, 0, Len);              }                    Fos.close ();              Fis.close ();          }        

Sixth type: Normal byte stream object reads one bytes at a time

public static void Method1 (String srcstring, String deststring)                  throws IOException {              FileInputStream fis = new Fil Einputstream (srcstring);              FileOutputStream fos = new FileOutputStream (deststring);                    int by = 0;              while (by = Fis.read ())! =-1) {                  fos.write (by);              }                    Fos.close ();              Fis.close ();          }  
The seventh type:BufferedReader reader = new BufferedReader (new InputStreamReader (New FileInputStream ("A.txt"));
BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (New FileOutputStream ("B,txt")), then one character at a time

Eighth:BufferedReader reader = new BufferedReader (new InputStreamReader (New FileInputStream ("A.txt"));
BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (New FileOutputStream ("B,txt")), and one character array at a time


Note that copying pictures, video, audio, only 4 ways (byte stream reading), cannot be read with a character stream

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

How to copy text files in Java I summed up 14 kinds (by character read 4, read by byte 8 kinds!?? )

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.