I don't want to take others' things as my own, but I want to make it easier for future reference or excerpt.
Reading Java programming ideology 3 or 4, I feel that foreigners write books differently from reading books. I always feel that what foreigners write is like writing a manual, which is comprehensive but cumbersome.
[Principle] Don't tell me history. Just tell me how to do it.
[Fact] the output input class is simple for TMD. Why do I have to make a manual so that this cainiao doesn't understand it?
[Thanks] Summary of the Chinese IT lab
----------------------------------------------------------
File Filter
Filterinputstream and filteroutputstream perform special processing on other input/output streams respectively. They can process data while reading/writing data. In addition, a synchronization mechanism is provided to allow only one thread to access an input/output stream at a certain time point.
Filterinputstream and filteroutputstream override all methods of the parent class inputstream and outputstream respectively. At the same time, their subclasses should also override their methods to meet specific needs.
• To use a filter stream, you must first connect it to an input/output stream. Generally, you must specify the stream to be connected in the parameters of the constructor:
-Filterinputstream (inputstream in );
-Filteroutputstream (outputstream out );
These two classes are abstract classes, and the constructor is also a protection method.
Bufferedinputstream and bufferedoutputstream implement a filter stream with a buffer. They provide a buffer mechanism to "bundle" any I/O stream to the buffer stream to improve read/write efficiency.
• In addition to specifying the connected I/O stream, you can also specify the buffer size during initialization. The buffer with the default size is suitable for the general situation. The optimal buffer size usually depends on the host operating system, available memory space, and machine configuration; generally, the buffer size is an integer multiple of memory pages or disk blocks, such as 8912 bytes or smaller.
-Bufferedinputstream (inputstream in [, int size])
-Bufferedoutputstream (outputstream out [, int size])
Example 8.5 list the file names with filters in the current directory.
In this example, the accept method in the filenamefilter interface is implemented to list the file names with filters in the current directory.
The procedure is as follows:
Import java. Io .*;
Public class dirfilter implements filenamefilter
{
Private string prefix = "", suffix = ""; // prefix and suffix of the file name
Public dirfilter (string filterstr)
{
Filterstr = filterstr. tolowercase ();
Int I = filterstr. indexof ('*');
Int J = filterstr. indexof ('.');
If (I> 0)
Prefix = filterstr. substring (0, I );
If (j> 0)
Suffix = filterstr. substring (J + 1 );
}
Public static void main (string ARGs [])
{// Create a filename filter object with wildcards
Filenamefilter filter = new dirfilter ("W * abc.txt ");
File F1 = new file ("");
File curdir = new file (f1.getabsolutepath (), ""); // the current directory
System. Out. println (curdir. getabsolutepath ());
String [] STR = curdir. List (filter); // list file names with Filters
For (INT I = 0; I <Str. length; I ++)
System. Out. println ("/t" + STR [I]);
}
Public Boolean accept (File Dir, string filename)
{
Boolean Yes = true;
Try
{
Filename = filename. tolowercase ();
Yes = (filename. startswith (prefix ))&
(Filename. endswith (suffix ));
}
Catch (nullpointerexception E)
{
}
Return yes;
}
}
When the program is running, list the file names that meet the filtering condition "W *. txt" in the current directory. The result is as follows:
D:/myjava
Write1.txt
Write2.txt