81 lines
5.2 KiB
HTML
81 lines
5.2 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>Exercise17_01.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}
|
|
.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}
|
|
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
|
|
-->
|
|
</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_01.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.EOFException;
|
|
<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.RandomAccessFile;
|
|
|
|
<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_01 {
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST1">main</span>(String[] args)
|
|
{
|
|
<span class="literal">try</span> (RandomAccessFile fileIO = <span class="literal">new</span> RandomAccessFile(<span class="string">"</span><span class="string">Exercise17_01.txt</span><span class="string">"</span>, <span class="string">"</span><span class="string">rw</span><span class="string">"</span>)) {
|
|
<span class="literal">int</span> random = <span class="number">0</span>;
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Writing data to file...</span><span class="string">"</span>);
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">1</span>; i < <span class="number">100</span> + <span class="number">1</span>; ++i) {
|
|
random = (<span class="literal">int</span>) (Math.<span class="ST3">random</span>() * <span class="number">9</span>) + <span class="number">1</span>;
|
|
System.<span class="ST2">out</span>.print(random + <span class="string">"</span> <span class="string">"</span>);
|
|
<span class="literal">if</span> (i != <span class="number">0</span> && i % <span class="number">10</span> == <span class="number">0</span>) {
|
|
System.<span class="ST2">out</span>.println();
|
|
}
|
|
fileIO.writeInt(random);
|
|
}
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">Wrote to the file successfully!</span><span class="string">"</span>);
|
|
System.<span class="ST2">out</span>.println(<span class="string">"</span><span class="string">File contents:</span><span class="string">"</span>);
|
|
fileIO.seek(<span class="number">0</span>);
|
|
<span class="literal">int</span> readIterator = <span class="number">1</span>;
|
|
<span class="literal">while</span> (<span class="literal">true</span>) {
|
|
<span class="literal">try</span> {
|
|
System.<span class="ST2">out</span>.print(fileIO.readInt() + <span class="string">"</span> <span class="string">"</span>);
|
|
<span class="literal">if</span> (readIterator != <span class="number">0</span> && readIterator % <span class="number">10</span> == <span class="number">0</span>) {
|
|
System.<span class="ST2">out</span>.println();
|
|
}
|
|
++readIterator;
|
|
} <span class="literal">catch</span> (EOFException e) {
|
|
<span class="literal">break</span>;
|
|
}
|
|
|
|
}
|
|
|
|
} <span class="literal">catch</span> (Exception ex) {
|
|
System.<span class="ST2">out</span>.println(ex);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
</pre></body>
|
|
</html>
|