Java file operations 1 and java operations 1

Source: Internet
Author: User

Java file operations 1 and java operations 1

In daily development, we often encounter file operations. All file operations in java are performed in java. under the I/O package, the classes in this package include File, inputStream, outputStream, FileInputStream, FileOutputStream, reader, writer, FileReader, and FileWriter. Operations on files are classified into two categories, the first is the byte stream. The so-called bytes stream reads/writes in bytes (8b). bytes stream reads/writes in characters. java uses unicode encoding and one character is two bytes, the following sections describe the byte stream and the byte stream respectively,

1. byte stream


Byte streams are read/write operations in bytes. They are used to operate binary files, such as images, videos, and audios.

Byte streams are divided into reading and writing. Here, reading and writing are for programs. Read: reads files from the disk to the program; writes: writes to the disk.

In byte streams, inputStream and outputStream are the abstract classes for reading and writing. They only define some basic methods, and the specific operations are completed by their subclasses. Shows its subclass,

Here we will focus on FileInputStream and FileOutputStream.

FileInputStream is a file operation class that reads data in bytes,

Its construction method is as follows,

There are three constructor methods, mainly to describe the first and third, the first parameter is the File object, the third is a File path, if the File path indicates the File does not exist, this will throw an exception that does not exist in the file.

When FileInputStream is used to read files, the following methods are used: read (), read (byte [] B ),

Read () reads one byte at a time. The returned value is an int value of this byte type.

Read (byte [] B) reads B. length bytes at a time. The returned value is the number of bytes read.

The usage is as follows,

Package cn.com. test; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. IOException; public class ReadFileByBytes {public static void main (String [] args) {// TODO Auto-generated method stub FileInputStream FD = null; try {FD = new FileInputStream ("e: \ 11.txt"); // 1. Use the read () method to read int B; while (B = FCM. read ())! =-1) {// If the returned value is-1, it indicates that the file has been read. // do something} // 2. read int bs using read (byte [] B; // stores the number of bytes read each time. byte [] bytes = new byte [1024]; // defines an array of 1024 bytes as the buffer pool while (bs = FS. read (bytes ))! =-1) {// The returned value is-1, indicating that the file has been read. // the file has been read to the bytes array. String str = new String (bytes ); // generate a string using each byte array} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {if (FS! = Null) {try {fs. close ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}}}

In the above Code, the file is read in two ways. The returned values are-1.-1 indicates that the file has been read at the end of the file, at last, you need to close the input stream.

Writing a file in bytes is similar to reading a file. The method is write (int B) and write (byte [] B). That is, one byte is written at a time and one byte array is written at a time.

Ii. Ghost stream


The delimiter stream reads/writes files in characters each time. The Reader and Writer are included in the delimiter stream. The reader introduction is as follows,

Reader is also an abstract class. Its sub-classes mainly include BufferedReader, FilterReader, and InputStreamReader. FileInputStream is used when byte streams are used, and FileReader is also available in the bytes stream. It is just a subclass of InputStreamReader. InputStreamReader and BufferedReader are two very interesting classes.

Its construction method is the same as that of FileInputStream. We also use the following two methods:

The specific reading method is as follows. Since FileInputStream reads data by byte, it reads data by character, read () and read (char [] c) in FileReader ), the characters read by the first method are returned in int type, and the second method returns the number of characters read.

Package cn.com. test; import java. io. fileNotFoundException; import java. io. fileReader; import java. io. IOException; public class ReadFileByChars {public static void main (String [] args) {// TODO Auto-generated method stub FileReader fr = null; try {fr = new FileReader ("e: \ 11.txt"); // 1. Use the read () method to read int B; while (B = fr. read ())! =-1) {// If the returned value is-1, it indicates that the file has been read // do something} // 2. read int bs using read (char c; // stores the number of characters read each time. char [] chars = new char [1024]; // defines an array of 1024 characters as the buffer pool while (bs = fr. read (chars ))! =-1) {// The returned value is-1, indicating that the file has been read. // the object has been read to the bytes array String str = new String (chars ); // generate a string using each character array} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();} finally {if (fr! = Null) {try {fr. close ();} catch (IOException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}}}

The above two methods are used to read files: read and read (char [] c ).

The character writing method is the same. The FileWriter writer (char c) and writer (char [] c) methods are used to write one character and one character data group respectively.

 

Through the above description, we found that the two methods of Operating Files in bytes and characters are similar. The methods used are the read and write methods, but the operation units are different. Put these two methods together to make it easier to understand and master. You are welcome to give different comments. Thank you !!!

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.