Refactor/Streamify
This commit is contained in:
parent
ad33d2247f
commit
f7e0a748d7
@ -7,8 +7,7 @@ import java.io.*;
|
|||||||
|
|
||||||
public class BasicFileOutput {
|
public class BasicFileOutput {
|
||||||
static String file = "BasicFileOutput.dat";
|
static String file = "BasicFileOutput.dat";
|
||||||
public static void
|
public static void main(String[] args) {
|
||||||
main(String[] args) throws IOException {
|
|
||||||
try(
|
try(
|
||||||
BufferedReader in = new BufferedReader(
|
BufferedReader in = new BufferedReader(
|
||||||
new StringReader(
|
new StringReader(
|
||||||
@ -17,10 +16,9 @@ public class BasicFileOutput {
|
|||||||
PrintWriter out = new PrintWriter(
|
PrintWriter out = new PrintWriter(
|
||||||
new BufferedWriter(new FileWriter(file)))
|
new BufferedWriter(new FileWriter(file)))
|
||||||
) {
|
) {
|
||||||
int lineCount = 1;
|
in.lines().forEach(out::println);
|
||||||
String s;
|
} catch(IOException e) {
|
||||||
while((s = in.readLine()) != null )
|
throw new RuntimeException(e);
|
||||||
out.println(lineCount++ + ": " + s);
|
|
||||||
}
|
}
|
||||||
// Show the stored file:
|
// Show the stored file:
|
||||||
System.out.println(BufferedInputFile.read(file));
|
System.out.println(BufferedInputFile.read(file));
|
||||||
|
@ -4,23 +4,20 @@
|
|||||||
// Visit http://OnJava8.com for more book information.
|
// Visit http://OnJava8.com for more book information.
|
||||||
// {ValidateByHand}
|
// {ValidateByHand}
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.util.stream.*;
|
||||||
|
|
||||||
public class BufferedInputFile {
|
public class BufferedInputFile {
|
||||||
public static String
|
public static String read(String filename) {
|
||||||
read(String filename) throws IOException {
|
try(BufferedReader in = new BufferedReader(
|
||||||
try(
|
new FileReader(filename))) {
|
||||||
BufferedReader in = new BufferedReader(
|
return in.lines()
|
||||||
new FileReader(filename))
|
.collect(Collectors.joining("\n"));
|
||||||
) {
|
} catch(IOException e) {
|
||||||
String s;
|
throw new RuntimeException(e);
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
while((s = in.readLine())!= null)
|
|
||||||
sb.append(s + "\n");
|
|
||||||
return sb.toString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public static void
|
public static void main(String[] args) {
|
||||||
main(String[] args) throws IOException {
|
System.out.print(
|
||||||
System.out.print(read("BufferedInputFile.java"));
|
read("BufferedInputFile.java"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,7 @@ import java.io.*;
|
|||||||
|
|
||||||
public class FileOutputShortcut {
|
public class FileOutputShortcut {
|
||||||
static String file = "FileOutputShortcut.dat";
|
static String file = "FileOutputShortcut.dat";
|
||||||
public static void
|
public static void main(String[] args) {
|
||||||
main(String[] args) throws IOException {
|
|
||||||
try(
|
try(
|
||||||
BufferedReader in = new BufferedReader(
|
BufferedReader in = new BufferedReader(
|
||||||
new StringReader(BufferedInputFile.read(
|
new StringReader(BufferedInputFile.read(
|
||||||
@ -16,12 +15,10 @@ public class FileOutputShortcut {
|
|||||||
// Here's the shortcut:
|
// Here's the shortcut:
|
||||||
PrintWriter out = new PrintWriter(file)
|
PrintWriter out = new PrintWriter(file)
|
||||||
) {
|
) {
|
||||||
int lineCount = 1;
|
in.lines().forEach(out::println);
|
||||||
String s;
|
} catch(IOException e) {
|
||||||
while((s = in.readLine()) != null )
|
throw new RuntimeException(e);
|
||||||
out.println(lineCount++ + ": " + s);
|
|
||||||
}
|
}
|
||||||
// Show the stored file:
|
|
||||||
System.out.println(BufferedInputFile.read(file));
|
System.out.println(BufferedInputFile.read(file));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,7 @@
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
public class FormattedMemoryInput {
|
public class FormattedMemoryInput {
|
||||||
public static void
|
public static void main(String[] args) {
|
||||||
main(String[] args) throws IOException {
|
|
||||||
try(
|
try(
|
||||||
DataInputStream in = new DataInputStream(
|
DataInputStream in = new DataInputStream(
|
||||||
new ByteArrayInputStream(
|
new ByteArrayInputStream(
|
||||||
@ -18,7 +17,9 @@ public class FormattedMemoryInput {
|
|||||||
while(true)
|
while(true)
|
||||||
System.out.write((char)in.readByte());
|
System.out.write((char)in.readByte());
|
||||||
} catch(EOFException e) {
|
} catch(EOFException e) {
|
||||||
System.out.println("End of stream");
|
System.out.println("\nEnd of stream");
|
||||||
|
} catch(IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,7 @@
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
public class StoringAndRecoveringData {
|
public class StoringAndRecoveringData {
|
||||||
public static void
|
public static void main(String[] args) {
|
||||||
main(String[] args) throws IOException {
|
|
||||||
try(
|
try(
|
||||||
DataOutputStream out = new DataOutputStream(
|
DataOutputStream out = new DataOutputStream(
|
||||||
new BufferedOutputStream(
|
new BufferedOutputStream(
|
||||||
@ -16,6 +15,8 @@ public class StoringAndRecoveringData {
|
|||||||
out.writeUTF("That was pi");
|
out.writeUTF("That was pi");
|
||||||
out.writeDouble(1.41413);
|
out.writeDouble(1.41413);
|
||||||
out.writeUTF("Square root of 2");
|
out.writeUTF("Square root of 2");
|
||||||
|
} catch(IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
try(
|
try(
|
||||||
DataInputStream in = new DataInputStream(
|
DataInputStream in = new DataInputStream(
|
||||||
@ -28,6 +29,8 @@ public class StoringAndRecoveringData {
|
|||||||
System.out.println(in.readUTF());
|
System.out.println(in.readUTF());
|
||||||
System.out.println(in.readDouble());
|
System.out.println(in.readDouble());
|
||||||
System.out.println(in.readUTF());
|
System.out.println(in.readUTF());
|
||||||
|
} catch(IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,7 @@
|
|||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
public class TestEOF {
|
public class TestEOF {
|
||||||
public static void
|
public static void main(String[] args) {
|
||||||
main(String[] args) throws IOException {
|
|
||||||
try(
|
try(
|
||||||
DataInputStream in = new DataInputStream(
|
DataInputStream in = new DataInputStream(
|
||||||
new BufferedInputStream(
|
new BufferedInputStream(
|
||||||
@ -16,6 +15,8 @@ public class TestEOF {
|
|||||||
) {
|
) {
|
||||||
while(in.available() != 0)
|
while(in.available() != 0)
|
||||||
System.out.write(in.readByte());
|
System.out.write(in.readByte());
|
||||||
|
} catch(IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import java.io.*;
|
|||||||
|
|
||||||
public class UsingRandomAccessFile {
|
public class UsingRandomAccessFile {
|
||||||
static String file = "rtest.dat";
|
static String file = "rtest.dat";
|
||||||
static void display() throws IOException {
|
public static void display() {
|
||||||
try(
|
try(
|
||||||
RandomAccessFile rf =
|
RandomAccessFile rf =
|
||||||
new RandomAccessFile(file, "r")
|
new RandomAccessFile(file, "r")
|
||||||
@ -15,10 +15,11 @@ public class UsingRandomAccessFile {
|
|||||||
System.out.println(
|
System.out.println(
|
||||||
"Value " + i + ": " + rf.readDouble());
|
"Value " + i + ": " + rf.readDouble());
|
||||||
System.out.println(rf.readUTF());
|
System.out.println(rf.readUTF());
|
||||||
|
} catch(IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public static void
|
public static void main(String[] args) {
|
||||||
main(String[] args) throws IOException {
|
|
||||||
try(
|
try(
|
||||||
RandomAccessFile rf =
|
RandomAccessFile rf =
|
||||||
new RandomAccessFile(file, "rw")
|
new RandomAccessFile(file, "rw")
|
||||||
@ -28,6 +29,8 @@ public class UsingRandomAccessFile {
|
|||||||
rf.writeUTF("The end of the file");
|
rf.writeUTF("The end of the file");
|
||||||
rf.close();
|
rf.close();
|
||||||
display();
|
display();
|
||||||
|
} catch(IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
try(
|
try(
|
||||||
RandomAccessFile rf =
|
RandomAccessFile rf =
|
||||||
@ -37,6 +40,8 @@ public class UsingRandomAccessFile {
|
|||||||
rf.writeDouble(47.0001);
|
rf.writeDouble(47.0001);
|
||||||
rf.close();
|
rf.close();
|
||||||
display();
|
display();
|
||||||
|
} catch(IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user