I/O operations for files in Java

Source: Internet
Author: User

Read and write operations for files in Java

A

(1) Byte of file in Java into character read operation

     FileInputStream fstream = new FileInputStream ("test.txt");//byte stream at this time
Byte[] B = new byte[31];//defines the byte array to store data read from the folder, up to 31 bytes in size
Fstream.read (b);//read test.txt data to B
String line = new String (b, "UTF-8");//convert bytes to Characters
System.out.println (line);//Print output

The above is an example of a simple byte-to-character output, which should be read repeatedly when the amount of data is large

while (Fstream.read (b)!=-1)
{
String Line=new string (b, "UTF-8");
System.out.println (line);
}

(2

The characters in the file in Java are converted to byte write operations

FileOutputStream fos = new FileOutputStream ("test.txt");
String line= "Hello World";
byte b[]=line.getbytes ();//Convert characters to bytes
Fos.write (b); writing files by Byte stream

Fos.close ();//Close

Two

The buffer is used to write and write the files, all of which are buffer-mediated, java.io.BufferedReader and Java.io.BufferedWriter classes each have a buffer of 8192 characters. When BufferedReader reads a text file, it tries to read the character data from the file and place it in the buffer, and then reads from the buffer before using the Read () method. If the buffer data is not enough to be read from the file again, when using BufferedWriter, the data written is not first exported to the destination, but is first stored in the buffer. If the data in the buffer is full, the destination is written out one at a time.

(1) Write data to a file using a buffer

String line= "Hello";
FileOutputStream fos = new FileOutputStream ("test.txt");
BufferedWriter bfr = new BufferedWriter (new OutputStreamWriter (FOS));//
Bfr.write (line);//write data to buffer
Bfr.flush ();//clean buffer, pass data all into file
Bfr.close ();//Close buffer
Fos.close ();//close file output stream

Why Flush (), because the buffer for small data, may have to wait until the full amount of data to write to the file, resulting in a certain data retention, using the flush can be used to transport the stranded data to the file, to prevent the loss of file data.

(2) using buffers to read data from a file

FileInputStream fis = new FileInputStream ("test.txt"); BufferedReader bfr = new BufferedReader (new InputStreamReader (FIS));     String Line=null;  while ((Line=bfr.readline ())!=null)//Read data from a row of read buffers {      System.out.println (line);} Bfr.flush ();//emptying buffer
Bfr.close ();//close buffer fis.close ();//close file input stream

Bfr.readline () is the Bufferreader by converting bytes into character classes by reading the data from the buffer in the behavior unit.

In addition, the write about the file does not overwrite the new data, the method

FileOutputStream OS = new FileOutputStream ("Test.txt", true),//true means each time it is added to the end of the file, so the original data is not deleted

(iii) on FileReader and FileWriter

(1) FileReader

The FileReader class creates a reader class that can read the contents of a file. Comparison applies to plain text files

For example, reading from a file line by row and outputting it to the standard input stream.

Import  java.io.*;    Class Filereaderdemo {public        static void Main (String args[]) throws Exception {            FileReader fr =  new Filere Ader ("C://in.txt");            BufferedReader br = new BufferedReader (FR);            String s;             while ((s = br.readline ())!=null) {                 System.out.prinln (s);              }            Fr.close ();      }    }

(2) FileWriter

FileWriter Create a writer class that can write files. constructor function:

FileWriter (String FilePath) FilePath is the full path of a file

FileWriter (String FilePath, Boolean append) if Append is true, the output is appended to the end of the file, that is, the original data is not overwritten

FileWriter (file fileobj) is a file object that describes the

  FileWriter FR = new FileWriter ("test.txt");  BufferedWriter BFW = new BufferedWriter (FR);  String linstring= "Hello World";  Bfw.write (linstring);  Bfw.flush ();  Bfw.close ();  Fr.close ();

  

All in all, just a simple description of the common methods of reading and writing documents, please

I/O operations for files in 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.