-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileInputOutput.html
257 lines (240 loc) · 10.7 KB
/
FileInputOutput.html
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Technical Documentation: File Input and Output in Java</title>
<style>
body {
font-family: Arial, sans-serif;
line-height: 1.6;
margin: 0;
padding: 0;
background-color: #f4f4f4;
color: #333;
}
header {
background-color: #333;
color: #fff;
padding: 10px 0;
text-align: center;
}
header h1 {
margin: 0;
}
nav {
width: 200px;
float: left;
background-color: #444;
color: white;
height: 100vh;
position: sticky;
top: 0;
padding: 15px;
}
nav ul {
list-style-type: none;
padding: 0;
}
nav ul li {
margin: 15px 0;
}
nav ul li a {
color: white;
text-decoration: none;
font-weight: bold;
}
nav ul li a:hover {
color: #f4f4f4;
}
main {
margin-left: 220px;
padding: 20px;
background-color: white;
max-width: 800px;
}
main h2 {
color: #333;
}
code {
display: block;
background-color: #f4f4f4;
padding: 10px;
border-left: 3px solid #333;
margin-bottom: 20px;
white-space: pre-wrap;
}
.example-output {
background-color: #f4f4f4;
border-left: 3px solid #333;
padding: 10px;
margin-bottom: 20px;
}
</style>
</head>
<body>
<header>
<h1>Technical Documentation: File Input and Output in Java</h1>
</header>
<nav>
<ul>
<li><a href="#overview">Overview</a></li>
<li><a href="#file-reader">FileReader</a></li>
<li><a href="#file-writer">FileWriter</a></li>
<li><a href="#buffered-io">Buffered I/O</a></li>
<li><a href="#print-writer">PrintWriter</a></li>
<li><a href="#exceptions">Handling Exceptions</a></li>
<li><a href="#file-paths">File Paths</a></li>
<li><a href="#summary">Summary</a></li>
<li><a href="#best-practices">Best Practices</a></li>
</ul>
</nav>
<main>
<section id="overview">
<h2>Overview</h2>
<p>File input and output (I/O) in Java allows programs to read from and write to external files. Java provides several classes and methods for handling file I/O, all contained within the <code>java.io</code> package. This documentation covers essential steps for reading and writing files in Java.</p>
</section>
<section id="file-reader">
<h2>FileReader</h2>
<p><code>FileReader</code> is used to read data from a file in the form of characters. It is often used with <code>BufferedReader</code> for efficient reading.</p>
<h3>Example:</h3>
<code>
import java.io.FileReader;<br>
import java.io.BufferedReader;<br>
import java.io.IOException;<br><br>
public class FileInputExample {<br>
 public static void main(String[] args) {<br>
  try {<br>
   FileReader fileReader = new FileReader("example.txt");<br>
   BufferedReader bufferedReader = new BufferedReader(fileReader);<br>
   String line;<br>
<br>
   while ((line = bufferedReader.readLine()) != null) {<br>
    System.out.println(line);<br>
   }<br>
<br>
   bufferedReader.close();<br>
  } catch (IOException e) {<br>
   e.printStackTrace();<br>
  }<br>
 }<br>
}
</code>
</section>
<section id="file-writer">
<h2>FileWriter</h2>
<p><code>FileWriter</code> writes data to a file in the form of characters. It is often combined with <code>BufferedWriter</code> for efficient writing operations.</p>
<h3>Example:</h3>
<code>
import java.io.FileWriter;<br>
import java.io.BufferedWriter;<br>
import java.io.IOException;<br><br>
public class FileOutputExample {<br>
 public static void main(String[] args) {<br>
  try {<br>
   FileWriter fileWriter = new FileWriter("output.txt");<br>
   BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);<br>
<br>
   bufferedWriter.write("This is an example of writing to a file.");<br>
   bufferedWriter.newLine();<br>
   bufferedWriter.write("Writing another line of text.");<br>
<br>
   bufferedWriter.close();<br>
  } catch (IOException e) {<br>
   e.printStackTrace();<br>
  }<br>
 }<br>
}
</code>
</section>
<section id="buffered-io">
<h2>Buffered I/O</h2>
<p><code>BufferedReader</code> and <code>BufferedWriter</code> are efficient wrappers around <code>FileReader</code> and <code>FileWriter</code> that improve performance by buffering data.</p>
<h3>Example:</h3>
<code>
import java.io.*;<br><br>
public class BufferedFileIOExample {<br>
 public static void main(String[] args) {<br>
  String inputFile = "input.txt";<br>
  String outputFile = "output.txt";<br><br>
  try (<br>
   BufferedReader reader = new BufferedReader(new FileReader(inputFile));<br>
   BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));<br>
  ) {<br>
   String line;<br>
   while ((line = reader.readLine()) != null) {<br>
    writer.write(line);<br>
    writer.newLine();<br>
   }<br>
  } catch (IOException e) {<br>
   e.printStackTrace();<br>
  }<br>
 }<br>
}
</code>
</section>
<section id="print-writer">
<h2>PrintWriter</h2>
<p><code>PrintWriter</code> allows for formatted output to a file, making it useful for writing structured text files. It supports methods such as <code>println()</code>, <code>print()</code>, and <code>printf()</code>.</p>
<h3>Example:</h3>
<code>
import java.io.PrintWriter;<br>
import java.io.FileWriter;<br>
import java.io.IOException;<br><br>
public class PrintWriterExample {<br>
 public static void main(String[] args) {<br>
  try (PrintWriter writer = new PrintWriter(new FileWriter("print_output.txt"))) {<br>
   writer.println("Hello, World!");<br>
   writer.printf("Formatted number: %.2f", 123.456);<br>
  } catch (IOException e) {<br>
   e.printStackTrace();<br>
  }<br>
 }<br>
}
</code>
</section>
<section id="exceptions">
<h2>Handling Exceptions</h2>
<p>File I/O operations in Java can throw several types of exceptions, including:</p>
<ul>
<li><strong><code>FileNotFoundException</code></strong>: Thrown when attempting to open a file that does not exist.</li>
<li><strong><code>IOException</code></strong>: Thrown when an I/O operation fails, such as during reading or writing.</li>
</ul>
<h3>Example of Exception Handling:</h3>
<code>
try {<br>
 BufferedReader reader = new BufferedReader(new FileReader("nonexistent.txt"));<br>
} catch (FileNotFoundException e) {<br>
 System.out.println("The specified file was not found.");<br>
} catch (IOException e) {<br>
 System.out.println("An I/O error occurred.");<br>
}
</code>
</section>
<section id="file-paths">
<h2>File Paths</h2>
<p>Java supports both relative and absolute file paths when working with file input/output:</p>
<h3>Relative Path Example:</h3>
<code>
BufferedReader reader = new BufferedReader(new FileReader("files/example.txt"));
</code>
<h3>Absolute Path Example:</h3>
<code>
BufferedReader reader = new BufferedReader(new FileReader("/home/user/documents/example.txt"));
</code>
</section>
<section id="summary">
<h2>Summary</h2>
<p>Java's file input and output framework provides multiple options for reading and writing files. Using classes such as <code>FileReader</code>, <code>FileWriter</code>, <code>BufferedReader</code>, <code>BufferedWriter</code>, and <code>PrintWriter</code>, developers can efficiently handle I/O tasks while managing exceptions like <code>IOException</code>.</p>
</section>
<section id="best-practices">
<h2>Best Practices</h2>
<ul>
<li><strong>Use Buffered I/O</strong>: Always prefer buffered readers and writers for performance.</li>
<li><strong>Close Resources</strong>: Use <code>try-with-resources</code> or ensure that file streams are properly closed to avoid resource leaks.</li>
<li><strong>Handle Exceptions</strong>: Properly handle exceptions to ensure your program doesn't crash unexpectedly.</li>
</ul>
</section>
</main>
</body>
</html>