Skip to content

Commit f7e0a74

Browse files
committed
Refactor/Streamify
1 parent ad33d22 commit f7e0a74

7 files changed

Lines changed: 39 additions & 37 deletions

iostreams/BasicFileOutput.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
public class BasicFileOutput {
99
static String file = "BasicFileOutput.dat";
10-
public static void
11-
main(String[] args) throws IOException {
10+
public static void main(String[] args) {
1211
try(
1312
BufferedReader in = new BufferedReader(
1413
new StringReader(
@@ -17,10 +16,9 @@ public class BasicFileOutput {
1716
PrintWriter out = new PrintWriter(
1817
new BufferedWriter(new FileWriter(file)))
1918
) {
20-
int lineCount = 1;
21-
String s;
22-
while((s = in.readLine()) != null )
23-
out.println(lineCount++ + ": " + s);
19+
in.lines().forEach(out::println);
20+
} catch(IOException e) {
21+
throw new RuntimeException(e);
2422
}
2523
// Show the stored file:
2624
System.out.println(BufferedInputFile.read(file));

iostreams/BufferedInputFile.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,20 @@
44
// Visit http://OnJava8.com for more book information.
55
// {ValidateByHand}
66
import java.io.*;
7+
import java.util.stream.*;
78

89
public class BufferedInputFile {
9-
public static String
10-
read(String filename) throws IOException {
11-
try(
12-
BufferedReader in = new BufferedReader(
13-
new FileReader(filename))
14-
) {
15-
String s;
16-
StringBuilder sb = new StringBuilder();
17-
while((s = in.readLine())!= null)
18-
sb.append(s + "\n");
19-
return sb.toString();
10+
public static String read(String filename) {
11+
try(BufferedReader in = new BufferedReader(
12+
new FileReader(filename))) {
13+
return in.lines()
14+
.collect(Collectors.joining("\n"));
15+
} catch(IOException e) {
16+
throw new RuntimeException(e);
2017
}
2118
}
22-
public static void
23-
main(String[] args) throws IOException {
24-
System.out.print(read("BufferedInputFile.java"));
19+
public static void main(String[] args) {
20+
System.out.print(
21+
read("BufferedInputFile.java"));
2522
}
2623
}

iostreams/FileOutputShortcut.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,18 @@
77

88
public class FileOutputShortcut {
99
static String file = "FileOutputShortcut.dat";
10-
public static void
11-
main(String[] args) throws IOException {
10+
public static void main(String[] args) {
1211
try(
1312
BufferedReader in = new BufferedReader(
1413
new StringReader(BufferedInputFile.read(
1514
"FileOutputShortcut.java")));
1615
// Here's the shortcut:
1716
PrintWriter out = new PrintWriter(file)
1817
) {
19-
int lineCount = 1;
20-
String s;
21-
while((s = in.readLine()) != null )
22-
out.println(lineCount++ + ": " + s);
18+
in.lines().forEach(out::println);
19+
} catch(IOException e) {
20+
throw new RuntimeException(e);
2321
}
24-
// Show the stored file:
2522
System.out.println(BufferedInputFile.read(file));
2623
}
2724
}

iostreams/FormattedMemoryInput.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
import java.io.*;
77

88
public class FormattedMemoryInput {
9-
public static void
10-
main(String[] args) throws IOException {
9+
public static void main(String[] args) {
1110
try(
1211
DataInputStream in = new DataInputStream(
1312
new ByteArrayInputStream(
@@ -18,7 +17,9 @@ public class FormattedMemoryInput {
1817
while(true)
1918
System.out.write((char)in.readByte());
2019
} catch(EOFException e) {
21-
System.out.println("End of stream");
20+
System.out.println("\nEnd of stream");
21+
} catch(IOException e) {
22+
throw new RuntimeException(e);
2223
}
2324
}
2425
}

iostreams/StoringAndRecoveringData.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
import java.io.*;
66

77
public class StoringAndRecoveringData {
8-
public static void
9-
main(String[] args) throws IOException {
8+
public static void main(String[] args) {
109
try(
1110
DataOutputStream out = new DataOutputStream(
1211
new BufferedOutputStream(
@@ -16,6 +15,8 @@ public class StoringAndRecoveringData {
1615
out.writeUTF("That was pi");
1716
out.writeDouble(1.41413);
1817
out.writeUTF("Square root of 2");
18+
} catch(IOException e) {
19+
throw new RuntimeException(e);
1920
}
2021
try(
2122
DataInputStream in = new DataInputStream(
@@ -28,6 +29,8 @@ public class StoringAndRecoveringData {
2829
System.out.println(in.readUTF());
2930
System.out.println(in.readDouble());
3031
System.out.println(in.readUTF());
32+
} catch(IOException e) {
33+
throw new RuntimeException(e);
3134
}
3235
}
3336
}

iostreams/TestEOF.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
import java.io.*;
88

99
public class TestEOF {
10-
public static void
11-
main(String[] args) throws IOException {
10+
public static void main(String[] args) {
1211
try(
1312
DataInputStream in = new DataInputStream(
1413
new BufferedInputStream(
1514
new FileInputStream("TestEOF.java")))
1615
) {
1716
while(in.available() != 0)
1817
System.out.write(in.readByte());
18+
} catch(IOException e) {
19+
throw new RuntimeException(e);
1920
}
2021
}
2122
}

iostreams/UsingRandomAccessFile.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
public class UsingRandomAccessFile {
88
static String file = "rtest.dat";
9-
static void display() throws IOException {
9+
public static void display() {
1010
try(
1111
RandomAccessFile rf =
1212
new RandomAccessFile(file, "r")
@@ -15,10 +15,11 @@ static void display() throws IOException {
1515
System.out.println(
1616
"Value " + i + ": " + rf.readDouble());
1717
System.out.println(rf.readUTF());
18+
} catch(IOException e) {
19+
throw new RuntimeException(e);
1820
}
1921
}
20-
public static void
21-
main(String[] args) throws IOException {
22+
public static void main(String[] args) {
2223
try(
2324
RandomAccessFile rf =
2425
new RandomAccessFile(file, "rw")
@@ -28,6 +29,8 @@ static void display() throws IOException {
2829
rf.writeUTF("The end of the file");
2930
rf.close();
3031
display();
32+
} catch(IOException e) {
33+
throw new RuntimeException(e);
3134
}
3235
try(
3336
RandomAccessFile rf =
@@ -37,6 +40,8 @@ static void display() throws IOException {
3740
rf.writeDouble(47.0001);
3841
rf.close();
3942
display();
43+
} catch(IOException e) {
44+
throw new RuntimeException(e);
4045
}
4146
}
4247
}

0 commit comments

Comments
 (0)