108 lines
7.0 KiB
HTML
108 lines
7.0 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>Exercise17_05.java</title>
|
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
|
<style type="text/css">
|
|
<!--
|
|
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
|
|
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
|
|
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
|
|
.string {color: #6a8759}
|
|
.number {color: #6897bb}
|
|
.ST3 {color: #9876aa}
|
|
.ST4 {color: #ffc66d}
|
|
.comment {color: #808080}
|
|
.whitespace {color: #505050}
|
|
.ST1 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
|
|
.ST2 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
|
|
.ST0 {color: #287bde}
|
|
.literal {color: #cc7832}
|
|
-->
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 3/Assignments/MP5-Binary-Files_CalebFontenot/src/com/calebfontenot/mp5/files_calebfontenot/Exercise17_05.java</td></tr></table>
|
|
<pre>
|
|
<span class="comment">/*</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
|
|
<span class="comment"> */</span>
|
|
<span class="literal">package</span> com.calebfontenot.mp5.files_calebfontenot;
|
|
|
|
<span class="literal">import</span> java.io.File;
|
|
<span class="literal">import</span> java.io.FileInputStream;
|
|
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">io</span><span class="comment">.</span><span class="comment">FileNotFoundException</span><span class="comment">;</span>
|
|
<span class="literal">import</span> java.io.FileOutputStream;
|
|
<span class="literal">import</span> java.io.IOException;
|
|
<span class="literal">import</span> java.io.ObjectInputStream;
|
|
<span class="literal">import</span> java.io.Serializable;
|
|
<span class="literal">import</span> java.io.ObjectOutputStream;
|
|
<span class="literal">import</span> java.util.Date;
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">class</span> Exercise17_05 {
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args) {
|
|
File dataFile = <span class="literal">new</span> File(<span class="string">"</span><span class="string">Exercise17_05.dat</span><span class="string">"</span>);
|
|
<span class="literal">if</span>(!dataFile.exists()) {
|
|
<span class="literal">try</span> (ObjectOutputStream fileStream = <span class="literal">new</span> ObjectOutputStream(<span class="literal">new</span> FileOutputStream(dataFile))) {
|
|
fileStream.writeObject(<span class="literal">new</span> DataContainer());
|
|
} <span class="literal">catch</span> (IOException ex) {
|
|
System.<span class="ST2">out</span>.println(ex);
|
|
}
|
|
}
|
|
DataContainer data = <span class="literal">null</span>;
|
|
<span class="literal">try</span> (ObjectInputStream fileStream = <span class="literal">new</span> ObjectInputStream(<span class="literal">new</span> FileInputStream(dataFile))) {
|
|
data = (DataContainer) fileStream.readObject();
|
|
} <span class="literal">catch</span> (IOException ex) {
|
|
System.<span class="ST2">out</span>.println(ex);
|
|
} <span class="literal">catch</span> (ClassNotFoundException ex) {
|
|
System.<span class="ST2">out</span>.println(ex);
|
|
}
|
|
<span class="comment">// Now print out the data!</span>
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">We got the data from the file!</span><span class="string">"</span>);
|
|
System.<span class="ST2">out</span>.println(data.toString());
|
|
}
|
|
}
|
|
|
|
<span class="literal">class</span> DataContainer <span class="literal">implements</span> Serializable {
|
|
<span class="literal">int</span>[] <span class="ST3">intArray</span> = {<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>};
|
|
Date <span class="ST3">currentDate</span> = <span class="literal">new</span> Date();
|
|
<span class="literal">double</span> <span class="ST3">doubleMoment</span> = <span class="number">5.5</span>;
|
|
|
|
<span class="literal">public</span> <span class="literal">int</span>[] <span class="ST4">getIntArray</span>()
|
|
{
|
|
<span class="literal">return</span> <span class="ST3">intArray</span>;
|
|
}
|
|
|
|
<span class="literal">public</span> Date <span class="ST4">getCurrentDate</span>()
|
|
{
|
|
<span class="literal">return</span> <span class="ST3">currentDate</span>;
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">double</span> <span class="ST4">getDoubleMoment</span>()
|
|
{
|
|
<span class="literal">return</span> <span class="ST3">doubleMoment</span>;
|
|
}
|
|
|
|
@Override
|
|
<span class="literal">public</span> String <span class="ST4">toString</span>()
|
|
{
|
|
String intString = <span class="string">"</span><span class="string">[</span><span class="string">"</span>;
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < <span class="ST3">intArray</span>.<span class="ST3">length</span> - <span class="number">1</span>; ++i) {
|
|
intString += <span class="ST3">intArray</span>[i];
|
|
<span class="literal">if</span> (i == (<span class="ST3">intArray</span>.<span class="ST3">length</span> - <span class="number">2</span>)) {
|
|
intString += <span class="string">"</span><span class="string">]</span><span class="string">"</span>;
|
|
} <span class="literal">else</span> {
|
|
intString += <span class="string">"</span><span class="string">, </span><span class="string">"</span>;
|
|
}
|
|
}
|
|
<span class="literal">return</span> <span class="string">"</span><span class="string">DataContainer{</span><span class="string">"</span> + <span class="string">"</span><span class="string">intArray=</span><span class="string">"</span> + intString + <span class="string">"</span><span class="string">, currentDate=</span><span class="string">"</span> + <span class="ST3">currentDate</span> + <span class="string">"</span><span class="string">, doubleMoment=</span><span class="string">"</span> + <span class="ST3">doubleMoment</span> + <span class="string">'</span><span class="string">}</span><span class="string">'</span>;
|
|
}
|
|
}
|
|
</pre></body>
|
|
</html>
|