Java file I/O operations, javao operations

Source: Internet
Author: User

Java file I/O operations, javao operations

Read/write operations on files in java

(1)

(1) convert the bytes of a file in java into characters for read Operations

FileInputStream fStream = new FileInputStream ("test.txt"); // The byte stream
Byte [] B = new byte [31]; // defines a byte array to store the data read from the folder, up to 31 bytes
FStream. read (B); // read data from test.txt to B.
String line = new String (B, "UTF-8"); // converts bytes to characters
System. out. println (line); // print the output

The above is an example of a simple byte to character output. When the data volume is large, you should read it again

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

(2

) Convert the character of a file in java into a byte write operation.

FileOutputStream fos = new FileOutputStream ("test.txt ");
String line = "hello world ";
Byte B [] = line. getBytes (); // converts a character to a byte
Fos. write (B); write files through byte streams

Fos. close (); // close

(2)

The buffer zone is used to write and write files. It is a buffer zone. java. io. BufferedReader and java. io. BufferedWriter each have a buffer zone of 8192 characters. When BufferedReader reads a text file, it tries its best to read character data from the file and put it into the buffer zone. If the read () method is used, it will first read the data from the buffer zone. When BufferedWriter is used, the written data is not output to the destination, but stored in the buffer. If the data in the buffer zone is full, the destination will be written at one time.

(1) using a buffer to write data to a file

String line = "hello ";
FileOutputStream fos = new FileOutputStream ("test.txt ");
BufferedWriter bfr = new BufferedWriter (new OutputStreamWriter (fos ));//
Bfr. write (line); // write data to the buffer zone
Bfr. flush (); // clear the buffer and pass all data to the file
Bfr. close (); // close the buffer
Fos. close (); // close the file output stream

Why is flush () used, because the buffer zone may have to wait until a certain amount of data is fully stored before writing to the file, resulting in a certain amount of data stranded, flush can be used to deliver stranded data to a file together to prevent the loss of file data.

(2) read data from files using the Buffer Zone

FileInputStream FCM = new FileInputStream ("test.txt"); BufferedReader bfr = new BufferedReader (new InputStreamReader (FCM); String line = null; while (line = bfr. readLine ())! = Null) // read data from a row in the read buffer {System. out. println (line);} bfr. flush (); // clear the buffer
Bfr. close (); // close the buffer. close (); // close the file input stream

Bfr. readline () reads data from the buffer in units of behavior, and BufferReader converts bytes into character classes.

 

In addition, writing a file does not overwrite new data.

FileOutputStream OS = new FileOutputStream ("test.txt", true); // true indicates that the original data is not deleted every time it is added to the end of the file.

 

(3) about FileReader and FileWriter

(1) FileReader

 

The FileReader class creates a Reader class that can read the file content. Applicable to plain text files

For example, read from a file row by row and output it to the standard input stream.

import  java.io.*;    class FileReaderDemo {        public static void main (String args[ ] ) throws Exception {            FileReader fr =  new FileReader ("c://in.txt");            BufferedReader br = new BufferedReader (fr);            String s;             while ((s = br.readLine() )!=null) {                 System.out.prinln (s);              }            fr.close();      }    }

(2) FileWriter

FileWriter creates a Writer class that can write files. Constructor:

FileWriter (String filePath) filePath is the complete 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 the File object that describes the File.

  FileWriter fr = new FileWriter("test.txt");  BufferedWriter bfw = new BufferedWriter(fr);  String linString="hello World";  bfw.write(linString);  bfw.flush();  bfw.close();  fr.close();

  

 

 

In summary, this is just a brief description of common methods for reading and writing files.

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.