File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 77
88public 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 ));
Original file line number Diff line number Diff line change 44// Visit http://OnJava8.com for more book information.
55// {ValidateByHand}
66import java .io .*;
7+ import java .util .stream .*;
78
89public 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}
Original file line number Diff line number Diff line change 77
88public 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}
Original file line number Diff line number Diff line change 66import java .io .*;
77
88public 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 ("\n End of stream" );
21+ } catch (IOException e ) {
22+ throw new RuntimeException (e );
2223 }
2324 }
2425}
Original file line number Diff line number Diff line change 55import java .io .*;
66
77public 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}
Original file line number Diff line number Diff line change 77import java .io .*;
88
99public 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}
Original file line number Diff line number Diff line change 66
77public 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}
You can’t perform that action at this time.
0 commit comments