JAVA input/output stream

Source: Internet
Author: User

1.
How to read and write data in Java? What is stream )?

You must use Java I/O class to create an object and call methods in the class to read and write data. A Java I/O object is called a data stream. The object for reading data is called the input stream, and the object for writing data is called the output stream.

2.
In Java, how are characters represented in memory and in text files?

In the memory, Unicode encoding is used to represent characters. In a text file, a specified encoding scheme is used to represent characters. If no encoding scheme is specified, the system uses the default encoding scheme.

3.
What is the return value of the read () method of the text input stream? Is this the value stored in text files?

The read () method returns the unified character code. The actual value stored in a text file may not be the unified character code,

Note: Java uses Unicode. When a character is read from the filereader stream, the same code of the character is returned. The character encoding in the file may not be a uniform code. Java automatically converts it into a uniform code. When writing characters to a filewriter stream, Java automatically converts the unified code to the encoding specified by the file. That is to say, Java programs operate on uniform codes, but not necessarily in files. Therefore, when reading characters from files, they must first be converted to uniform codes, when writing characters to a file, you must first convert the uniform code to the specified encoding scheme of the file.

4.
How to Use filereader to create an input stream? What will happen when the file does not exist? How to Use filewriter to create an output stream? What happens when a file exists? Can I add data to an existing file?

You can use filereader to create input streams: filereader (File file) and filereader (string filename)

Classnotfoundexception is thrown when the file does not exist.

There are four constructor methods used to create a filewriter object:

Public filewriter (File file)

Public filewriter (sting filename)

Public filewriter (File file, Boolean append)

Public filewriter (string filename, Boolean append)

If the file does not exist, a new file will be created. If the file already exists, the first two constructor methods will delete the existing content of the file. To retain the content of the current file and append new data, use the following two constructor methods and set their parameter append to true.

5.
In Java I/O programs, why do I have to declare in the method to throw an exception ioexception or handle the exception in the try-Catch Block?

All methods and constructors except printwriter and printstream in Java I/O classes throw an ioexception because they usually encounter unexpected situations.

6.
Use the write ("91") method on the filewriter object to write to the file?

This method writes a string to the file and converts the '9' and '1' characters according to the encoding scheme specified in the file.

7.
Why is it always required to disable the data stream?

There are two reasons: 1. Close the stream to ensure that the data will be written to the file.

2. Closing a stream can release the resources required by the data stream object.

8.
What are the benefits of using the bufferedreader class and bufferedwriter class?

The bufferedreader and bufferedwriter classes speed up input and output by reducing the number of reads and writes. The data stream in the cache area uses a character array similar to the action of the cache. During input, the array reads characters into the buffer block and then reads a single character from the cache area. During output, the accumulated characters in the array are organized into blocks, and then the entire block is written into the output data stream.

If no cache size is specified, the default size is 8192 characters. The input stream in the cache area reads as much data as possible into the cache area during each read operation. On the contrary, the output stream in the cache zone calls the file writing method only when its cache zone is full or the flush () method is called.

9.
When should I use printwriter and printstream? How to Create a printwriter object? What types of data are used by system. In, system. Out, And system. Err? Why cannot I put system. Out. prinltn () into the try-catch method?

The printwriter class and printstream class can output objects, strings, and values in text format. Numeric value, character, or Boolean value is converted to a string and printed to the output stream. To print an object, you can print the description string returned by calling tostring.

System. in, system. out, system. the err type is printstream. printwriter and printstream do not throw ioexception, so system is called. out. when the println () method is used, it cannot be placed into a try-Catch Block.

10.
What is the difference between byte stream and byte stream?

The byte stream reads the byte from the file and then directly copies it To the memory without any conversion. On the contrary, the byte stream requires encoding and anti-encoding. The Java Virtual Machine converts a uniform code to the encoding specified by the file. When a character is written, the specified encoding in the file is converted into a uniform code when one character is read.

11.
Why does the read () method return an int value instead of a byte when inputstream reads bytes? What are the abstract methods in inputstream and outputstream?

The read () method returns the number of int bytes in the range of 0 to 255.

The abstract method of inputstream is only read ()

The abstract method of outputstream is only write (int B)

12.
What new methods are introduced in the fileinputstream and fileoutputstream classes? How do I create fileinputstream and fileoutputstream objects?


All fileinputstream/fileoutputstream methods are inherited from inputstream/outputstream.

You can create a fileinputstream object as follows:

Public fileinputstream (File file)

Public fileinputstream (string name)

You can create a fileoutputstream object as follows:

Public fileoutputstream (File file)

Public fileoutputstream (string name)

Public fileoutputstream (File file, Boolean append)

Public fileoutputstream (string name, Boolean append)

10.
After the writebyte (91) method is called in fileoutputstream, what is written into the file?

91 represents the number of 1 byte, expressed in hexadecimal notation as 0x5b, expressed in binary notation
01011011

13.
In a binary input stream (fileinputstream and datainputstream), how does one determine whether the output has reached the end of the file?

If no Bytes are readable, it indicates that the end of the file is reached. We use the available () method to determine if

Stream. Available () = 0 indicates that the end of the file is reached.

 

Overall grasp:

I/O is divided into two categories: text input output and binary input output.

Text input and output:

 


Text Files and binary files

The data stored in the text is expressed in a human-readable manner, while the data stored in the binary file is expressed in binary form. People do not understand binary files. They are designed for program reading. For example, the Java source program is stored in a text file and can be read in a text editor. However, Java classes are stored in binary files and read using Java virtual machines.

 


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.