package com.company; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; public class Main { public static void main(String[] args) throws IOException { MyClass myObject = new MyClass(); OutputStream outStream = new FileOutputStream("c:/my-object-data.txt"); myObject.write(5); while (myObject.available() > 0) { int data = myObject.read(); //read one int from the input stream outStream.write(data); //write that int to the other stream. } outStream.close(); } } class MyClass { private ArrayList<Integer> list; public void write(int data) { list.add(data); } public int read() { int first = list.get(0); list.remove(0); return first; } public int available() { return list.size(); } }