Java io file learning notes

Source: Internet
Author: User

File f = new file ("D:" + File. separator + "test.txt ");

File. separator cross-System File separator

F. createNewFile (); create a file

F. mkdir (); Create a folder

F. delete (); delete

F. list (); f. listFile (); used to traverse objects

 

Byte streams are processed by InputStream and OutputStream.

Classes that process byte streams inherit from InputStream or OutputStream.

BufferedInputStream: Provides the buffer function.

DataInputStream: allows applications to read Basic Java data types from the underlying input stream in a machine-independent manner. Applications can use the data output stream to write data that is later read by the data input stream.

PipedInputStream: allows you to process streams in pipelines. When connected to a PipedOutputStream, it reads the data output from the latter to the pipeline.

PushbackInputStream: allows you to return data that has been read.

SequenceInputStream: it can process multiple inputstreams in sequence.

BufferedOutputStream: Provides the buffer function for the output stream. before writing, you must call flush to ensure data output.

DataOutputStream: The data output stream allows applications to write Basic Java data types to the output stream in an appropriate manner. Then, the application can use the data input stream to read data.

PipedOutputStream: Allows stream processing in the pipeline. You can connect the MPs queue output stream to the MPs queue input stream to create a MPs queue. The output stream of the MPs queue is the sender of the MPs queue. Generally, data is written by a thread.PipedOutputStreamObject, and connected by other threads fromPipedInputStreamRead.

PrintStream: adds features for other output streams so that they can easily print various data value representations. The commonly used System. out or System. err are both PrintStream.

The volume stream is processed by Reader and Writer.

Input stream:

CharArrayReader: reads data from the character array in memory to stream read data.

StringReader: reads data from strings in the memory for stream reading.

FileReader: reads data from a file. Note that the default JVM encoding is used to convert the data, but the encoding cannot be specified. Therefore, do not use this method when the file encoding is not the JVM default encoding. To perform transcoding correctly, use InputStreamReader.

Decoration type:

BufferedReader: Provides the buffer function to read rows: readLine ();

LineNumberReader: provides methods such as getLineNumber () to read rows.

InputStreamReader: bridges the byte stream to the bytes: it uses the specifiedcharset Read bytes and decode them into characters.

Output stream:

Classes related to root data goals:

CharArrayWriter: writes the character array in the memory to the output stream. The buffer zone of the output stream automatically increases the size. The data of the output stream can be retrieved through some methods.

StringWriter: A Response stream that can be recycled from the output in the string buffer to construct a string.

FileWriter: writes data to a file.

Decoration type:

BufferedWriter: Provides the buffer function.

OutputStreamWriter: bridge between the bytes stream and the byte stream: You can use the specifiedcharset The characters to be written into the stream are encoded into bytes.

PrintWriter: prints the formatted representation of the object to the text output stream.

 

Sample Code for one exercise

Main (String [] args) String filename = "E:" + File. separator + "test.txt" File f = f. createNewFile (); OutputStream outputStream = FileOutputStream (f,); String str = "Hello, hahaha" [] B = (f. isDirectory () {System. out. println ("is a folder");} InputStream inputStream = FileInputStream (f); [] by = [() f. length ()]; System. out. println (Writer writer = FileWriter (f,); writer. write ("hello" Reader reader = FileReader (f); [] Ch = [1000 print (f); (f! = File [] file = (file! = (I = 0; I <file. length; I ++ }}

 

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.