aspose file tools*
The moose likes Beginning Java and the fly likes reading from a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "reading from a file" Watch "reading from a file" New topic
Author

reading from a file

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 374
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

Joined: Jun 13, 2009
Posts: 2203
    
    7
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

Joined: Apr 28, 2009
Posts: 63

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

Joined: Dec 28, 2007
Posts: 2266
    
    3

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!


Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 374
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
Marshal

Joined: Mar 22, 2005
Posts: 42286
    
  64
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.


Ping & DNS - my free Android networking tools app
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18662
    
    8

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

Joined: Dec 31, 2010
Posts: 10
nirjari patel wrote:
How to get the output from file ?

you use a while loop and try/catch
nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 374
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

Joined: Apr 28, 2009
Posts: 63

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reading from a file