ASDV-Java/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/Printed HTMLs/WriteWithAutoClose2.html
2023-03-21 11:43:38 -05:00

64 lines
3.8 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>WriteWithAutoClose2.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}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace}
table {color: #888888; background-color: #313335; font-family: monospace}
.string {color: #6a8759}
.number {color: #6897bb}
.comment {color: #808080}
.whitespace {color: #505050}
.ST2 {color: #ffc66d; font-family: monospace; font-style: italic}
.ST3 {color: #9876aa; font-family: monospace; font-style: italic}
.ST1 {color: #808080; font-family: monospace; font-weight: bold}
.ST0 {color: #287bde}
.literal {color: #cc7832}
.ST4 {font-family: monospace; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 2/Assignments/lab6-Exceptions_CalebFontenot/src/main/java/com/mycompany/lab6/exceptions_calebfontenot/WriteWithAutoClose2.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.mycompany.lab6.exceptions_calebfontenot;
<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">IOException</span><span class="comment">;</span>
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> WriteWithAutoClose2 {
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">main</span>(String[] args) {
<span class="literal">try</span> {
java.io.File file = <span class="literal">new</span> java.io.File(<span class="string">&quot;</span><span class="string">scores.txt</span><span class="string">&quot;</span>);
<span class="literal">if</span> (file.exists()) {
System.<span class="ST3">out</span>.println(<span class="string">&quot;</span><span class="string">File already exists!</span><span class="string">&quot;</span>);
System.<span class="ST4">exit</span>(<span class="number">0</span>);
}
<span class="comment">//throw new IOException(); //Just for testing :)</span>
<span class="literal">try</span> (java.io.PrintWriter output = <span class="literal">new</span> java.io.PrintWriter(file);) {
<span class="comment">// Write formatted output to the file</span>
output.print(<span class="string">&quot;</span><span class="string">John T Smith </span><span class="string">&quot;</span>);
output.println(<span class="number">9</span><span class="number">0</span>);
output.print(<span class="string">&quot;</span><span class="string">Eric K Jones </span><span class="string">&quot;</span>);
output.println(<span class="number">8</span><span class="number">5</span>);
}
} <span class="literal">catch</span> (Exception ex) {
System.<span class="ST3">err</span>.println(<span class="string">&quot;</span><span class="string">Cought exception: </span><span class="string">&quot;</span> + ex);
}
}
}
</pre></body>
</html>