File tree Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Original file line number Diff line number Diff line change @@ -34,17 +34,27 @@ public static void numbersExceptionHandling(){
3434 Scanner fileReader = null ;
3535 try {
3636 fileReader = new Scanner (file );
37-
3837 while (fileReader .hasNext ()){
3938 double num = fileReader .nextDouble ();
4039 System .out .println (num );
4140 }
42-
43-
4441 }catch (FileNotFoundException | InputMismatchException e ){
4542 e .printStackTrace ();
4643 }finally {
4744 fileReader .close ();
4845 }
4946 }
47+
48+
49+ public static void tryWithResources (){
50+ File file = new File ("resources/numbers.txt" );
51+ try (Scanner fileReader = new Scanner (file )){
52+ while (fileReader .hasNext ()){
53+ double num = fileReader .nextDouble ();
54+ System .out .println (num );
55+ }
56+ }catch (FileNotFoundException | InputMismatchException e ){
57+ e .printStackTrace ();
58+ }
59+ }
5060}
You can’t perform that action at this time.
0 commit comments