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)