The file IO stream completes file replication (the complex version is mainly used to demonstrate the usage of various streams, not the best replication solution)

Source: Internet
Author: User

Package io;

Import java. io. BufferedReader;
Import java. io. BufferedWriter;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStreamReader;
Import java. io. OutputStreamWriter;

 

Public class FileReaderDemo {
Public static void main (String [] args ){

String path = "D :\\ eclipse \ workspace \ javase \ src \ io \ FileInputStreamDemo. java ";
String outpa = "c:/ganbo.txt ";

File f = new File ("D :\\ eclipse \ workspace \ javase \ src \ io \ FileInputStreamDemo. java ");
File f2 = new File (outpa );

// Node stream
FileInputStream fin = null;
FileOutputStream fo = null;

// Package stream
InputStreamReader inr = null;
OutputStreamWriter bpw = null;

// Buffer the stream
BufferedReader br = null;
BufferedWriter bw = null;


Try {
// Instantiate the input stream
Fin = new FileInputStream (f );
Inr = new InputStreamReader (fin );
Br = new BufferedReader (inr );

// Instantiate the output stream
Fo = new FileOutputStream (f2); // note that FileOutputStream () also has an overloaded method. The value true and false after the parameter indicates whether to append or overwrite data.
Bpw = new OutputStreamWriter (fo );
Bw = new BufferedWriter (bpw );


// Start copying
String temp;
While (temp = br. readLine ())! = Null ){
System. out. println (temp );

/**
* Note that a line is written to the file. By default, no line breaks are generated at the end of a line. You need to manually add "\ r \ n ";
* \ R indicates the carriage return, that is, moving the cursor to the starting position of the row.
* \ N indicates that the cursor is redirected to the next row of the current row.
* Generally, "\ r \ n" is used in combination. It serves as the enter key on the keyboard.
*/
Bw. write (temp + "\ r \ n ");
Bw. flush ();
}

} Catch (Exception e ){
E. printStackTrace ();
} Finally {

// Close the resource
If (br! = Null ){
Try {
Br. close ();
} Catch (IOException e ){
E. printStackTrace ();
} If (bw! = Null ){
Try {
Bw. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}
}




}
}

The file IO stream completes file replication (the complex version is mainly used to demonstrate the usage of various streams, not the best replication solution)

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.