-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIn.java
More file actions
192 lines (157 loc) · 5.03 KB
/
Copy pathIn.java
File metadata and controls
192 lines (157 loc) · 5.03 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
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
/*************************************************************************
* Compilation: javac In.java
* Execution: java In
*
* Reads in data of various types from: stdin, file, URL.
*
* Reads in a string, rest of line, or rest of file and returns
* it as a String or null if there is no more data.
*
* The client can use Integer.parseInt(in.readString()) to read
* in an int.
*
* Typically this class is used with another client, but main()
* provides a testing routine.
*
* % java In
* Test if In.java works.
* Second line.
*
* Test
* if
* In.java
* works.
* Second
* line.
*
* Test if In.java works.
* Second line.
*
*************************************************************************/
import java.net.URLConnection;
import java.net.URL;
import java.net.Socket;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.FileReader;
class In {
private BufferedReader br;
// system independent
private final static String NEWLINE = System.getProperty("line.separator");
// for stdin
public In() {
InputStreamReader isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
}
// for stdin
public In(Socket socket) {
try {
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
} catch (IOException ioe) { ioe.printStackTrace(); }
}
// for URLs
public In(URL url) {
try {
URLConnection site = url.openConnection();
InputStream is = site.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
} catch (IOException ioe) { ioe.printStackTrace(); }
}
// for files and web pages
public In(String s) {
try {
// first try to read file from local file system
File file = new File(s);
if (file.exists()) {
FileReader fr = new FileReader(s);
br = new BufferedReader(fr);
}
// next try for files included in jar
URL url = getClass().getResource(s);
// or URL from web
if (url == null) url = new URL(s);
URLConnection site = url.openConnection();
InputStream is = site.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
} catch(IOException ioe) { }
}
// note read() returns -1 if EOF
private int readChar() {
int c = -1;
try { c = br.read(); }
catch(IOException ioe) { ioe.printStackTrace(); }
return c;
}
// read a token - delete preceding whitespace and one trailing whitespace character
public String readString() {
int c;
while ((c = readChar()) != -1)
if (!Character.isWhitespace((char) c)) break;
if (c == -1) return null;
String s = "" + (char) c;
while ((c = readChar()) != -1)
if (Character.isWhitespace((char) c)) break;
else s += (char) c;
return s;
}
// return rest of line as string and return it, not including newline
public String readLine() {
if (br == null) return null;
String s = null;
try { s = br.readLine(); }
catch(IOException ioe) { ioe.printStackTrace(); }
return s;
}
// return rest of input as string, use StringBuffer to avoid quadratic run time
// don't include NEWLINE at very end
public String readAll() {
StringBuffer sb = new StringBuffer();
String s = readLine();
if (s == null) return null;
sb.append(s);
while ((s = readLine()) != null) {
sb.append(NEWLINE).append(s);
}
return sb.toString();
}
public void close() {
try { br.close(); }
catch (IOException e) { e.printStackTrace(); }
}
// This method is just here to test the class
public static void main (String args[]) {
In in;
String s;
// read from a URL
in = new In("http://xxx/InTest.txt");
System.out.println(in.readAll());
System.out.println();
// read one line at a time from URL
in = new In("http://xxx/InTest.txt");
while ((s = in.readLine()) != null)
System.out.println(s);
System.out.println();
// read one string at a time from URL
in = new In("http://xxxx/InTest.txt");
while ((s = in.readString()) != null)
System.out.println(s);
System.out.println();
// read one line at a time from file in current directory
in = new In("InTest.txt");
while ((s = in.readLine()) != null)
System.out.println(s);
System.out.println();
// read one line at a time from file using relative path
in = new In("../home/index.php");
while ((s = in.readLine()) != null)
System.out.println(s);
System.out.println();
}
}