Java Video Tutorial 33

Java Binary StreamsHere I continue covering everything about Java. Today I completely cover reading and writing Java Binary Streams.

I cover everything you need to know including DataOutputStream, FileOutputStream, BufferedOutputStream, all of the reading and writing primitive type methods and more.

All of the code follows the video. It is heavily commented.

If you like videos like this, share it

Code from the Video


// A binary stream is a series of data type values
// To read and write to them you use different methods
// based on the type of data that you are using

public class Lesson33{
	public static void main(String[] args){
		// Create an array of type Customer
		Customer[] customers = getCustomers();
		// A DataOutputStream allows you to print 
		// primitive data types to a file
		DataOutputStream custOutput = createFile("/Users/derekbanas/Documents/workspace3/Java Code/src/customers.dat");
		// Enhanced for loop for arrays
		// Cycles through all of the people in the customers array
		for(Customer person : customers){
			createCustomers(person, custOutput);
		// Closes the connection to the DataOutputStream
		try {
		} catch (IOException e) {
			System.out.println("An I/O Error Occurred");
			// Closes the program
	// class that defines all the fields for my customers
	private static class Customer{
		public String custName;
		public int custAge; 
		public double custDebt;
		public boolean oweMoney;
		public char custSex;
		// constructor that's called when a customer is made
		public Customer(String custName, int custAge, double custDebt, boolean oweMoney, char custSex){
			this.custName = custName; // String
			this.custAge = custAge; // Integer
			this.custDebt = custDebt; // Double
			this.oweMoney = oweMoney; // Boolean
			this.custSex = custSex; // Character
	// Creates an array of Customer Objects
	private static Customer[] getCustomers(){
		Customer[] customers = new Customer[5];
		customers[0] = new Customer("John Smith", 21, 12.25, true, 'M');
		customers[1] = new Customer("Sally Smith", 30, 2.25, true, 'F');
		customers[2] = new Customer("Paul Ryan", 21, 0, false, 'M');
		customers[3] = new Customer("Mark Jacobs", 21, 3.25, true, 'M');
		customers[4] = new Customer("Steve Nash", 21, 5.25, true, 'M');
		return customers;
	// Create the file and the DataOutputStream that will write to the file
	private static DataOutputStream createFile(String fileName){
			// Creates a File object that allows you to work with files 
			// on the hard drive. There is no difference between File
			// for character or binary stream writing, or reading
			File listOfNames = new File(fileName);
			// FileOutputStream is used to write streams of data to a file
			// You define whether a new file is created versus appended
			// to based on if you add a boolean to the FileOutputStream
			// FileOutputStream(file, true) : Appends to the file
			// FileOutputStream(file, false) : Creates a new file
			// BufferedOutputStream gathers all the data and then writes
			// it all at one time (Speeds up the Program)
			// DataOutputStream is used to write primitive data to the file
			DataOutputStream infoToWrite = new DataOutputStream(
			new BufferedOutputStream(
					new FileOutputStream(listOfNames)));
			return infoToWrite;
		// You have to catch this when you call FileWriter
		catch(IOException e){
			System.out.println("An I/O Error Occurred");
			// Closes the program
		return null;
	// Create a string with the customer info and write it to the file
	private static void createCustomers(Customer customer, DataOutputStream custOutput){
		// Write primitive data to the file
		// Writes a String in UTF format
		// Writes an Integer 
		// Writes a Double
		// Writes a Boolean 
		// Writes a Character
		// You also have writeByte, writeFloat, writeLong
		// and writeShort
		catch(IOException e){
			System.out.println("An I/O Error Occurred");
	// Read info from the file and write it to the screen
	private static void getFileInfo(){
		System.out.println("Info Written to File\n");
		// Open a new connection to the file
		File listOfNames = new File("/Users/derekbanas/Documents/workspace3/Java Code/src/customers.dat");
		boolean eof = false;
		try {
			// A DataInputStream object has the methods for reading the data
			// The BufferedInputStream gathers the data in blocks
			// FileInputStream gets data from the file
			DataInputStream getInfo = new DataInputStream(
					new BufferedInputStream(
					new FileInputStream(listOfNames)));
			// Using a while loop that pulls data until EOFException is thrown
			while (!eof){
				// You have to read data in the exact order it was put in the file
				String custName = getInfo.readUTF();
				int custAge = getInfo.readInt(); 
				double custDebt = getInfo.readDouble();
				boolean oweMoney = getInfo.readBoolean();
				char custSex = getInfo.readChar();
				System.out.println(custSex + "\n");
		} // END OF TRY
		catch (EOFException e) {
			eof = true;
		// Can be thrown by FileInputStream
		catch (FileNotFoundException e) {
			System.out.println("Couldn't Find the File");
		catch(IOException e){
			System.out.println("An I/O Error Occurred");

11 Responses to “Java Video Tutorial 33”

  1. mr.roshik says:

    nice as usual , thanks for sharing with us.

  2. mr.roshik says:

    sorry i pasted some wrong code in html, dont mind bro, i linked those tabs but im just unable to use jquery to slide those tabs.

  3. mr.roshik says:

    im really sorry for avobe posts , my problem is solved now 😀 dont mind bro and my english isn’t good enough, but i hope u understand wht i say.:D

  4. Punit says:

    hey what about serialization(object input and object output stream), are you going to teach that also, because what if i want to transfer data over the network.?

  5. giovetti says:

    Just found this series, nice and compact. Very well produced. Thanks for sharing.

  6. Saleh says:

    How did you add the arguments to the constructor properly after copying them without including the access modifiers.

    And how did you initialize them inside the constructor,, you made the first two and the rest modified immediately..

    what tricks you use…
    Is there any reference to it..
    is it eclipse tricks… I am using netbeans.

    Thank you

Leave a Reply

Your email address will not be published.