Refactor/Streamify

This commit is contained in:
Bruce Eckel 2017-01-21 12:51:00 -08:00
parent ad33d2247f
commit f7e0a748d7
7 changed files with 39 additions and 37 deletions

View File

@ -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));

View File

@ -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"));
}
}

View File

@ -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));
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}