/*
* Requirement: Job: Copy a text file of C drive to D drive.
*
Ideas
* 1, need to read the source,
* 2, writes the read source data to the destination.
* 3, since it is manipulating text data, use a character stream.
*
*/
public class Copytexttest {
/**
* @param args
* @throws IOException
*/
public static void Main (string[] args) throws IOException {
1. Read an existing text file, using the character read stream and the file associated.
FileReader FR = new FileReader ("IO stream _2.txt");
2, create a purpose for storing read data.
FileWriter FW = new FileWriter ("Copytext_1.txt");
3, frequent read and write operations.
int ch = 0;
while ((Ch=fr.read ())!=-1) {
Fw.write (CH);
}
4. Close the stream resource.
Fw.close ();
Fr.close ();
}
}
public class Copytexttest_2 {
private static final int buffer_size = 1024;
/**
* @param args
*/
public static void Main (string[] args) {
FileReader FR = null;
FileWriter FW = NULL;
try {
FR = new FileReader ("IO stream _2.txt");
FW = new FileWriter ("Copytest_2.txt");
Creates a temporary container to cache the characters that are read to.
char[] buf = new char[buffer_size];//This is the buffer.
Defines the number of characters that a variable record reads, (in fact, the number of characters to be loaded into the array)
int len = 0;
while ((Len=fr.read (BUF))!=-1) {
Fw.write (buf, 0, Len);
}
} catch (Exception e) {
System.out.println ("Read and Write Failed");
throw new RuntimeException ("Read and Write Failed");
}finally{
if (fw!=null)
try {
Fw.close ();
} catch (IOException e) {
E.printstacktrace ();
}
if (fr!=null)
try {
Fr.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}
Using decorative Flow
public class Bufferedreaderdemo {
/**
* @param args
* @throws IOException
*/
public static void Main (string[] args) throws IOException {
FileReader FR = new FileReader ("Buf.txt");
BufferedReader bufr = new BufferedReader (FR);
String line = null;
while ((Line=bufr.readline ())!=null) {
System.out.println (line);
}
/*
String line1 = Bufr.readline ();
System.out.println (line1);
String line2 = Bufr.readline ();
System.out.println (line2);
String line3 = Bufr.readline ();
System.out.println (LINE3);
String line4 = Bufr.readline ();
System.out.println (line4);
String line5 = Bufr.readline ();
System.out.println (Line5);
*/
Bufr.close ();
}
/**
* @throws FileNotFoundException
* @throws IOException
*/
public static void Demo () throws FileNotFoundException, IOException {
FileReader FR = new FileReader ("Buf.txt");
char[] buf = new char[1024];
int len = 0;
while ((Len=fr.read (BUF))!=-1) {
System.out.println (New String (Buf,0,len));
}
Fr.close ();
}
}
public class Bufferedwriterdemo {
private static final String Line_separator = System.getproperty ("Line.separator");
/**
* @param args
* @throws IOException
*/
public static void Main (string[] args) throws IOException {
FileWriter FW = new FileWriter ("Buf.txt");
In order to improve the efficiency of writing. A buffer that uses a character stream.
A buffer object that creates a character write stream, and is associated with the flow object specified to be buffered
BufferedWriter BUFW = new BufferedWriter (FW);
Use the Write method of the buffer to write the data to the buffer first.
Bufw.write ("Abcdefq" +line_separator+ "Hahahha");
Bufw.write ("XIXIIXII");
Bufw.newline ();
Bufw.write ("Heheheheh");
for (int x=1; x<=4; x + +) {
Bufw.write ("abcdef" +x);
Bufw.newline ();
Bufw.flush ();
}
Use the Refresh method of the buffer to brush the data to the destination.
Bufw.flush ();
Closes the buffer. In fact, the buffered stream object is closed.
Bufw.close ();
Fw.write ("hehe");
Fw.close ();
}
}
copy of text stream
public static void Main (string[] args) throws IOException {
FileReader FR = new FileReader ("Buf.txt");
BufferedReader bufr = new BufferedReader (FR);
FileWriter FW = new FileWriter ("Buf_copy.txt");
BufferedWriter BUFW = new BufferedWriter (FW);
String line = null;
while ((Line=bufr.readline ())!=null) {
Bufw.write (line);
Bufw.newline ();
Bufw.flush ();
}
/*
int ch = 0;
while ((Ch=bufr.read ())!=-1) {
Bufw.write (CH);
}
*/
Bufw.close ();
Bufr.close ();
}
Io Stream Demo