From f7e0a748d7b0a5364c5e3cfa441788e9ea0ca144 Mon Sep 17 00:00:00 2001 From: Bruce Eckel Date: Sat, 21 Jan 2017 12:51:00 -0800 Subject: [PATCH] Refactor/Streamify --- iostreams/BasicFileOutput.java | 10 ++++------ iostreams/BufferedInputFile.java | 25 +++++++++++-------------- iostreams/FileOutputShortcut.java | 11 ++++------- iostreams/FormattedMemoryInput.java | 7 ++++--- iostreams/StoringAndRecoveringData.java | 7 +++++-- iostreams/TestEOF.java | 5 +++-- iostreams/UsingRandomAccessFile.java | 11 ++++++++--- 7 files changed, 39 insertions(+), 37 deletions(-) diff --git a/iostreams/BasicFileOutput.java b/iostreams/BasicFileOutput.java index eaee9895..810bdc5f 100644 --- a/iostreams/BasicFileOutput.java +++ b/iostreams/BasicFileOutput.java @@ -7,8 +7,7 @@ import java.io.*; public class BasicFileOutput { static String file = "BasicFileOutput.dat"; - public static void - main(String[] args) throws IOException { + public static void main(String[] args) { try( BufferedReader in = new BufferedReader( new StringReader( @@ -17,10 +16,9 @@ public class BasicFileOutput { PrintWriter out = new PrintWriter( new BufferedWriter(new FileWriter(file))) ) { - int lineCount = 1; - String s; - while((s = in.readLine()) != null ) - out.println(lineCount++ + ": " + s); + in.lines().forEach(out::println); + } catch(IOException e) { + throw new RuntimeException(e); } // Show the stored file: System.out.println(BufferedInputFile.read(file)); diff --git a/iostreams/BufferedInputFile.java b/iostreams/BufferedInputFile.java index cee5c3d3..ff7c0caf 100644 --- a/iostreams/BufferedInputFile.java +++ b/iostreams/BufferedInputFile.java @@ -4,23 +4,20 @@ // Visit http://OnJava8.com for more book information. // {ValidateByHand} import java.io.*; +import java.util.stream.*; public class BufferedInputFile { - public static String - read(String filename) throws IOException { - try( - BufferedReader in = new BufferedReader( - new FileReader(filename)) - ) { - String s; - StringBuilder sb = new StringBuilder(); - while((s = in.readLine())!= null) - sb.append(s + "\n"); - return sb.toString(); + public static String read(String filename) { + try(BufferedReader in = new BufferedReader( + new FileReader(filename))) { + return in.lines() + .collect(Collectors.joining("\n")); + } catch(IOException e) { + throw new RuntimeException(e); } } - public static void - main(String[] args) throws IOException { - System.out.print(read("BufferedInputFile.java")); + public static void main(String[] args) { + System.out.print( + read("BufferedInputFile.java")); } } diff --git a/iostreams/FileOutputShortcut.java b/iostreams/FileOutputShortcut.java index 74b73184..f1d47655 100644 --- a/iostreams/FileOutputShortcut.java +++ b/iostreams/FileOutputShortcut.java @@ -7,8 +7,7 @@ import java.io.*; public class FileOutputShortcut { static String file = "FileOutputShortcut.dat"; - public static void - main(String[] args) throws IOException { + public static void main(String[] args) { try( BufferedReader in = new BufferedReader( new StringReader(BufferedInputFile.read( @@ -16,12 +15,10 @@ public class FileOutputShortcut { // Here's the shortcut: PrintWriter out = new PrintWriter(file) ) { - int lineCount = 1; - String s; - while((s = in.readLine()) != null ) - out.println(lineCount++ + ": " + s); + in.lines().forEach(out::println); + } catch(IOException e) { + throw new RuntimeException(e); } - // Show the stored file: System.out.println(BufferedInputFile.read(file)); } } diff --git a/iostreams/FormattedMemoryInput.java b/iostreams/FormattedMemoryInput.java index 556383c4..4e38370c 100644 --- a/iostreams/FormattedMemoryInput.java +++ b/iostreams/FormattedMemoryInput.java @@ -6,8 +6,7 @@ import java.io.*; public class FormattedMemoryInput { - public static void - main(String[] args) throws IOException { + public static void main(String[] args) { try( DataInputStream in = new DataInputStream( new ByteArrayInputStream( @@ -18,7 +17,9 @@ public class FormattedMemoryInput { while(true) System.out.write((char)in.readByte()); } catch(EOFException e) { - System.out.println("End of stream"); + System.out.println("\nEnd of stream"); + } catch(IOException e) { + throw new RuntimeException(e); } } } diff --git a/iostreams/StoringAndRecoveringData.java b/iostreams/StoringAndRecoveringData.java index cda8d864..664eecc0 100644 --- a/iostreams/StoringAndRecoveringData.java +++ b/iostreams/StoringAndRecoveringData.java @@ -5,8 +5,7 @@ import java.io.*; public class StoringAndRecoveringData { - public static void - main(String[] args) throws IOException { + public static void main(String[] args) { try( DataOutputStream out = new DataOutputStream( new BufferedOutputStream( @@ -16,6 +15,8 @@ public class StoringAndRecoveringData { out.writeUTF("That was pi"); out.writeDouble(1.41413); out.writeUTF("Square root of 2"); + } catch(IOException e) { + throw new RuntimeException(e); } try( DataInputStream in = new DataInputStream( @@ -28,6 +29,8 @@ public class StoringAndRecoveringData { System.out.println(in.readUTF()); System.out.println(in.readDouble()); System.out.println(in.readUTF()); + } catch(IOException e) { + throw new RuntimeException(e); } } } diff --git a/iostreams/TestEOF.java b/iostreams/TestEOF.java index 87ff8c98..692ea407 100644 --- a/iostreams/TestEOF.java +++ b/iostreams/TestEOF.java @@ -7,8 +7,7 @@ import java.io.*; public class TestEOF { - public static void - main(String[] args) throws IOException { + public static void main(String[] args) { try( DataInputStream in = new DataInputStream( new BufferedInputStream( @@ -16,6 +15,8 @@ public class TestEOF { ) { while(in.available() != 0) System.out.write(in.readByte()); + } catch(IOException e) { + throw new RuntimeException(e); } } } diff --git a/iostreams/UsingRandomAccessFile.java b/iostreams/UsingRandomAccessFile.java index 8ed27b99..256db78c 100644 --- a/iostreams/UsingRandomAccessFile.java +++ b/iostreams/UsingRandomAccessFile.java @@ -6,7 +6,7 @@ import java.io.*; public class UsingRandomAccessFile { static String file = "rtest.dat"; - static void display() throws IOException { + public static void display() { try( RandomAccessFile rf = new RandomAccessFile(file, "r") @@ -15,10 +15,11 @@ public class UsingRandomAccessFile { System.out.println( "Value " + i + ": " + rf.readDouble()); System.out.println(rf.readUTF()); + } catch(IOException e) { + throw new RuntimeException(e); } } - public static void - main(String[] args) throws IOException { + public static void main(String[] args) { try( RandomAccessFile rf = new RandomAccessFile(file, "rw") @@ -28,6 +29,8 @@ public class UsingRandomAccessFile { rf.writeUTF("The end of the file"); rf.close(); display(); + } catch(IOException e) { + throw new RuntimeException(e); } try( RandomAccessFile rf = @@ -37,6 +40,8 @@ public class UsingRandomAccessFile { rf.writeDouble(47.0001); rf.close(); display(); + } catch(IOException e) { + throw new RuntimeException(e); } } }