- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOut.java
More file actions
Latest commit
129 lines (98 loc) · 4.24 KB
/
Copy pathOut.java
File metadata and controls
129 lines (98 loc) · 4.24 KB
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
importjava.io.*;
/** Simple output to the console and to files.
<p>Copyright (c) 2005 Hanspeter Moessenboeck, University of Linz</p>
<p>This class is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.</p>
<p>This class is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the <a href="http://www.gnu.org/copyleft/gpl.html">
GNU General Public License</a> for more details.</p>
<hr>
<p>This class allows printing formatted data either to the console
or to a file. It is intended to be used in an introductory
programming course when classes, packages and exceptions are unknown
at the beginning. To use it, simply copy Out.class into the
current directory. </p>
<p>All output goes to the current output file, which is initially
the console. Opening a file with open() makes it the new current
output file. Closing a file with close() switches back to the previous
output file.</p>
*/
publicclassOut {
privatestaticPrintStreamout;
privatestaticPrintStream[] stack;
privatestaticintsp;
privatestaticbooleandone;
/** Return true if the previous Out operation was
successful, otherwise return false. */
publicstaticbooleandone() {
returndone && ! out.checkError();
}
/** Print the boolean value b either as "true" or "false". */
publicstaticvoidprint(booleanb) { out.print(b); }
/** Print the character value c. */
publicstaticvoidprint(chars) { out.print(s); }
/** Print the integer value i. */
publicstaticvoidprint(inti) { out.print(i); }
/** Print the long value l. */
publicstaticvoidprint(longl) { out.print(l); }
/** Print the float value f. */
publicstaticvoidprint(floatf) { out.print(f); }
/** Print the double value d. */
publicstaticvoidprint(doubled) { out.print(d); }
/** Print the character array a. */
publicstaticvoidprint(char[] a) { out.print(a); }
/** Print the String s. */
publicstaticvoidprint(Strings) { out.print(s); }
/** Print the Object o as resulting from String.valueOf(o). */
publicstaticvoidprint(Objecto) { out.print(o); }
/** Terminate the current line by writing a line separator string.
On windows this is the character sequence '\r' and '\n' */
publicstaticvoidprintln() { out.println(); }
/** Print the boolean value b and terminate the line. */
publicstaticvoidprintln(booleanb) { out.println(b); }
/** Print the character value c and terminate the line. */
publicstaticvoidprintln(chars) { out.println(s); }
/** Print the integer value i and terminate the line. */
publicstaticvoidprintln(inti) { out.println(i); }
/** Print the long value l and terminate the line. */
publicstaticvoidprintln(longl) { out.println(l); }
/** Print the float value f and terminate the line. */
publicstaticvoidprintln(floatf) { out.println(f); }
/** Print the double value d and terminate the line. */
publicstaticvoidprintln(doubled) { out.println(d); }
/** Print the character array a and terminate the line. */
publicstaticvoidprintln(char[] a) { out.println(a); }
/** Print the String s and terminate the line. */
publicstaticvoidprintln(Strings) { out.println(s); }
/** Print the Object o as resulting from String.valueOf(o)
and terminate the line. */
publicstaticvoidprintln(Objecto) { out.println(o); }
/** Open the file with the name fn as the current output file.
All subsequent output goes to this file until it is closed.
The old output file will be restored when the new output file is closed. */
publicstaticvoidopen(Stringfn) {
try {
PrintStreams = newPrintStream(newFileOutputStream(fn));
stack[sp++] = out;
out = s;
} catch (Exceptione) {
done = false;
}
}
/** Close the current output file.
The previous output file is restored and becomes the current output file. */
publicstaticvoidclose() {
out.flush();
out.close();
if (sp > 0) out = stack[--sp];
}
static { // initializer
done = true;
out = System.out;
stack = newPrintStream[8];
sp = 0;
}
}