OutputStream

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page        

Can a value from an int variable be written to a binary file without first translating it into characters?

Answer:

Yes.

OutputStream

A Java int uses patterns of 32 bits to represent integers. The exact pattern contained in an int can be written to four bytes of a binary file.

OutputStream is an abstract class from which all byte-oriented output streams descend. Its descendant classes are used for general-purpose (non-character output). These streams write groups of bytes to output destinations. Java writes the same format for primitive types on all computer systems. The data in a binary file can be read by Java programs running on any computer system.

A FileOuputStream connects an output stream of bytes to a file. It is has methods for writing single bytes or an array of bytes to a file, and some other methods. These methods are not very useful for us since you have to deal with the individual bytes of a data type.

A DataOuputStream can be connected to a FileOutputStream to provide methods that are convenient for writing various data types to a file.

QUESTION 3:

Do you suppose that FileOutputStream provides a way to write an int?