And look back on the 26th day of Java

Source: Internet
Author: User

Pushing back the input stream
There are two special streams in the Java input and output stream system that are unique, namely Pushbackinputstream/pashbackreader, which provide the following three methods:
void Unread (byte[]/char[] buf): Pushes the contents of a byte/character array back into the push buffer, allowing repeated reads of the content just read.
void Unread (byte[]/char[] buf, int off,int ben): Starts a byte/character array from off, and the Len Byte/character content is pushed back into the push buffer, allowing repeated reads of the content just read
void unread (int b): pushes a byte/character back into the push buffer, allowing repeated reading of the content just read
Both inputs have a push-back buffer, which, as the two-unread method that pushes the input stream into the program caller, pushes the contents of the specified array into the buffer, which is always read from the recall buffer each time the Read method is called, and only when the contents of the push-back buffer are fully read , and it is not read from the source input stream until the array parameter in the Read method is filled.
The size of the push-back buffer needs to be set when we create the Pushbackinputstream/pushbackreader, and the default push-back buffer size is 1. If the size of the push-back buffer exceeds the size of the push buffer, the program throws pushback buffer overflow IOException

The following program tries to find the new pushbacktest string in the Pushbacktest.java file, and after the string is found, the program just prints out the contents of the target string.

Import java.io.FileNotFoundException;
Import Java.io.FileReader;
Import java.io.IOException;
Import Java.io.PushbackReader;

Public class pushbacktest{
public static void Main (string[] args) {
Pushbackreader pr=null;
try {
Pr=new pushbackreader (New FileReader (". \\src\\PushbackTest.java"), +);
Char[] Cbuf=new char[64];
String lastcontent= "";
int hasread=0;
while ((Hasread=pr.read (CBUF)) >0) {
string content=new string (cbuf,0,hasread);
int targetindex=0;
If ((targetindex= (lastcontent+content). IndexOf ("New Pushbackreader")) >0) {
Pr.unread (lastcontent+ Content). ToCharArray ());
Pr.read (Cbuf,0,targetindex);
System.out.println (New String (Cbuf,0,targetindex));
return;
}else{
System.out.println (lastcontent);
Lastcontent=content;
}
}
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
} finally {
if (pr!=null) {
try {
Pr.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}
}

The first step of the program is to create a Pushbackreader object that specifies a buffer size of 128, and create a character array of length 64 to hold each read of the character content and then request an empty character variable to hold the last read of the string content. The hashread is used to hold the number of characters per read. Go to the While method first look at the condition: as long as the file is not read to execute; the while condition executes a read operation, and the read content is intercepted to the exact length of the character saved in the Cbuf array, and the number of characters read back to Hasread, once read, A while condition is also completed, then executes the statement in the while loop, first converts the read character array cbuf from the subscript 0 to the beginning of the length of hasread into a string, Then apply for a Targetindex variable to record the target string in the last read content and the content of this read, if the read content contains the target substring, in fact, the record is also pushed back to the buffer after the target string appears in the position,
Here is the if judge to see if the content of this read contains the target string, if not in the execution of the content output of else the last print test content, and copy the contents of this read to Lastread as the next print content, the last read, If the IF condition satisfies then the last read content and this read content as the connection operation, and the target string to find the location of the assignment to the Targetindex variable, followed by
The last read string and the string read after the connection operation is returned to the buffer, and then call the Read method again this time read is the contents of the buffer, read the content is exactly 0 from the beginning of the length of the target just to fill the cbuf and then process the read array and print the completion of the program, The IOException exception that occurs pushback buffer overfow does not occur because the character length of the two reads is exactly equal to the buffer length and is not exceeded.
On the whole, the above procedure is to constantly read the fixed-length content and print it, until the target string is read, after satisfying the if condition, do two things to print the last read content, and push back the input stream buffer.
There is an implicit record pointer after each read method in the while condition to record the location of this read, as the start of the next read

And look back on the 26th day of Java

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.