JAVA 48th-IO stream (2): File Replication & amp; buffer 1, javaio

Source: Internet
Author: User

JAVA 48th-IO stream (2): File Replication & Buffer Zone 1, javaio

1. Copy a text file

Copy the text file from disk G to disk D.

That is, read the data of text files in the G drive and write the data to drive D-> write-in-connection

The cut is to delete the file on the original disk after the original disk is read and written.

<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> <strong> import java. io. fileReader; import java. io. fileWriter; import java. io. IOException; public class Main {public static void main (String [] args) throws IOException {FileReader fr = null; FileWriter fw = null; try {fr = new FileReader ("gdisk .txt"); fw = new FileWriter ("D: \ ACM.txt"); char [] ch = new char [1024]; // int t; while (t = fr. read (ch ))! =-1) {fw. write (ch, 0, t) ;}} catch (Exception e) {System. out. println ("read/write failed");} finally {if (fw! = Null) {try {fw. close () ;}catch (Exception e2) {throw new RuntimeException () ;}} if (fr! = Null) {try {fr. close () ;}catch (Exception e2) {throw new RuntimeException () ;}}}</strong> </span>

If a file is in use, it cannot be deleted. Therefore, be sure to associate these two actions.


Ii. Shard Stream Buffer

In fact, the char [] array above is a buffer zone,The emergence of a buffer increases the reading efficiency of data.

BufferedWriter and BufferedReader

Prerequisite: the buffer must be combined with a stream before it can be used.

Role: the function of streaming is enhanced based on the stream.

BufferWriter CLASS DEMONSTRATION

Writes text to the character output stream to buffer each character, providing efficient writing of a single character, array, and string.

You can specify the buffer size or accept the default size. In most cases, the default value is large enough.

Special method: newLine ()

<Span style = "font-family: KaiTi_GB2312; font-size: 18px;"> import java. io. bufferedWriter; import java. io. fileWriter; import java. io. IOException; public class Main {public static void main (String [] args) throws IOException {FileWriter fw = new FileWriter ("ACM.txt "); // create a buffer object for writing a character to the stream and associate it with the specified buffer Stream object BufferedWriter bw = new BufferedWriter (fw); // use the buffer method, write data into the buffer bw. write ("ACM .... "); Bw. newLine (); // actually System. getproperty ("line. separator "); bw. write ("acm"); // write data to the destination bw by using a buffered refresh method. newLine (); bw. flush (); for (int I = 0; I <5; I ++) {bw. write ("1111"); bw. newLine (); bw. flush ();} // closes the buffer. Actually, the buffered Stream object fwbw is closed. close () ;}</span>

BufferedReader CLASS DEMONSTRATION

Read text from the character input stream, buffer each character, so as to achieve efficient reading of characters, arrays and rows.

You can specify the buffer size or use the default size. In most cases, the default value is large enough.

Special method: readLine ()


<span style="font-family:KaiTi_GB2312;font-size:18px;"><strong>import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;public class Main {public static void main(String[] args) throws IOException {FileReader fr = new FileReader("ACM.txt");BufferedReader br = new BufferedReader(fr);String str = null;while((str = br.readLine())!= null){System.out.println(str);}br.close();}}</strong></span>

Note:

ReadLine () method: The read method of the read buffer is used to buffer the read characters and determine the line feed mark. The buffer data before the mark is converted into a string and the returned result is


Java io stream copying File Content

Er, simply put, it is because you read the same file twice at the same time. After the first while reading, you just wanted to see what was in it, therefore, you can run the command to read one byte at a time until the system reads the end of the file! =-1 condition is false), but in fact, you think you are reading. After reading it, you may not want to read it again. You want to use the "while" command to read one byte and one byte of the same ". However, as early as the first while, the system has been stuck at the end of the file, and the result returned by running the command "FS" to read "is always-1. So in the second while statement, the system jumps out when the first read is executed by the system. Why? -1 is returned for the result of the command. read. So C is not assigned at all, and even if it is assigned a value, C can only hold the next char.
As for this solution, the idea is as follows: You read data to an int type variable date in one byte and one byte through the fins. Since date can be printed, it can be assigned to char c, naturally, it can be written into a file. Then, you can see if fos can write a file in one byte. In this way, each byte read by the while, fos writes it to a file (think about the efficiency ~~). However, you must remember that it is impossible to step into the same file for more than two times for the same fiis. A while is the end of the game !!

Ask a question in the JAVA book, copy IO stream files, and answer

Import java. io .*;
Public class Aaa {
Public static void main (String [] ashs) throws IOException
{
FileInputStream FCM = new FileInputStream (ashs [0]);
FileOutputStream fos = new FileOutputStream (ashs [1]);
Byte [] buf = new byte [1, 512];
Int n = 0;
Do {
N = FCM. read (buf, 0,512 );
Fos. write (buf, 0,512 );
} While (n! =-1)
FCM. close ();
Fos. close ();
}

}

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.