Java Basic notes (vi)

Source: Internet
Author: User

Io:
Press Flow:
Input stream: The stream from which the program can read data
Output stream: The stream to which the program can write data
By Transmission Unit:
BYTE stream: Streams that transmit data in bytes
Character stream: Streams that transmit data in characters
by function:
Node Flow: A stream used to directly manipulate a target device
Process flow: A connection and encapsulation of an already existing stream, providing more powerful and flexible read and write capabilities for the program by providing processing of the data
IO (Input output) inputs and outputs
Read-Write file operation:
1 Create a File class object to locate the files.
2 Creating a Stream
3 Reading and writing
4 Closing the Stream

Character Stream:
Writer:
FileWriter (String fileName)
FileWriter (String Filename,boolean append)

    void write(int c):一个字符一个字符的写入    void write(char[] c)    void write(char[] c,int off,int len)    close()    flush()

Reader:
FileReader (String fileName) throws FileNotFoundException
int read (): Return value Read content
int read (char[] c): Length of return value read
int read (char[] c,int off,int len) returns the length of the value read
Read (): When the end of the file is read, the return value-1
Close ()
Buffered streams: processing flow
specifically to improve read and write efficiency, the read-write operation must be performed in conjunction with the reading stream
It is efficient to read the data to the buffer and then read the data from the buffer.
Applies to large file situations
BufferedReader character Buffer Read stream
BufferedWriter character Buffer Write stream

Decorator Mode: Provides enhanced functionality based on existing features
The extended class is passed in as a constructor parameter for the new class, and the class is decorated.
In order for the decorator to have the function of the decorator, there is a need to have an inheritance relationship

Character stream: FileWriter
Writing data to a text file
1) using a writer Stream
2) using a file character stream FileWriter
Construction method: Throws IOException
FileWriter ( File file)
FileWriter (file File,boolean append)
FileWriter (string filename)
FileWriter (String Filename,boo Lean Append)
Method:
void write (int c): write single character
Void write (char[] c): Write character array
void write (char[] C,intoff,int len ): Write part of the character array
void write (String str): write String
Void Write (String str,int off,intlen): Write part of the string
Void Flush (): Refreshes the Buffered
Void Close () of stream: Closes the stream and automatically refreshes before it is closed

Create a file output stream object FileWriter
If the file does not exist, it will be created automatically if it is present and will be overwritten
FileWriter FW = new FileWriter ("Src/temp.txt");
Writes data to the file, writes to the buffer, which is a byte array
Fw.write ("Hello world!!");
Flush buffers
Fw.flush ();
Fw.write ("AA");
Fw.close (); After the buffer is automatically refreshed, the stream is closed. You cannot write after you close the stream, or you will throw an exception
System.out.println ("write success");
System.out.println ("—————");
The new content that is written is appended to the original content
FileWriter FW1 = new FileWriter ("Src/temp.txt", true);
Fw1.write ("Hello!! ”);
Fw1.flush ();
Fw1.close ();

Reader class: FileReader
Construction method: Throws FileNotFoundException
FileReader (File file)
FileReader (String fileName)
Method: Throws IOException
int read (): read one character at a time, end-1
The return value is the read-in content
int read (char[] c): Reads a set of characters, the maximum reading group length, the end-1
The number of actual reads of the return value
int read (char[] c,int off,int len): reads a set of characters, up to Len, data into an array starting from off, end-1
The number of actual reads of the return value
void Close ()

int num = 0;
while (num = Fr.read ())! =-1) {//read one character at a time, the return value at the end is-1
System.out.println ((char) num);
}
Fr.close ();

BufferedWriter
Process flow (do not manipulate files directly)
Purpose: Cache data to increase the speed of writing. Similar memory
BufferedWriter (writer writer)
NewLine ()->void writes a new line
The file write stream is created first because the buffer stream does not have write capability
FileWriter FW = new FileWriter ("Src/temp.txt");
To improve efficiency, use a buffer stream. A construction method for passing a stream that needs to be improved to write efficiency as a parameter to a buffer write inflow
BufferedWriter bw = new BufferedWriter (FW);
for (int i = 1; i <=; i++) {
Bw.write ("Hello" + i);
Bw.newline (); Cross-platform
Bw.flush ();
}
Bw.close (); Closing the buffer stream is closing the write stream

Bufferedreader:bufferedreader (Reader Reader)
String ReadLine (): reads a row of data at a time, but the returned content does not include a newline, if the end, null
Read from a stream file using a buffer
Create a file read stream and a file-associated buffer read stream
FileReader FR = new FileReader ("Src/temp.txt");
To improve efficiency, read the stream with a buffer
BufferedReader br = new BufferedReader (FR);
Provides a function to read one line at a time string readLine ()
String line = null;
while (line = Br.readline ()) = null) {
System.out.println (line);
}
System.out.println (Br.readline ());//null
Br.close (); Automatically Close FileReader objects

BYTE stream:
InputStream class
FileInputStream
OutputStream class
FileOutputStream
Returns the size of the file, unit byte int num = Fis.available (); SYSTEM.OUT.PRINTLN ("File size:" + num + "bytes");
byte[] arr = new Byte[num]; Defines a byte array with the same size as the file int len =fis.read (arr);//does not apply too large a case System.out.println (new String (arr, 0,len));
Read one byte at a time: Chinese will appear garbled, half of the Chinese characters are read
int num = 0;
while (Num =fis.read ())! =-1) {
System.out.println ((char) num);
}

Put into the array after reading byte[] b = new byte[1000]; int len = 0;
while (Len =fis.read (b))! =-1) {
System.out.println (New String (b, 0, Len));
}
Fis.close ();

Standard input stream
InputStream is = system.in;
Convert standard input byte stream into character stream
InputStreamReader ISR = new InputStreamReader (IS);
Increase efficiency with buffer flow
BufferedReader br = new BufferedReader (ISR);
Standard output stream: print stream
PrintStream out = System.out;
Creating an output Stream object
OutputStreamWriter OSW = new OutputStreamWriter (out);
Creating a buffer output stream
BufferedWriter bw = new BufferedWriter (OSW);
String line = null;
while (line = Br.readline ()) = null) {
if (Line.equals ("88")) {
Bw.write ("Thanks for use");
Break
}
Bw.write (line);
Bw.newline ();
Bw.flush ();
}
Br.close ();
Bw.close ();

File:

Basic methods:
File F05 = new file ("Src/demo1.java");
SYSTEM.OUT.PRINTLN ("filename called:" + f05.getname ());
SYSTEM.OUT.PRINTLN ("The path to the file is:" + f05.getpath ());
SYSTEM.OUT.PRINTLN ("Parent path is:" + f05.getparent ());
SYSTEM.OUT.PRINTLN ("absolute path:" + F05.getabsolutepath ());
System.out.println ("File length is:" + f05.length () + "B");

    System.out.println("Demo1.java是否是一个文件?" + f05.isFile());    System.out.println("Demo1.java是否是一个目录?" + f05.isDirectory());    System.out.println(new File("e:/abc").delete());    System.out.println(new File("e:/abc/123/456/789").delete());

FileFilter: File filter
Show All TXT
static void Listtxt (file file) {
file[] files = file.listfiles (new FileFilter () {

        @Override        public boolean accept(File pathname) {            String name = pathname.getName();            if (name.endsWith("txt")) {                return true;            }            return false;        }    });    for (File f : files) {        System.out.println(f.getName());    }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Basic notes (vi)

Related Article

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.