File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes cannot read file.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "cannot read file.." Watch "cannot read file.." New topic

cannot read file..

nani aman
Ranch Hand

Joined: Sep 22, 2004
Posts: 33
i couldn't read a file. There is no error but it keep give the exception statement. Please help me to figure this out.

This is the code :

public class TesTing2 {

public TesTing2(){


BufferedReader file = new BufferedReader(new FileReader("input.txt"));

StringTokenizer inputStream;

String s;

while ((s = file.readLine()) != null) {
inputStream = new StringTokenizer (s);

char[] keep = new char[s.length()];

for(int i =0;inputStream.hasMoreTokens()==true;i++)
keep = s.toCharArray();
System.out.println("All done!" + keep);

catch (Exception file){
System.out.println("Cannot open file");

Please help me. Thank you.
Alex Iordanoglou

Joined: Oct 21, 2004
Posts: 6
Hi there,

Is your class in a package? I run your code (except the part that you dont include), and I did initially get a FileNotFoundException (by the way, the catch clause would catch anything, it might be a good idea at least to print the stack trace of the exception...) when I first tried to run it.
The problem was that I had put the class in a package, and input.txt was in
the same directory as the class... but since I was running from one directory "up", it could not find the file... Once I put the input.txt in the same directory from where I was running, there was no problem...
Hope it helps...
Arnaud Burlet
Ranch Hand

Joined: Oct 08, 2004
Posts: 31
Of course it gives no error when you hide the error !

The description of the error is inside the Exception, just print it ...

and see what is your problem !

Parveen Aggarwal

Joined: Jun 08, 2004
Posts: 7

Yaar i think that u should specify the file directory (in which your txt file is present)in classpath settings ... aftre that it will going to work...

If it still gives problem please let me know...


nani aman
Ranch Hand

Joined: Sep 22, 2004
Posts: 33
The exception e is work. It is not an error opening file but an array over bound. Thank a lot friends.
[ October 22, 2004: Message edited by: nani aman ]
Don't get me started about those stupid light bulbs.
subject: cannot read file..
jQuery in Action, 3rd edition