Reading file and writing to output file with line numbers

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
5
down vote

favorite












I started learning java relatively recently as my first computer language independently and would like to see if my approach is efficient. An exercise in the book I am studying from tells me to read a file and write it into a seperate file with each line numbered.



Here is my solution



public class FileStuff

public static void main (String args)

System.out.println("Which file do you want to read");
Scanner in = new Scanner(System.in);
String fileInput = in.next();
System.out.println("Which file do you want to write to?");
String fileOutput = in.next();
Scanner goThrough = null;
PrintWriter print = null;
String content ="";
try

print = new PrintWriter(fileOutput);
goThrough = new Scanner(new File(fileInput));
while(goThrough.hasNextLine())


content = goThrough.nextLine();
writeToLine(content, print);


catch(FileNotFoundException e)

e.getMessage();
finally

goThrough.close();
print.close();



static int lineNumber = 1;
static void writeToLine(String line, PrintWriter out) throws FileNotFoundException

try

out.println(String.format("/* %d */ %s ",lineNumber,line));
lineNumber+=1;

catch(Exception e)

e.getMessage();






Concerns



I have several inquiries about my code as I cannot understand my book clearly, and coding practicing questions.



Firstly, is it ok to initialize goThrough and print outside the try block as null? I did this because I wanted to close the Scanner and PrintWriter inside the finally block, but did not want to define something inside the try block since it is a different scope from the finally block



Secondly: Is it fine to throw FileNotFoundException at my method writeToLine? Or should I be throwing it after public static void main(String args), I have seen it done both ways in my book.







