20170831-a-Java IO operations

Source: Internet
Author: User
Tags object serialization

1 What does it take to convert an object to bytes?
Object Flow
Person p = new person ("Cang teacher", 18, "male", list);
SYSTEM.OUT.PRINTLN (P);

FileOutputStream fos = new FileOutputStream ("Person.obj");
ObjectOutputStream oos = new ObjectOutputStream (FOS);

Oos.writeobject (P);
Oos.close ();

2 What do I need to be aware of when using object flow to byte objects?
Objects that are bytes must be implemented serialzable

3 How do I convert a Byte object to file data?
Use the object input stream to load a file stream that connects a byte object, which is used to read and convert a set of bytes (a set of bytes that are converted from the object's output stream) to the corresponding object
The Unicode memory is two bytes.
FileInputStream fis = new FileInputStream ("Person.obj");
ObjectInputStream ois = new ObjectInputStream (FIS);

Person P = (person) ois.readobject ();
SYSTEM.OUT.PRINTLN (P);

Ois.close ();

4 What is Object serialization, object deserialization?
Object output stream The process of converting an object out to a set of bytes is called: Object serialization
The process by which an object input stream reads and restores this set of bytes to an object is called: Object deserialization

5 Object deserialization returns binary data or objects?
Objects that are directly object type, because OIS has converted the read binary data.

5 Once the Serializable interface is implemented, is it necessary?
Maintenance serial. UID to improve the compatibility of the class.
The class will first check that the UID is consistent when deserializing, so if it is not maintained, it is automatically generated by the system, and once the class is modified, the UID will change itself, so it needs to be maintained when the interface is implemented.

6 How do I selectively write out object properties? When do I need to use it?
Using the Transient keyword, the value of this property is null once the keyword is used. When the object needs to be thin.
(Private transient String gender;)

7 by unit shunt, byte stream, character stream.

8 The characters in Java are char type data?

9 What are the characters used for reading?
/**
* Java according to the stream read and write data units divided into: byte stream, character stream
* Byte stream reads and writes data in bytes.
* Character streams read and write data in characters (Unicode), but the underlying is essentially read-write bytes, but the conversion of bytes and characters is done by a character stream
*
* Write and reader are the parent classes of all character streams, they are a pair of abstract classes,
* Specifies the method of reading and writing characters for all character streams.
*
* Conversion Stream
* OutputStreamWriter and InputStreamReader are a common implementation class for character streams
* @author Admin
*
*/

10 Can I copy a mp3 file with a character stream?
No, the character stream can only manipulate characters.

11 What stream can I write directly to a string?
OutputStreamWriter

FileOutputStream fos = new FileOutputStream ("Osw.txt");
OutputStreamWriter OSW = new OutputStreamWriter (FOS);
Osw.write ("Rubbing on a smooth road");
Osw.write (", friction, friction. ");
Osw.close ();

12 How to develop character encoding mode for character input stream?
OutputStreamWriter OSW = new OutputStreamWriter (FOS, "UTF-8");

13 How do I output the obtained string?
FileInputStream fis = new FileInputStream ("Osw.txt");
InputStreamReader ISR = new InputStreamReader (FIS, "GBK");

int d =-1;
while ((d = isr.read ())! =-1) {
System.out.println ((char) d);
}

char[] data = new CHAR[100];
int len = isr.read (data);
String str = string.valueof (data, 0, Len);
System.out.println (str);

Isr.close ();
Read () or read (char[]) method using the ISR
You can also specify
InputStreamReader ISR = new InputStreamReader (FIS, "GBK");
Read returns the low eight-bit (that is, the corresponding Unicode value) of an array of type int
Read (char[]) will deposit the specified number of characters in the array. So one needs to convert, one does not need.

The character stream is only connected to the byte stream.

14 How do I write a string by line?
PrintWriter pw = new PrintWriter ("Pw.txt");

Can printwriter specify the write-out character encoding mode?
No, so when you have to specify, connect to the transition stream, you must create a new byte stream (only one step bw is saved)
FileOutputStream fos = new FileOutputStream ("Pw2.txt");
OutputStreamWriter OSW = new OutputStreamWriter (FOS, "utf-8");

What does the CSN in the 16 parameter mean?
CharSet name

17 What are buffered character streams?
* Buffer character stream built-in buffer, can improve the efficiency of read and write characters,
* and the buffered character stream is characterized by the ability to read and write strings by line
* Java.io.BufferedWrite
* Java.io.BufferedReader
*
* Java.io.PrintWrite is a commonly used buffered character output stream,
* The automatic row refresh feature is also available. Because Printwrite is created
* The BufferedWriter is embedded in the period, so the actual buffer operation is bufferedwriter implementation.

How does PW work?
/*
* PW supports the construction method of the operation of the file
*
* PW Internal principle:
* PW nested bw--speed Read and write, and PW can be wrapped, automatic row refresh
* The BW in PW is then nested with the osw--character converted into bytes.
* OSW of BW inside pw nested f0s--Write bytes
*/

PW embedded BW to speed up read and write, bw connected to the character conversion stream OSW,OSW and then connected to the byte stream to get bytes of data.

18 can read and write be completed if the file is not closed?
No, because the file is also stored in the buffer (unless it is larger than the buffer cache).

19 What is automatic row refresh?
When using PW's println, it automatically flush () after each input, that is, the file does not close, and read and write.
Close is required to call flush ()

PrintWriter (Outpustream Out,boolean AutoFlush)
PrintWriter (Writer Out,boolean AutoFlush)

20 How to read a row of strings at once.
public class Bufferedreader_readline {

public static void Main (string[] args) throws IOException {
TODO auto-generated Method Stub
FileInputStream fis = new FileInputStream
("src" +file.separator+
"Day08" +file.separator+
"Bufferedreader_readline.java");
InputStreamReader ISR = new InputStreamReader (FIS);
BufferedReader br = new BufferedReader (ISR);
/*
* BufferedReader provides methods:
* String ReadLine ()
* Read a number of characters in a row, knowing that a newline character has been read.
* Returns all previous characters as a string.
* Note: The returned string does not contain a line break
* If the return value is NULL, it indicates the end of the file
*/

String line = null;
while (line = Br.readline ()) = null) {
System.out.println (line);
}

Br.close ();
}

}
PW BR
OSW ISW
FOS FIS


22 What is the parent class of all exceptions and what subclasses are there?
Throwable, under which there are two sub-classes--
Program error: Exception
Java Run Environment error:

23 is thrown when an error occurs?
code block for calling code

How does Java handle errors?
try{
String str = NULL;
System.out.println (Str.length ());
}catch (NullPointerException e) {//Catch Exception instance
System.out.println (e);
String str = "123";
System.out.println (Str.length ());
}

25 What happens if an error occurs within the error-handling code block? How to deal with?


26 can I catch a different error?
try{
System.out.println (Str.charat (3));
System.out.println (Str.length ());
}catch (NullPointerException e) {
System.out.println (e);
}catch (Exception e) {
System.out.println (e);
}

What happens when the code in finally is executed?
In any case, whether or not an error has been
Even if you return in a try, the code block in the finally run will not jump out.

28 The instantiated Io object, which must finally be closed, must be judged if it is a null value when it is closed in finally.
FileOutputStream fos = null;
try {
FOS = new FileOutputStream ("Fos.txt");
Fos.write ("Hello" <br/>.getbytes ());
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} finally{
try {
if (fos! = null) {
Fos.close ();
}
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

What is Finalize?
The method defined in the object class that, when an object is about to be released by the GC, invokes the Finalize method of the object, and the object is freed after the call.

20170831-a-Java IO operations

Related Article

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.