Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reading from a file

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to read from a file using FileReader as follows.


I am getting following errors, when I compile this program
FileReading.java:7: illegal escape character
BufferedReader br = new BufferedReader(new FileReader("C
:\Users\nirjari\Desktop\hexalock"));

^
FileReading.java:7: illegal escape character
BufferedReader br = new BufferedReader(new FileReader("C:\Users\nirjari\Desktop\hexalock"));

^
FileReading.java:7: illegal escape character
BufferedReader br = new BufferedReader(new FileReader("C:\Users\nirjari\Desktop\hexalock"));
^
FileReading.java:7: illegal escape character
BufferedReader br = new BufferedReader(new FileReader("C:\Users\nirjari\Desktop\hexalock"));

^
4 errors


When I try using "/" in place of "\" in the filepath , code does compile, but does not produce any output from the file.

How can I provide path ? How to correct this code, so that it prints content from the file ?

Regards
 
Rob Camick
Ranch Hand
Posts: 2612
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I try using "/" in place of "\" in the filepath , code does compile, but does not produce any output from the file.


And what is the output from the "catch" statement.
 
Richard Broersma
Ranch Hand
Posts: 63
Firefox Browser Postgres Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nirjari patel wrote:I am trying to read from a file using FileReader as follows.
FileReading.java:7: illegal escape character


Remember that Java strings will consume '\' characters. One such example is '\n' - newline.
So, if you what to represent a '\' you need to escape it with '\\'.
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Broersma wrote:So, if you what to represent a '\' you need to escape it with '\\'.


That's it! You can also use '/' instead of '\\'. Also, avoid using things like catch(Exception e){}. This can mask problems in your program. Always treat each exception that can occur properly!
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried using "\\" and "/"also. Code compiles and runs, but there is no content of the file being printed in output.


Why is that ? How can I print content of the file ?

Then I tried without try-catch block as below
This is the output I received

FileReading.java:7: unreported exception java.io.FileNotFoundException; must be
caught or declared to be thrown

BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\nirjari\\Desktop\\hexalock"));

^
FileReading.java:9: unreported exception java.io.IOException; must be caught or
declared to be thrown
String br1 = br.readLine();

^
2 errors

How to get the output from file ?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to keep the try/catch statement - but you also need to put something in the catch block to alert you if there's a problem. As Roberto said, empty catch blocks are a bad idea. For starters, something like "e.printStackTrace()" will do.
 
Paul Clapham
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most likely the way to get output from the file would be to specify the name of a file which actually exists. You had this code:

Which says "If any exceptions occur, don't do anything". So when you get, for example, an exception because you used the name of a file which didn't exist, that catch-block does nothing and your program ends normally.

So don't do that. Have your catch-block tell you if there's a problem:
 
Kurosaki Ichigo
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nirjari patel wrote:
How to get the output from file ?

you use a while loop and try/catch
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code is working now.
But still I dont understand try/catch block.
When I use try/catch block in the code, I can get output. But when I try without try/catch block as below, I get errors.


Thi sis the error I get
FileReadingWithoutTryCatch.java:9: unreported exception java.io.FileNotFoundExce
ption; must be caught or declared to be thrown
BufferedReader br = new BufferedReader(new FileReader("C
:\\Users\\Jignesh\\Desktop\\hexalock.txt"));
^
FileReadingWithoutTryCatch.java:16: unreported exception java.io.IOException; mu
st be caught or declared to be thrown
while((line=br.readLine()) != null) {
^
2 errors

Now try/catch block is optional to catch some user defined error. So why is this code not working without try/catch block ?

How do I understand, when I need to use try/catch block ?

Thanks
 
Richard Broersma
Ranch Hand
Posts: 63
Firefox Browser Postgres Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I understand, when I need to use try/catch block ?


Notice the definition of the FileReader Constructor:
http://download.oracle.com/javase/6/docs/api/java/io/FileReader.html#constructor_detail

Notice that by it's definition it throws FileNotFoundException.

By examination of FileNotFoundException:
http://download.oracle.com/javase/6/docs/api/java/io/FileNotFoundException.html

We see that is extends from Exception - as opposed to RuntimeException or Error. So we know that it, like all other "checked" exceptions requires that codes deal with it (e.g. catch or declare).

exceptions/catchOrDeclare

I hope that this helps to explain the design of checked exceptions.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic