Repository navigation
Expand file tree
/
Copy pathOut.java
More file actions
73 lines (58 loc) · 2.51 KB
/
Copy pathOut.java
File metadata and controls
73 lines (58 loc) · 2.51 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
/******************************************************************************
* Compilation: javac Out.java
* Execution: java Out
*
* Writes data of various types to: stdout, file, or socket.
*
******************************************************************************/
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
public class Out {
private PrintWriter out;
// for stdout
public Out(OutputStream os) { out = new PrintWriter(os, true); }
public Out() { this(System.out); }
// for Socket output
public Out(Socket socket) {
try { out = new PrintWriter(socket.getOutputStream(), true); }
catch (IOException ioe) { ioe.printStackTrace(); }
}
// for file output
public Out(String s) {
try { out = new PrintWriter(new FileOutputStream(s), true); }
catch(IOException ioe) { ioe.printStackTrace(); }
}
public void close() { out.close(); }
public void println() { out.println(); out.flush(); }
public void println(Object x) { out.println(x); out.flush(); }
public void println(boolean x) { out.println(x); out.flush(); }
public void println(char x) { out.println(x); out.flush(); }
public void println(double x) { out.println(x); }
public void println(float x) { out.println(x); }
public void println(int x) { out.println(x); }
public void println(long x) { out.println(x); }
public void print() { out.flush(); }
public void print(Object x) { out.print(x); out.flush(); }
public void print(boolean x) { out.print(x); out.flush(); }
public void print(char x) { out.print(x); out.flush(); }
public void print(double x) { out.print(x); out.flush(); }
public void print(float x) { out.print(x); out.flush(); }
public void print(int x) { out.print(x); out.flush(); }
public void print(long x) { out.print(x); out.flush(); }
// This method is just here to test the class
public static void main (String[] args) {
Out out;
String s;
// write to stdout
out = new Out();
out.println("Test 1");
out.close();
// write to a file
out= new Out("test.txt");
out.println("Test 2");
out.close();
}
}