-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtutorial_49.java
255 lines (211 loc) · 6.68 KB
/
tutorial_49.java
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
/**
* FILE 1
* javac BasicExamples.java
* java BasicExamples
*/
import java.util.Scanner;
import java.io.*;
/**
* Nano timer for timing things.
*/
class NanoTimer {
private long elapsed;
//
public void startTimer() {
elapsed = System.nanoTime();
}
//
public long stopTimer() {
return (System.nanoTime() - elapsed);
}
//
}
/**
* Read and Write to file.
*/
class BasicExamples {
//
public static void main(String[] args) throws IOException {
// write to a file
try (FileWriter fw = new FileWriter("file1.txt")) {
fw.write("Test this.");
}
try (PrintWriter pw = new PrintWriter("file2.txt")){
pw.println("Test this too.");
pw.printf("My name is %s", "Jeremy");
}
// timed examples
NanoTimer nt = new NanoTimer();
nt.startTimer();
try (FileWriter fw = new FileWriter("file3.txt")) {
for (int i = 1; i < 100; i++) {
fw.write("Line number " + i);
fw.write("\n");
}
}
long fwTime = nt.stopTimer();
nt.startTimer();
try (BufferedWriter bw = new BufferedWriter(new FileWriter("file4.txt"))) {
for (int i = 1; i < 100; i++) {
bw.write("Line number " + i);
bw.newLine();
}
}
long bwTime = nt.stopTimer();
// read from a file
try (Scanner sr = new Scanner(new FileReader("file1.txt"))) {
String firstLine = sr.nextLine();
}
try (BufferedReader br = new BufferedReader(new FileReader("file2.txt"))) {
String firstLine = br.readLine();
String secondLine = br.readLine();
}
// timed example
nt.startTimer();
try (Scanner srf = new Scanner(new FileReader("file4.txt"))) {
while(srf.hasNextLine()) {
System.out.println( srf.nextLine() );
}
}
long srfTime = nt.stopTimer();
nt.startTimer();
try (BufferedReader brf = new BufferedReader(new FileReader("file4.txt"))) {
String line;
while ( (line = brf.readLine()) != null ) {
System.out.println( line );
}
}
long brfTime = nt.stopTimer();
System.out.printf("Timer for Write:\n %d = FileWriter\n %d = BufferedWriter\n Timer for Read:\n %d = Scanner\n %d = BufferedReader",
fwTime, bwTime, srfTime, brfTime
);
}
//
}
/**
* FILE 2
* javac ReadWriteProgram.java
* java ReadWriteProgram
*/
import java.util.Scanner;
import java.util.ArrayList;
import java.util.logging.*;
import java.io.*;
/**
* Choose a file, read that file, choose line of file, write that line to a file.
*/
public class ReadWriteProgram {
//
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in);
FileManager fm = new FileManager();
fm.getFiles();
ArrayList<String> fileLines = fm.splitIntoLines();
if (fileLines.size() == 0) {
System.out.println("Program finished.");
return;
}
System.out.println("What line would you like to Read? 1-" + fileLines.size() );
while(!scan.hasNextInt()) {
scan.next();
System.out.print("Not a valid line number. Try again: ");
}
int line = scan.nextInt();
if (line > 0 && line <= fileLines.size()) {
fm.writeTo("line-" + line + ".txt", fileLines.get(line - 1) );
}
else {
System.out.println("Line doesn't exist.");
}
}
//
}
/**
* Read, Write, and List files
*/
class FileManager {
//
private final static Logger logr = Logr.setup( FileManager.class.getName() );
//
ArrayList<String> splitIntoLines() throws IOException {
Scanner scan = new Scanner(System.in);
ArrayList<String> lines = new ArrayList<String>();
String filePath = scan.nextLine();
if (filePath.equals("q")) {
return lines;
}
else if (filePath.equals("a")) {
getFiles();
}
String nextLine = null;
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
try {
while ((nextLine = br.readLine()) != null) {
lines.add(nextLine);
}
}
catch(IOException e) {
System.out.print("Error reading file. Try another ('q' to quit, 'a' for available).\n: ");
lines = splitIntoLines();
}
}
catch(FileNotFoundException e) {
System.out.print("File doesn't exist. Try again ('q' to quit, 'a' for available).\n: ");
lines = splitIntoLines();
}
catch(IOException e) {
System.out.println("Error closing the file. Program shutting down.");
logr.log(Level.SEVERE, "BufferedReader close error.", e);
throw e;
}
return lines;
}
//
void getFiles() {
File[] files = new File(".").listFiles();
System.out.print("What file do you want to read? ('q' to quit). [ " );
for (File file : files) {
if (
file.isFile() &&
!(file.getName().substring(file.getName().lastIndexOf(".") + 1).equals("class")) //ignores files with .class extension
) {
System.out.printf("'%s' ", file.getName());
}
}
System.out.print("]\n: ");
}
//
void writeTo(String fileName, String text) {
try (FileWriter fwriter = new FileWriter(fileName)) {
fwriter.write(text);
}
catch(IOException e) {
logr.log(Level.SEVERE, "Error writing to file.", e);
}
}
//
}
/**
* Log data out
*/
class Logr {
//
public static Logger setup(String name) {
Logger logr = Logger.getLogger(name);
logr.setUseParentHandlers(false);
logr.setLevel(Level.ALL);
try {
FileHandler fh = new FileHandler( name + ".log" );
fh.setLevel(Level.INFO);
logr.addHandler(fh);
}
catch(IOException e) {
ConsoleHandler ch = new ConsoleHandler();
ch.setLevel(Level.INFO);
logr.addHandler(ch);
logr.info("Couldn't setup file logger.");
}
return logr;
}
//
}