OCP - 3

Concurrency & IO

Concurrency & IO


Fichier Détails

Cartes-fiches 110
Langue English
Catégorie Informatique
Niveau Autres
Crée / Actualisé 08.03.2019 / 08.01.2020
Lien de web
https://card2brain.ch/box/20190308_ocp_3
Intégrer
<iframe src="https://card2brain.ch/box/20190308_ocp_3/embed" width="780" height="150" scrolling="no" frameborder="0"></iframe>

WHat is the skip method for on an INputstream()

Skip n bytes of data

How can you determine EOF when using a FileInputStream?

When the read() method returns -1

How can you determine EOF when using a BufferedInputStream?

When the return value of read is 0 since it returns the number of bytes read

How can you determine EOF when using a FileReader?

When the readLine() method returns null or or read() returns -1

What variants of read() and write() do FileInputStream and FileOutputStream privde?

An override to pass a byte array. This is used by the BufferedIn/OuputStream classes to improve performance

What convenience method does BufferedWriter provde?

the newLine method

Converting an in-memory object to a stored data format is refered to as ____

Converting an in-memory object to a stored data format is refered to as serialization

Converting stored data into an object is known as ____.

Converting stored data into an object is known as deserialization

What methods does the Serializable interface declare?

None, it's a marker interface.

What happens whe serializing a class that does not implement Serializable

A checked NotSerializableException (subclass of IOException) is thrown

How can you prevent fields from being serialized?

Using the transient keyword on the fields. The values are then lost during the serialization process

What happens with static members during serialization?

They are ignored as they do not belong to the particular instance

What is the serialVersionUID for? Do you have to declare it?

It is good practice to identify a unique version of the class. If it changes, an exception can be throw or it can be migrated.

If not declared, the compiler does it for you. Because different compilers have different strategies, you should manage it yourself

How can you Serialize and Deserialize objects?

Using the ObjectInputStream and ObjectOutputStream classes and the coresponding methods readObject() and writeObject

What has to be minded when using readObject()?

It throws additionally to the checked IOExcption a checked ClassNotFoundException

Do you have to manually cast the results of the readObject() method?

Yes, the return type is of type Object

How can you determine EOF when using a ObjectInputStream?

Catch the EOFException!

is it safe to use inputsream.available() to check if a stream is EOF?

NO! Use the specific strategies for each kind of stream

How is a serialized object created?

  • Java calls the first non-arg constructor for the first nonserializable parent class, skipping the constructors of all serialized classes in between!
  • Static variables are ignored
  • default intializers (eg private String myField = "123" and {}) are ignored

What are the PrintStream and PrintWriter classes for?

They offer a high-level interface to write formatted representation of Java objects to a text-based outputs stream

What is a common use of PrintStream?

The System.err and Sytstem.out fields

What is special about the print, println, printf and format methods found on the PrintStream/PrintWriter classes?

They do not throw an exception! The checkError() method can be used for that instead

What line separator does the println method od PrintWriter/PrintStream use?

It depends on the os. You can access the value using System.getProperty("line.separator")

What is the difference between format and printf on PrintWriter/PrintStream

They are identical, printf calls format behind the scenes

What is the Console class for?

It is convenience for the Readers/Writers System.in, System.out and System.err

What is special about the Console class?

It is a Singleton that is accessed using the System.console() method

What common methods does the Console class provide?

  • reader()
  • writer()
  • format() / printf()
    • Locale override is not supported! Use writer.format instead!
  • flush()
  • readLine()
  • readPassword()

When should you use the Console.flush() method?

Before using readLine and readPassword to ensure only user input is read

Why does Console.readPassword return a byte array?

If a string were returned, the passwourd might be available in the String pool long after the user entered it. By using a byte array, the password can be wiped as soon as it is not required anymore using Arrays.fill(passwordArray, 'x')

Does the method Writer.append exist

Yes