Reading file and writing to output file with line numbers

Clash 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.
java beginner file io
add a comment |Â
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.
java beginner file io
add a comment |Â
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.
java beginner file io
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.
java beginner file io
edited Jan 29 at 23:38
200_success
123k14143401
123k14143401
asked Jan 29 at 23:28
bob
261
261
add a comment |Â
add a comment |Â
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:
- the File handlers (
goThroughandprint) are defined outside of thetryblock, - are opened at the beginning inside the block
- are closed in the
finallyclause.
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:
The method is declared with
throws FileNotFoundExceptionclause. However, the code is surrounded withtry-catchthat catches all exceptions, including the supposedly thrown one.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 ...
add a comment |Â
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.
add a comment |Â
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:
- the File handlers (
goThroughandprint) are defined outside of thetryblock, - are opened at the beginning inside the block
- are closed in the
finallyclause.
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:
The method is declared with
throws FileNotFoundExceptionclause. However, the code is surrounded withtry-catchthat catches all exceptions, including the supposedly thrown one.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 ...
add a comment |Â
up vote
2
down vote
Here are my comments:
1) File handling
Your file handling logic is in the right direction:
- the File handlers (
goThroughandprint) are defined outside of thetryblock, - are opened at the beginning inside the block
- are closed in the
finallyclause.
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:
The method is declared with
throws FileNotFoundExceptionclause. However, the code is surrounded withtry-catchthat catches all exceptions, including the supposedly thrown one.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 ...
add a comment |Â
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:
- the File handlers (
goThroughandprint) are defined outside of thetryblock, - are opened at the beginning inside the block
- are closed in the
finallyclause.
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:
The method is declared with
throws FileNotFoundExceptionclause. However, the code is surrounded withtry-catchthat catches all exceptions, including the supposedly thrown one.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 ...
Here are my comments:
1) File handling
Your file handling logic is in the right direction:
- the File handlers (
goThroughandprint) are defined outside of thetryblock, - are opened at the beginning inside the block
- are closed in the
finallyclause.
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:
The method is declared with
throws FileNotFoundExceptionclause. However, the code is surrounded withtry-catchthat catches all exceptions, including the supposedly thrown one.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 ...
answered Jan 30 at 10:01
Sharon Ben Asher
2,073512
2,073512
add a comment |Â
add a comment |Â
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.
add a comment |Â
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.
add a comment |Â
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.
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.
edited Jan 30 at 8:17
Mast
7,33663484
7,33663484
answered Jan 30 at 7:17
user159297
1
1
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password