Java file write operations

Source: Internet
Author: User

Here, both the read and write files in Java are based on the writable stream and mainly use the following classes:

1. FileReader ---- read the volume stream

2. FileWriter ---- write the volume stream

3. BufferedReader ---- buffer the input of a specified file

Methods of this class include:

Void close () to close the stream.

Void mark (int readAheadLimit) indicates the current position in the stream.

Boolean markSupported () determines whether the stream supports the mark () operation (it must support)

Int read () reads a single character.

Int read (char [] cbuf, int off, int len) reads characters into a part of the array.

String readLine () reads a text line.

Boolean ready () determines whether the stream is ready for reading.

Void reset () resets the stream to the latest tag.

Long skip (long n) skips characters.

4. BufferedWriter ---- buffer the output of the file

Methods of this class include:

Void close () to close the stream.

Void flush () refreshes the buffer of the stream.

Void newLine () writes a line separator.

Void write (char [] cbuf, int off, int len) writes a part of the character array.

Void write (int c) writes a single character.

Void write (String s, int off, int len) writes a part of the String.

For example:

Package aillo; import java. io. *; public class FileWriterReader {// function: Read the content of the f:/aillo.txt file (one row and one row), and write the content to f:/jackie.txt // knowledge point: java reads and writes files --- <in bytes stream mode> public static void main (String [] args) {try {FileReader fr = new FileReader ("f:/aillo.txt "); // create a FileReader object to read the latest stream BufferedReader br = new BufferedReader (fr); // buffer the input file FileWriter fw = new FileWriter ("f:/jackie.txt "); // create a FileWriter object to write the BufferedW of the primary stream. Riter bw = new BufferedWriter (fw); // outputs the buffer to the file String myreadline; // defines a String type variable, used to read a row at a time while (br. ready () {myreadline = br. readLine (); // read a row of bw. write (myreadline); // write the bw file. newLine (); System. out. println (myreadline); // output on the screen} bw. flush (); // refresh the buffer of the stream. After writing the file, remember to refresh the stream so that the content can be written into the file. In addition, close () can also write the content into the file. Bw. close (); br. close (); fw. close (); br. close (); fr. close ();} catch (IOException e) {e. printStackTrace ();}}} **************************************** ******************

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.