<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code With Design &#187; binary</title>
	<atom:link href="http://codewithdesign.com/tag/binary/feed/" rel="self" type="application/rss+xml" />
	<link>http://codewithdesign.com</link>
	<description>web application development blog by Caleb Jonasson</description>
	<lastBuildDate>Thu, 15 Sep 2011 17:52:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Writing and reading objects and arrays to a binary file with java</title>
		<link>http://codewithdesign.com/2010/04/07/writing-and-reading-objects-and-arrays-to-a-binary-file-with-java/</link>
		<comments>http://codewithdesign.com/2010/04/07/writing-and-reading-objects-and-arrays-to-a-binary-file-with-java/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 14:59:31 +0000</pubDate>
		<dc:creator>Caleb Jonasson</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[read]]></category>
		<category><![CDATA[write]]></category>

		<guid isPermaLink="false">http://codewithdesign.com/?p=393</guid>
		<description><![CDATA[TweetThis article requires that you have read the following java tutorials or you feel comfortable writing and reading variables to a binary file. Advantage of writing files in binary with java Reading information from a binary file with java Writing information to a binary file with java The first thing that is needed in this [...]


Related posts:<ol><li><a href='http://codewithdesign.com/2010/03/31/reading-information-from-a-binary-file-with-java/' rel='bookmark' title='Permanent Link: Reading information from a binary file with java'>Reading information from a binary file with java</a></li>
<li><a href='http://codewithdesign.com/2010/03/30/writing-information-to-a-binary-file/' rel='bookmark' title='Permanent Link: Writing information to a binary file in java'>Writing information to a binary file in java</a></li>
<li><a href='http://codewithdesign.com/2010/03/31/advantages-to-writing-files-in-binary-with-java/' rel='bookmark' title='Permanent Link: Advantages to writing files in binary with java'>Advantages to writing files in binary with java</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div style = 'float:left; margin-right:5px;'>            <a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-text="Writing and reading objects and arrays to a binary file with java" data-via="" data-url="http://codewithdesign.com/2010/04/07/writing-and-reading-objects-and-arrays-to-a-binary-file-with-java/" en>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><g:plusone size="tall" href="http://codewithdesign.com/2010/04/07/writing-and-reading-objects-and-arrays-to-a-binary-file-with-java/"></g:plusone><p>This article requires that you have read the following java tutorials or you feel comfortable writing and reading variables to a binary file.</p>
<p><a href="http://codewithdesign.com/2010/03/31/advantages-to-writing-files-in-binary-with-java/">Advantage of writing files in binary with java</a></p>
<p><a href="http://codewithdesign.com/2010/03/31/reading-information-from-a-binary-file-with-java/">Reading information from a binary file with java</a></p>
<p><a href="http://codewithdesign.com/2010/03/30/writing-information-to-a-binary-file/">Writing information to a binary file with java</a></p>
<p>The first thing that is needed in this tutorial is an object that we are going to save into binary. This object is going to be a simple object with one instance variable, two constructors, one of which is default and a get name method.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.Serializable</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> Person<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;no name&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> Person<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> x<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        name <span style="color: #339933;">=</span> x<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> name<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>You may notice that at the top of our new object we have a line that says “import java.io.Serializable” This line imports a class that we can use. It is required when we want to turn an object into binary and save it to a file. There is also a line that implements Serializable. This line is needed so the class can use the methods within the class Serializable.</p>
<p>The next piece of code will be added outside of the main method. It is added because we will need to use the file input and output streams along with the object input and output streams. We will also be using an IOException. If you would like to use other exception handling classes you can certainly feel free too. I would advise using the file not found and end of file exception.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileOutputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileInputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.ObjectOutputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.ObjectInputStream</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This next bit of code is going to save what we want for a filename inside of a string variable. After that we will be creating two person objects and two arrays, one being a string type and the other an integer type. The next line of code will create a null output stream then create the output stream while testing it for any errors.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//the filename to create.</span>
        <span style="color: #003399;">String</span> fileName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;file.dat&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//Person Objects</span>
        Person p1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bob&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Person p2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Paul&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//Array to write</span>
        <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> helloThere <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;dog&quot;</span>,<span style="color: #0000ff;">&quot;cat&quot;</span>,<span style="color: #0000ff;">&quot;hamburger&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> thereHello <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">7</span>,<span style="color: #cc66cc;">8</span>,<span style="color: #cc66cc;">9</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//creating the space in memory for the file</span>
        <span style="color: #003399;">ObjectOutputStream</span>  outputStream <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
            outputStream <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ObjectOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileOutputStream</span><span style="color: #009900;">&#40;</span>fileName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not open the file.&quot;</span> <span style="color: #339933;">+</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Writing the objects to a binary file is done in a similar way to writing everything else. Since an array is actually an object it uses the same method as the object person. To write these two kinds of objects we will need to use the try catch in order to handle any errors then inside of that write objects using the outputstream.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
            outputStream.<span style="color: #006633;">writeObject</span><span style="color: #009900;">&#40;</span>p1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            outputStream.<span style="color: #006633;">writeObject</span><span style="color: #009900;">&#40;</span>p2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            outputStream.<span style="color: #006633;">writeObject</span><span style="color: #009900;">&#40;</span>helloThere<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            outputStream.<span style="color: #006633;">writeObject</span><span style="color: #009900;">&#40;</span>thereHello<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            outputStream.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Writing error: &quot;</span> <span style="color: #339933;">+</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Now that we are done writing the objects to the binary file we will need to read the information back into new objects.</p>
<p>We are going to create a new input stream in the same way that we made the output stream. It will first be of type null and then we will truly create it inside of the try catch blocks.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">ObjectInputStream</span> inputStream <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
            inputStream <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ObjectInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileInputStream</span><span style="color: #009900;">&#40;</span>fileName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;There was a problem opening the file: &quot;</span> <span style="color: #339933;">+</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Since we are going to pull the information out of the file and reload them as objects we will need to create new objects in memory to show that we have successfully read the file.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//Create new blank objects to store the data in.</span>
        Person newp1 <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        Person newp2 <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> test <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> test2 <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In order to read the information we will rely on our trusted try and catch blocks to let us test that everything is going as planned. Inside of the try catch blocks there is going to be the code required to load the objects read from memory into the new objects that we created in the code above.</p>
<p>You will also notice that when we saved the object information to a new object we specified what kind of object it was. This casts the information and sets it up as said object so it can once again use the methods that come along with that object type.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
            newp1 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Person<span style="color: #009900;">&#41;</span>inputStream.<span style="color: #006633;">readObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            newp2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Person<span style="color: #009900;">&#41;</span>inputStream.<span style="color: #006633;">readObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            test <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>inputStream.<span style="color: #006633;">readObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            test2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>inputStream.<span style="color: #006633;">readObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            inputStream.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;There was an issue reading from the file: &quot;</span> <span style="color: #339933;">+</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The last thing required is for us to do is test a person and an array. We will do this as follows.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;newp1<span style="color: #000099; font-weight: bold;">\'</span>s name is &quot;</span> <span style="color: #339933;">+</span> newp1.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>test2<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>If everything goes according to plan then we will have no errors. If you do have an error then please refer to the last bit of code to see if you did anything wrong.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* Code with Design
 *
 * Created By: Caleb Jonasson
 *
 * Desc: This class file is used to create a binary file,
 * write values to the file and check for errors along the way.
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileOutputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileInputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.ObjectOutputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.ObjectInputStream</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> writebin <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//the filename to create.</span>
        <span style="color: #003399;">String</span> fileName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;file.dat&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//Person Objects</span>
        Person p1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Bob&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Person p2 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Paul&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//Array to write</span>
        <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> helloThere <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;dog&quot;</span>,<span style="color: #0000ff;">&quot;cat&quot;</span>,<span style="color: #0000ff;">&quot;hamburger&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> thereHello <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">7</span>,<span style="color: #cc66cc;">8</span>,<span style="color: #cc66cc;">9</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//creating the space in memory for the file</span>
        <span style="color: #003399;">ObjectOutputStream</span>  outputStream <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
            outputStream <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ObjectOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileOutputStream</span><span style="color: #009900;">&#40;</span>fileName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not open the file.&quot;</span> <span style="color: #339933;">+</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
        <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
            outputStream.<span style="color: #006633;">writeObject</span><span style="color: #009900;">&#40;</span>p1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            outputStream.<span style="color: #006633;">writeObject</span><span style="color: #009900;">&#40;</span>p2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            outputStream.<span style="color: #006633;">writeObject</span><span style="color: #009900;">&#40;</span>helloThere<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            outputStream.<span style="color: #006633;">writeObject</span><span style="color: #009900;">&#40;</span>thereHello<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            outputStream.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Writing error: &quot;</span> <span style="color: #339933;">+</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Records have been written to the file.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
        <span style="color: #666666; font-style: italic;">/*Reading the objects from memory.*/</span>
&nbsp;
        <span style="color: #003399;">ObjectInputStream</span> inputStream <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
            inputStream <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ObjectInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileInputStream</span><span style="color: #009900;">&#40;</span>fileName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;There was a problem opening the file: &quot;</span> <span style="color: #339933;">+</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//Create new blank objects to store the data in.</span>
        Person newp1 <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        Person newp2 <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> test <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> test2 <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
            newp1 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Person<span style="color: #009900;">&#41;</span>inputStream.<span style="color: #006633;">readObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            newp2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Person<span style="color: #009900;">&#41;</span>inputStream.<span style="color: #006633;">readObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            test <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>inputStream.<span style="color: #006633;">readObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            test2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>inputStream.<span style="color: #006633;">readObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            inputStream.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;There was an issue reading from the file: &quot;</span> <span style="color: #339933;">+</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Reading of the file: &quot;</span> <span style="color: #339933;">+</span> fileName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; has been completed.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//Now we test to make sure that it worked correctly.</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;newp1<span style="color: #000099; font-weight: bold;">\'</span>s name is &quot;</span> <span style="color: #339933;">+</span> newp1.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>test2<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<img src="http://codewithdesign.com/?ak_action=api_record_view&id=393&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://codewithdesign.com/2010/03/31/reading-information-from-a-binary-file-with-java/' rel='bookmark' title='Permanent Link: Reading information from a binary file with java'>Reading information from a binary file with java</a></li>
<li><a href='http://codewithdesign.com/2010/03/30/writing-information-to-a-binary-file/' rel='bookmark' title='Permanent Link: Writing information to a binary file in java'>Writing information to a binary file in java</a></li>
<li><a href='http://codewithdesign.com/2010/03/31/advantages-to-writing-files-in-binary-with-java/' rel='bookmark' title='Permanent Link: Advantages to writing files in binary with java'>Advantages to writing files in binary with java</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://codewithdesign.com/2010/04/07/writing-and-reading-objects-and-arrays-to-a-binary-file-with-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Advantages to writing files in binary with java</title>
		<link>http://codewithdesign.com/2010/03/31/advantages-to-writing-files-in-binary-with-java/</link>
		<comments>http://codewithdesign.com/2010/03/31/advantages-to-writing-files-in-binary-with-java/#comments</comments>
		<pubDate>Wed, 31 Mar 2010 08:23:38 +0000</pubDate>
		<dc:creator>Caleb Jonasson</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[ObjectOutputStream]]></category>
		<category><![CDATA[precision]]></category>
		<category><![CDATA[space]]></category>
		<category><![CDATA[speed]]></category>

		<guid isPermaLink="false">http://codewithdesign.com/?p=352</guid>
		<description><![CDATA[There are quite a few reasons that a programmer would like to write to a file in binary but the main reasons are speed, space and precision. Along with those three it is also important to understand when you are going to want to write in binary and when not to.


Related posts:<ol><li><a href='http://codewithdesign.com/2010/04/07/writing-and-reading-objects-and-arrays-to-a-binary-file-with-java/' rel='bookmark' title='Permanent Link: Writing and reading objects and arrays to a binary file with java'>Writing and reading objects and arrays to a binary file with java</a></li>
<li><a href='http://codewithdesign.com/2010/03/31/reading-information-from-a-binary-file-with-java/' rel='bookmark' title='Permanent Link: Reading information from a binary file with java'>Reading information from a binary file with java</a></li>
<li><a href='http://codewithdesign.com/2010/03/30/writing-information-to-a-binary-file/' rel='bookmark' title='Permanent Link: Writing information to a binary file in java'>Writing information to a binary file in java</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div style = 'float:left; margin-right:5px;'>            <a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-text="Advantages to writing files in binary with java" data-via="" data-url="http://codewithdesign.com/2010/03/31/advantages-to-writing-files-in-binary-with-java/" en>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><g:plusone size="tall" href="http://codewithdesign.com/2010/03/31/advantages-to-writing-files-in-binary-with-java/"></g:plusone><p>There are quite a few reasons that a programmer would like to write to a file in binary but the main reasons are speed, space and precision. Along with those three it is also important to understand when you are going to want to write in binary and when not to.</p>
<p>Speed is a great advantage to writing to files in binary. It allows you to simply pull the information that you want and load it into variables and objects effectively and easily. As you would try to do the same with its counter part you would instead have to go through each line, looping and parsing the information as you go which is not the most effective way to read a file. Using the methods that come with the ObjectOutputStream you can simply write “writeInt(variableInt);” and the data will be written to the file in binary.</p>
<p>The second great advantage to writing is binary is the space. Your files will be much more compact when everything is saved in binary and this makes an especially large difference when you have to pull large amounts of information from a file. Since everything is made up of a set amount of bytes you can compress more into one file by not having any white space.</p>
<p>The third reason to why binary is a good alternative is the precision of writing what is in memory to the file. There is no parsing, the files are written and pulled as they once where when they were placed in memory at one point in time. This allows for less of a risk factor for your programs to stop working on a client and will allow you to have more trust in what you are pulling out of a saved file.</p>
<p>There are times that you will want to write to a file using the full print writer over binary and this is when you have something like a crash log that allows a user to read what happened when the program crashed, where the bug was etc. Aside from information you want to be seen the upside of binary is quite significant. It allows you to have your own set pattern as to writing and pulling information making it harder for someone to go into your file and know what they are changing. And example of this would be a video game. You don’t want someone able to simple log in and change his or her score. You would be better off if they were unable to read the file.</p>
<p>Keeping information secretive is a great part about binary files. Sure you can crack it using patterns but it is less likely a general user will be able to do so unless they know how the program was written.</p>
<img src="http://codewithdesign.com/?ak_action=api_record_view&id=352&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://codewithdesign.com/2010/04/07/writing-and-reading-objects-and-arrays-to-a-binary-file-with-java/' rel='bookmark' title='Permanent Link: Writing and reading objects and arrays to a binary file with java'>Writing and reading objects and arrays to a binary file with java</a></li>
<li><a href='http://codewithdesign.com/2010/03/31/reading-information-from-a-binary-file-with-java/' rel='bookmark' title='Permanent Link: Reading information from a binary file with java'>Reading information from a binary file with java</a></li>
<li><a href='http://codewithdesign.com/2010/03/30/writing-information-to-a-binary-file/' rel='bookmark' title='Permanent Link: Writing information to a binary file in java'>Writing information to a binary file in java</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://codewithdesign.com/2010/03/31/advantages-to-writing-files-in-binary-with-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Reading information from a binary file with java</title>
		<link>http://codewithdesign.com/2010/03/31/reading-information-from-a-binary-file-with-java/</link>
		<comments>http://codewithdesign.com/2010/03/31/reading-information-from-a-binary-file-with-java/#comments</comments>
		<pubDate>Wed, 31 Mar 2010 07:19:12 +0000</pubDate>
		<dc:creator>Caleb Jonasson</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[IO]]></category>
		<category><![CDATA[objectinputstream]]></category>
		<category><![CDATA[reading]]></category>

		<guid isPermaLink="false">http://codewithdesign.com/?p=359</guid>
		<description><![CDATA[Since we have already written our data to the file and closed it we will need to open the file to read it using the input stream. Like in the last tutorial we are going to need to associate the memory to blank then try to load the file that we created earlier. The code will look something like this.


Related posts:<ol><li><a href='http://codewithdesign.com/2010/04/07/writing-and-reading-objects-and-arrays-to-a-binary-file-with-java/' rel='bookmark' title='Permanent Link: Writing and reading objects and arrays to a binary file with java'>Writing and reading objects and arrays to a binary file with java</a></li>
<li><a href='http://codewithdesign.com/2010/03/30/writing-information-to-a-binary-file/' rel='bookmark' title='Permanent Link: Writing information to a binary file in java'>Writing information to a binary file in java</a></li>
<li><a href='http://codewithdesign.com/2010/01/15/creating-user-input-with-menus-in-java/' rel='bookmark' title='Permanent Link: Creating user input with menus in java'>Creating user input with menus in java</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div style = 'float:left; margin-right:5px;'>            <a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-text="Reading information from a binary file with java" data-via="" data-url="http://codewithdesign.com/2010/03/31/reading-information-from-a-binary-file-with-java/" en>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><g:plusone size="tall" href="http://codewithdesign.com/2010/03/31/reading-information-from-a-binary-file-with-java/"></g:plusone><p>This tutorial is an extension from <a href="http://codewithdesign.com/2010/03/30/writing-information-to-a-binary-file/">“writing information to a binary file with java.”</a> It is in your best interest to read the previous article since this one will be continuing the code.</p>
<p>Previously we created a bunch of primitive data types along with a string. These files have been written to the data file entitled “file.dat” If you did not want to follow the previous article you can download the article here along with the data file within it.</p>
<p>We are going to want to add more classes to import. The classes that we need to import are the “ObjectInputStream” and “FileInputStream.” You should now have the following classes imported into your file.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileInputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileOutputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.ObjectInputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.ObjectOutputStream</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Since we have already written our data to the file and closed it we will need to open the file to read it using the input stream. Like in the last tutorial we are going to need to associate the memory to blank then try to load the file that we created earlier. The code will look something like this.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">ObjectInputStream</span> inputStream <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
            inputStream <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ObjectInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileInputStream</span><span style="color: #009900;">&#40;</span>fileName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;There was a problem opening the file: &quot;</span> <span style="color: #339933;">+</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The next thing to do is read the binary in the same order that it was written, top to bottom, so if we first write an integer then boolean we need to read an integer then a boolean or else we are going to get some wrong numbers coming back at us. The next part of the code is also in try catch using the Exception e.</p>
<p>Please note that it is good practice to place individual try catch whenever you read from the file and load into memory. This will allow you to better find errors but for the sake of this article we are going to save the space.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">byte</span> var1 <span style="color: #339933;">=</span> inputStream.<span style="color: #006633;">readByte</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">short</span> var2 <span style="color: #339933;">=</span> inputStream.<span style="color: #006633;">readShort</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">int</span> var3 <span style="color: #339933;">=</span> inputStream.<span style="color: #006633;">readInt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">long</span> var4 <span style="color: #339933;">=</span> inputStream.<span style="color: #006633;">readLong</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">float</span> var5 <span style="color: #339933;">=</span> inputStream.<span style="color: #006633;">readFloat</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">double</span> var6 <span style="color: #339933;">=</span> inputStream.<span style="color: #006633;">readDouble</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">char</span> var7 <span style="color: #339933;">=</span> inputStream.<span style="color: #006633;">readChar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">String</span> var8 <span style="color: #339933;">=</span> inputStream.<span style="color: #006633;">readUTF</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">boolean</span> b1 <span style="color: #339933;">=</span> inputStream.<span style="color: #006633;">readBoolean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            inputStream.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;There was an issue reading from the file: &quot;</span> <span style="color: #339933;">+</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>If you would like to take a look at all of the code used in this demo it is placed below for your convenience.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">/* Code with Design
 *
 * Created By: Caleb Jonasson
 *
 * Desc: This class file is used to create a binary file,
 * write values to the file and check for errors along the way.
 */</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileOutputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileInputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.ObjectOutputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.ObjectInputStream</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> writebin <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//the filename to create.</span>
        <span style="color: #003399;">String</span> fileName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;file.dat&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//creating the space in memory for the file</span>
        <span style="color: #003399;">ObjectOutputStream</span>  outputStream <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
            outputStream <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ObjectOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileOutputStream</span><span style="color: #009900;">&#40;</span>fileName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not open the file.&quot;</span> <span style="color: #339933;">+</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">byte</span> foo <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">short</span> bar <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span> baz <span style="color: #339933;">=</span> <span style="color: #cc66cc;">543</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">long</span> qurk <span style="color: #339933;">=</span> <span style="color: #cc66cc;">123923892</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">float</span> flt <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">double</span> dbl <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1.2323</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">char</span> chr <span style="color: #339933;">=</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> str <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;hello&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">boolean</span> bool <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">//integet based</span>
            outputStream.<span style="color: #006633;">writeByte</span><span style="color: #009900;">&#40;</span>foo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            outputStream.<span style="color: #006633;">writeShort</span><span style="color: #009900;">&#40;</span>bar<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            outputStream.<span style="color: #006633;">writeInt</span><span style="color: #009900;">&#40;</span>baz<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            outputStream.<span style="color: #006633;">writeLong</span><span style="color: #009900;">&#40;</span>qurk<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">//decimal based</span>
            outputStream.<span style="color: #006633;">writeFloat</span><span style="color: #009900;">&#40;</span>flt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            outputStream.<span style="color: #006633;">writeDouble</span><span style="color: #009900;">&#40;</span>dbl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">//alpha based</span>
            outputStream.<span style="color: #006633;">writeChar</span><span style="color: #009900;">&#40;</span>chr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            outputStream.<span style="color: #006633;">writeUTF</span><span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">//true false based</span>
            outputStream.<span style="color: #006633;">writeBoolean</span><span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            outputStream.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Writing error: &quot;</span> <span style="color: #339933;">+</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Records have been written to the file.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">ObjectInputStream</span> inputStream <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
            inputStream <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ObjectInputStream</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileInputStream</span><span style="color: #009900;">&#40;</span>fileName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;There was a problem opening the file: &quot;</span> <span style="color: #339933;">+</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">byte</span> var1 <span style="color: #339933;">=</span> inputStream.<span style="color: #006633;">readByte</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">short</span> var2 <span style="color: #339933;">=</span> inputStream.<span style="color: #006633;">readShort</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">int</span> var3 <span style="color: #339933;">=</span> inputStream.<span style="color: #006633;">readInt</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">long</span> var4 <span style="color: #339933;">=</span> inputStream.<span style="color: #006633;">readLong</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">float</span> var5 <span style="color: #339933;">=</span> inputStream.<span style="color: #006633;">readFloat</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">double</span> var6 <span style="color: #339933;">=</span> inputStream.<span style="color: #006633;">readDouble</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">char</span> var7 <span style="color: #339933;">=</span> inputStream.<span style="color: #006633;">readChar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">String</span> var8 <span style="color: #339933;">=</span> inputStream.<span style="color: #006633;">readUTF</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000066; font-weight: bold;">boolean</span> b1 <span style="color: #339933;">=</span> inputStream.<span style="color: #006633;">readBoolean</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            inputStream.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;There was an issue reading from the file: &quot;</span> <span style="color: #339933;">+</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Reading of the file: &quot;</span> <span style="color: #339933;">+</span> fileName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; has been completed.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<img src="http://codewithdesign.com/?ak_action=api_record_view&id=359&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://codewithdesign.com/2010/04/07/writing-and-reading-objects-and-arrays-to-a-binary-file-with-java/' rel='bookmark' title='Permanent Link: Writing and reading objects and arrays to a binary file with java'>Writing and reading objects and arrays to a binary file with java</a></li>
<li><a href='http://codewithdesign.com/2010/03/30/writing-information-to-a-binary-file/' rel='bookmark' title='Permanent Link: Writing information to a binary file in java'>Writing information to a binary file in java</a></li>
<li><a href='http://codewithdesign.com/2010/01/15/creating-user-input-with-menus-in-java/' rel='bookmark' title='Permanent Link: Creating user input with menus in java'>Creating user input with menus in java</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://codewithdesign.com/2010/03/31/reading-information-from-a-binary-file-with-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Writing information to a binary file in java</title>
		<link>http://codewithdesign.com/2010/03/30/writing-information-to-a-binary-file/</link>
		<comments>http://codewithdesign.com/2010/03/30/writing-information-to-a-binary-file/#comments</comments>
		<pubDate>Wed, 31 Mar 2010 02:03:13 +0000</pubDate>
		<dc:creator>Caleb Jonasson</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[IO]]></category>
		<category><![CDATA[ObjectOutputStream]]></category>
		<category><![CDATA[outputStream]]></category>
		<category><![CDATA[stream]]></category>
		<category><![CDATA[write]]></category>

		<guid isPermaLink="false">http://codewithdesign.com/?p=348</guid>
		<description><![CDATA[In this tutorial I will be showing you how to import the required IO classes, check for errors using a try catch and write to a binary file. In order to do this tutorial a basic knowledge of Java, methods and variables is required.


Related posts:<ol><li><a href='http://codewithdesign.com/2010/03/31/reading-information-from-a-binary-file-with-java/' rel='bookmark' title='Permanent Link: Reading information from a binary file with java'>Reading information from a binary file with java</a></li>
<li><a href='http://codewithdesign.com/2010/04/07/writing-and-reading-objects-and-arrays-to-a-binary-file-with-java/' rel='bookmark' title='Permanent Link: Writing and reading objects and arrays to a binary file with java'>Writing and reading objects and arrays to a binary file with java</a></li>
<li><a href='http://codewithdesign.com/2010/03/31/advantages-to-writing-files-in-binary-with-java/' rel='bookmark' title='Permanent Link: Advantages to writing files in binary with java'>Advantages to writing files in binary with java</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div style = 'float:left; margin-right:5px;'>            <a href="http://twitter.com/share" class="twitter-share-button" data-count="vertical" data-text="Writing information to a binary file in java" data-via="" data-url="http://codewithdesign.com/2010/03/30/writing-information-to-a-binary-file/" en>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></div><g:plusone size="tall" href="http://codewithdesign.com/2010/03/30/writing-information-to-a-binary-file/"></g:plusone><p>In this tutorial I will be showing you how to import the required IO classes, check for errors using a try catch and write to a binary file. In order to do this tutorial a basic knowledge of Java, methods and variables is required.</p>
<p><a href="http://codewithdesign.com/wp-content/uploads/2010/03/write_information_to_binary_java.zip">Get the files used in this tutorial.</a></p>
<p>The first thing we need to do is import our classes. The classes we will import are going to be the following: “ObjectOutputStream”, “FileOutputStream” and the IOException.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileOutputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.ObjectOutputStream</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The next thing that is required is to create the object “ObjectOutputStream.” We will do this by using the classes we imported at the start of this tutorial. We will also be placing this into a try catch statement to see if we are properly opening the file.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> fileName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;file.dat&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
            outputStream <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ObjectOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileOutputStream</span><span style="color: #009900;">&#40;</span>fileName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not open the file.&quot;</span> <span style="color: #339933;">+</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Take note that we are getting the file name from the a string variable and using it in our new file stream.</p>
<p>Since we now have our file created and classes imported the next thing required is for us to create all of the variables we will be using in this example. Since I will be using all of the primitive variable types there will be a fair sized list you will need to type out.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">byte</span> foo <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">short</span> bar <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span> baz <span style="color: #339933;">=</span> <span style="color: #cc66cc;">543</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">long</span> qurk <span style="color: #339933;">=</span> <span style="color: #cc66cc;">123923892</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">float</span> flt <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">double</span> dbl <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1.2323</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">char</span> chr <span style="color: #339933;">=</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> str <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;hello&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">boolean</span> bool <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The next step is to write all of the variables to the file through the output stream. Once again this will be done using try catch block but you shouldn&#8217;t have a problem if you follow along correctly.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">//integet based</span>
            outputStream.<span style="color: #006633;">writeByte</span><span style="color: #009900;">&#40;</span>foo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            outputStream.<span style="color: #006633;">writeShort</span><span style="color: #009900;">&#40;</span>bar<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            outputStream.<span style="color: #006633;">writeInt</span><span style="color: #009900;">&#40;</span>baz<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            outputStream.<span style="color: #006633;">writeLong</span><span style="color: #009900;">&#40;</span>qurk<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">//decimal based</span>
            outputStream.<span style="color: #006633;">writeFloat</span><span style="color: #009900;">&#40;</span>flt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            outputStream.<span style="color: #006633;">writeDouble</span><span style="color: #009900;">&#40;</span>dbl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">//alpha based</span>
            outputStream.<span style="color: #006633;">writeChar</span><span style="color: #009900;">&#40;</span>chr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            outputStream.<span style="color: #006633;">writeUTF</span><span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">//true false based</span>
            outputStream.<span style="color: #006633;">writeBoolean</span><span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            outputStream.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Writing error: &quot;</span> <span style="color: #339933;">+</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The final piece of the code will close the output stream and tell us that we have successfully written to a file.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Records have been written to the file.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The file with everything in it is as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.FileOutputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.IOException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.ObjectOutputStream</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> writebin <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">String</span> fileName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;file.dat&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">ObjectOutputStream</span>  outputStream <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
            outputStream <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ObjectOutputStream</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileOutputStream</span><span style="color: #009900;">&#40;</span>fileName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not open the file.&quot;</span> <span style="color: #339933;">+</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">byte</span> foo <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">short</span> bar <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span> baz <span style="color: #339933;">=</span> <span style="color: #cc66cc;">543</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">long</span> qurk <span style="color: #339933;">=</span> <span style="color: #cc66cc;">123923892</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">float</span> flt <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">double</span> dbl <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1.2323</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">char</span> chr <span style="color: #339933;">=</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> str <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;hello&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">boolean</span> bool <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">//integet based</span>
            outputStream.<span style="color: #006633;">writeByte</span><span style="color: #009900;">&#40;</span>foo<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            outputStream.<span style="color: #006633;">writeShort</span><span style="color: #009900;">&#40;</span>bar<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            outputStream.<span style="color: #006633;">writeInt</span><span style="color: #009900;">&#40;</span>baz<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            outputStream.<span style="color: #006633;">writeLong</span><span style="color: #009900;">&#40;</span>qurk<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">//decimal based</span>
            outputStream.<span style="color: #006633;">writeFloat</span><span style="color: #009900;">&#40;</span>flt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            outputStream.<span style="color: #006633;">writeDouble</span><span style="color: #009900;">&#40;</span>dbl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">//alpha based</span>
            outputStream.<span style="color: #006633;">writeChar</span><span style="color: #009900;">&#40;</span>chr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            outputStream.<span style="color: #006633;">writeUTF</span><span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">//true false based</span>
            outputStream.<span style="color: #006633;">writeBoolean</span><span style="color: #009900;">&#40;</span>bool<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            outputStream.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Writing error: &quot;</span> <span style="color: #339933;">+</span> e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Records have been written to the file.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<img src="http://codewithdesign.com/?ak_action=api_record_view&id=348&type=feed" alt="" />

<p>Related posts:<ol><li><a href='http://codewithdesign.com/2010/03/31/reading-information-from-a-binary-file-with-java/' rel='bookmark' title='Permanent Link: Reading information from a binary file with java'>Reading information from a binary file with java</a></li>
<li><a href='http://codewithdesign.com/2010/04/07/writing-and-reading-objects-and-arrays-to-a-binary-file-with-java/' rel='bookmark' title='Permanent Link: Writing and reading objects and arrays to a binary file with java'>Writing and reading objects and arrays to a binary file with java</a></li>
<li><a href='http://codewithdesign.com/2010/03/31/advantages-to-writing-files-in-binary-with-java/' rel='bookmark' title='Permanent Link: Advantages to writing files in binary with java'>Advantages to writing files in binary with java</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://codewithdesign.com/2010/03/30/writing-information-to-a-binary-file/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