share|improve this question



























    up vote
    5
    down vote

    favorite












    I started learning java relatively recently as my first computer language independently and would like to see if my approach is efficient. An exercise in the book I am studying from tells me to read a file and write it into a seperate file with each line numbered.



    Here is my solution



    public class FileStuff

    public static void main (String args)

    System.out.println("Which file do you want to read");
    Scanner in = new Scanner(System.in);
    String fileInput = in.next();
    System.out.println("Which file do you want to write to?");
    String fileOutput = in.next();
    Scanner goThrough = null;
    PrintWriter print = null;
    String content ="";
    try

    print = new PrintWriter(fileOutput);
    goThrough = new Scanner(new File(fileInput));
    while(goThrough.hasNextLine())


    content = goThrough.nextLine();
    writeToLine(content, print);


    catch(FileNotFoundException e)

    e.getMessage();
    finally

    goThrough.close();
    print.close();



    static int lineNumber = 1;
    static void writeToLine(String line, PrintWriter out) throws FileNotFoundException

    try

    out.println(String.format("/* %d */ %s ",lineNumber,line));
    lineNumber+=1;

    catch(Exception e)

    e.getMessage();






    Concerns



    I have several inquiries about my code as I cannot understand my book clearly, and coding practicing questions.



    Firstly, is it ok to initialize goThrough and print outside the try block as null? I did this because I wanted to close the Scanner and PrintWriter inside the finally block, but did not want to define something inside the try block since it is a different scope from the finally block



    Secondly: Is it fine to throw FileNotFoundException at my method writeToLine? Or should I be throwing it after public static void main(String args), I have seen it done both ways in my book.







    share|improve this question























      up vote
      5
      down vote

      favorite









      up vote
      5
      down vote

      favorite











      I started learning java relatively recently as my first computer language independently and would like to see if my approach is efficient. An exercise in the book I am studying from tells me to read a file and write it into a seperate file with each line numbered.



      Here is my solution



      public class FileStuff

      public static void main (String args)

      System.out.println("Which file do you want to read");
      Scanner in = new Scanner(System.in);
      String fileInput = in.next();
      System.out.println("Which file do you want to write to?");
      String fileOutput = in.next();
      Scanner goThrough = null;
      PrintWriter print = null;
      String content ="";
      try

      print = new PrintWriter(fileOutput);
      goThrough = new Scanner(new File(fileInput));
      while(goThrough.hasNextLine())


      content = goThrough.nextLine();
      writeToLine(content, print);


      catch(FileNotFoundException e)

      e.getMessage();
      finally

      goThrough.close();
      print.close();



      static int lineNumber = 1;
      static void writeToLine(String line, PrintWriter out) throws FileNotFoundException

      try

      out.println(String.format("/* %d */ %s ",lineNumber,line));
      lineNumber+=1;

      catch(Exception e)

      e.getMessage();






      Concerns



      I have several inquiries about my code as I cannot understand my book clearly, and coding practicing questions.



      Firstly, is it ok to initialize goThrough and print outside the try block as null? I did this because I wanted to close the Scanner and PrintWriter inside the finally block, but did not want to define something inside the try block since it is a different scope from the finally block



      Secondly: Is it fine to throw FileNotFoundException at my method writeToLine? Or should I be throwing it after public static void main(String args), I have seen it done both ways in my book.







      share|improve this question













      I started learning java relatively recently as my first computer language independently and would like to see if my approach is efficient. An exercise in the book I am studying from tells me to read a file and write it into a seperate file with each line numbered.



      Here is my solution



      public class FileStuff

      public static void main (String args)

      System.out.println("Which file do you want to read");
      Scanner in = new Scanner(System.in);
      String fileInput = in.next();
      System.out.println("Which file do you want to write to?");
      String fileOutput = in.next();
      Scanner goThrough = null;
      PrintWriter print = null;
      String content ="";
      try

      print = new PrintWriter(fileOutput);
      goThrough = new Scanner(new File(fileInput));
      while(goThrough.hasNextLine())


      content = goThrough.nextLine();
      writeToLine(content, print);


      catch(FileNotFoundException e)

      e.getMessage();
      finally

      goThrough.close();
      print.close();



      static int lineNumber = 1;
      static void writeToLine(String line, PrintWriter out) throws FileNotFoundException

      try

      out.println(String.format("/* %d */ %s ",lineNumber,line));
      lineNumber+=1;

      catch(Exception e)

      e.getMessage();






      Concerns



      I have several inquiries about my code as I cannot understand my book clearly, and coding practicing questions.



      Firstly, is it ok to initialize goThrough and print outside the try block as null? I did this because I wanted to close the Scanner and PrintWriter inside the finally block, but did not want to define something inside the try block since it is a different scope from the finally block



      Secondly: Is it fine to throw FileNotFoundException at my method writeToLine? Or should I be throwing it after public static void main(String args), I have seen it done both ways in my book.









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jan 29 at 23:38









      200_success

      123k14143401




      123k14143401









      asked Jan 29 at 23:28









      bob

      261




      261




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote













          Here are my comments:



          1) File handling



          Your file handling logic is in the right direction:



          1. the File handlers (goThrough and print) are defined outside of the try block,

          2. are opened at the beginning inside the block

          3. are closed in the finally clause.

          However, you forget to protect the code from NPE (NullPointerException):
          at the beginning inside the try block, both handlers contain null. You first open print the output handler, and then goThrough. However, in the finally clause, the order is reversed. So, if an exception is thrown while opening print, goThrough is never initialized and you might encounter NPE in the finally clause. it is absolutely essential to ask separately about each file handler before closing it:



          if (goThrough != null) goThrough.close();
          if (print != null) print.close();


          Note: the best practice rule dictates that you first open the input handler, then the output one, and closes them in the reverse order.



          As you can see, proper file handling is a delicate process that requires careful attention to details at different points in the code. Since many errors were made by many a good developers in many production programs, Java 7 added a feature that automatically handles this logic for you. it is called try-with-resources and it means that you declare and open the handlers at the point of the try block and the compiler adds the correct finally clause. I leave to you as study exercise to go though the tutorial and implement the feature into your code.



          2) Exception handling



          Exception handling in writeToLine() is incorrect:



          1. The method is declared with throws FileNotFoundException clause. However, the code is surrounded with try-catch that catches all exceptions, including the supposedly thrown one.


          2. The catch block contains what looks like a statement fragment that does nothing (but passes compilation). perhaps you meant to throw an Exception?


          3) Design



          This last comment falls into "best practice" category: the main() method does too many unrelated tasks: receive input from user, file handling and main loop. if, for example, you have a separate method that is responsible for communicating with the user, receiving user input and validating it, then perhaps you would be able to check for existence of input file as part of validation... then again, one method for receiving user input and validating it? hmm maybe we can design further break down ...






          share|improve this answer




























            up vote
            0
            down vote













            Your approach to define the variables is fine since you are trying to close using those variables in the finally block.



            But throwing the FileNotFoundException in the writeToLine is actually not required as there are no file related operations other than the write. This exception is likely to be thrown when the file is opened while initializing the goThrough variable.






            share|improve this answer























              Your Answer




              StackExchange.ifUsing("editor", function ()
              return StackExchange.using("mathjaxEditing", function ()
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
              );
              );
              , "mathjax-editing");

              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "196"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );








               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186288%2freading-file-and-writing-to-output-file-with-line-numbers%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote













              Here are my comments:



              1) File handling



              Your file handling logic is in the right direction:



              1. the File handlers (goThrough and print) are defined outside of the try block,

              2. are opened at the beginning inside the block

              3. are closed in the finally clause.

              However, you forget to protect the code from NPE (NullPointerException):
              at the beginning inside the try block, both handlers contain null. You first open print the output handler, and then goThrough. However, in the finally clause, the order is reversed. So, if an exception is thrown while opening print, goThrough is never initialized and you might encounter NPE in the finally clause. it is absolutely essential to ask separately about each file handler before closing it:



              if (goThrough != null) goThrough.close();
              if (print != null) print.close();


              Note: the best practice rule dictates that you first open the input handler, then the output one, and closes them in the reverse order.



              As you can see, proper file handling is a delicate process that requires careful attention to details at different points in the code. Since many errors were made by many a good developers in many production programs, Java 7 added a feature that automatically handles this logic for you. it is called try-with-resources and it means that you declare and open the handlers at the point of the try block and the compiler adds the correct finally clause. I leave to you as study exercise to go though the tutorial and implement the feature into your code.



              2) Exception handling



              Exception handling in writeToLine() is incorrect:



              1. The method is declared with throws FileNotFoundException clause. However, the code is surrounded with try-catch that catches all exceptions, including the supposedly thrown one.


              2. The catch block contains what looks like a statement fragment that does nothing (but passes compilation). perhaps you meant to throw an Exception?


              3) Design



              This last comment falls into "best practice" category: the main() method does too many unrelated tasks: receive input from user, file handling and main loop. if, for example, you have a separate method that is responsible for communicating with the user, receiving user input and validating it, then perhaps you would be able to check for existence of input file as part of validation... then again, one method for receiving user input and validating it? hmm maybe we can design further break down ...






              share|improve this answer

























                up vote
                2
                down vote













                Here are my comments:



                1) File handling



                Your file handling logic is in the right direction:



                1. the File handlers (goThrough and print) are defined outside of the try block,

                2. are opened at the beginning inside the block

                3. are closed in the finally clause.

                However, you forget to protect the code from NPE (NullPointerException):
                at the beginning inside the try block, both handlers contain null. You first open print the output handler, and then goThrough. However, in the finally clause, the order is reversed. So, if an exception is thrown while opening print, goThrough is never initialized and you might encounter NPE in the finally clause. it is absolutely essential to ask separately about each file handler before closing it:



                if (goThrough != null) goThrough.close();
                if (print != null) print.close();


                Note: the best practice rule dictates that you first open the input handler, then the output one, and closes them in the reverse order.



                As you can see, proper file handling is a delicate process that requires careful attention to details at different points in the code. Since many errors were made by many a good developers in many production programs, Java 7 added a feature that automatically handles this logic for you. it is called try-with-resources and it means that you declare and open the handlers at the point of the try block and the compiler adds the correct finally clause. I leave to you as study exercise to go though the tutorial and implement the feature into your code.



                2) Exception handling



                Exception handling in writeToLine() is incorrect:



                1. The method is declared with throws FileNotFoundException clause. However, the code is surrounded with try-catch that catches all exceptions, including the supposedly thrown one.


                2. The catch block contains what looks like a statement fragment that does nothing (but passes compilation). perhaps you meant to throw an Exception?


                3) Design



                This last comment falls into "best practice" category: the main() method does too many unrelated tasks: receive input from user, file handling and main loop. if, for example, you have a separate method that is responsible for communicating with the user, receiving user input and validating it, then perhaps you would be able to check for existence of input file as part of validation... then again, one method for receiving user input and validating it? hmm maybe we can design further break down ...






                share|improve this answer























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  Here are my comments:



                  1) File handling



                  Your file handling logic is in the right direction:



                  1. the File handlers (goThrough and print) are defined outside of the try block,

                  2. are opened at the beginning inside the block

                  3. are closed in the finally clause.

                  However, you forget to protect the code from NPE (NullPointerException):
                  at the beginning inside the try block, both handlers contain null. You first open print the output handler, and then goThrough. However, in the finally clause, the order is reversed. So, if an exception is thrown while opening print, goThrough is never initialized and you might encounter NPE in the finally clause. it is absolutely essential to ask separately about each file handler before closing it:



                  if (goThrough != null) goThrough.close();
                  if (print != null) print.close();


                  Note: the best practice rule dictates that you first open the input handler, then the output one, and closes them in the reverse order.



                  As you can see, proper file handling is a delicate process that requires careful attention to details at different points in the code. Since many errors were made by many a good developers in many production programs, Java 7 added a feature that automatically handles this logic for you. it is called try-with-resources and it means that you declare and open the handlers at the point of the try block and the compiler adds the correct finally clause. I leave to you as study exercise to go though the tutorial and implement the feature into your code.



                  2) Exception handling



                  Exception handling in writeToLine() is incorrect:



                  1. The method is declared with throws FileNotFoundException clause. However, the code is surrounded with try-catch that catches all exceptions, including the supposedly thrown one.


                  2. The catch block contains what looks like a statement fragment that does nothing (but passes compilation). perhaps you meant to throw an Exception?


                  3) Design



                  This last comment falls into "best practice" category: the main() method does too many unrelated tasks: receive input from user, file handling and main loop. if, for example, you have a separate method that is responsible for communicating with the user, receiving user input and validating it, then perhaps you would be able to check for existence of input file as part of validation... then again, one method for receiving user input and validating it? hmm maybe we can design further break down ...






                  share|improve this answer













                  Here are my comments:



                  1) File handling



                  Your file handling logic is in the right direction:



                  1. the File handlers (goThrough and print) are defined outside of the try block,

                  2. are opened at the beginning inside the block

                  3. are closed in the finally clause.

                  However, you forget to protect the code from NPE (NullPointerException):
                  at the beginning inside the try block, both handlers contain null. You first open print the output handler, and then goThrough. However, in the finally clause, the order is reversed. So, if an exception is thrown while opening print, goThrough is never initialized and you might encounter NPE in the finally clause. it is absolutely essential to ask separately about each file handler before closing it:



                  if (goThrough != null) goThrough.close();
                  if (print != null) print.close();


                  Note: the best practice rule dictates that you first open the input handler, then the output one, and closes them in the reverse order.



                  As you can see, proper file handling is a delicate process that requires careful attention to details at different points in the code. Since many errors were made by many a good developers in many production programs, Java 7 added a feature that automatically handles this logic for you. it is called try-with-resources and it means that you declare and open the handlers at the point of the try block and the compiler adds the correct finally clause. I leave to you as study exercise to go though the tutorial and implement the feature into your code.



                  2) Exception handling



                  Exception handling in writeToLine() is incorrect:



                  1. The method is declared with throws FileNotFoundException clause. However, the code is surrounded with try-catch that catches all exceptions, including the supposedly thrown one.


                  2. The catch block contains what looks like a statement fragment that does nothing (but passes compilation). perhaps you meant to throw an Exception?


                  3) Design



                  This last comment falls into "best practice" category: the main() method does too many unrelated tasks: receive input from user, file handling and main loop. if, for example, you have a separate method that is responsible for communicating with the user, receiving user input and validating it, then perhaps you would be able to check for existence of input file as part of validation... then again, one method for receiving user input and validating it? hmm maybe we can design further break down ...







                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered Jan 30 at 10:01









                  Sharon Ben Asher

                  2,073512




                  2,073512






















                      up vote
                      0
                      down vote













                      Your approach to define the variables is fine since you are trying to close using those variables in the finally block.



                      But throwing the FileNotFoundException in the writeToLine is actually not required as there are no file related operations other than the write. This exception is likely to be thrown when the file is opened while initializing the goThrough variable.






                      share|improve this answer



























                        up vote
                        0
                        down vote













                        Your approach to define the variables is fine since you are trying to close using those variables in the finally block.



                        But throwing the FileNotFoundException in the writeToLine is actually not required as there are no file related operations other than the write. This exception is likely to be thrown when the file is opened while initializing the goThrough variable.






                        share|improve this answer

























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          Your approach to define the variables is fine since you are trying to close using those variables in the finally block.



                          But throwing the FileNotFoundException in the writeToLine is actually not required as there are no file related operations other than the write. This exception is likely to be thrown when the file is opened while initializing the goThrough variable.






                          share|improve this answer















                          Your approach to define the variables is fine since you are trying to close using those variables in the finally block.



                          But throwing the FileNotFoundException in the writeToLine is actually not required as there are no file related operations other than the write. This exception is likely to be thrown when the file is opened while initializing the goThrough variable.







                          share|improve this answer















                          share|improve this answer



                          share|improve this answer








                          edited Jan 30 at 8:17









                          Mast

                          7,33663484




                          7,33663484











                          answered Jan 30 at 7:17









                          user159297

                          1




                          1






















                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186288%2freading-file-and-writing-to-output-file-with-line-numbers%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              Python Lists

                              Aion

                              JavaScript Array Iteration Methods