Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cannot read file..

 
nani aman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello..
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(){

try{

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++)
{
inputStream.nextToken();
testing();
keep = s.toCharArray();
elementCheck(keep);
System.out.println("All done!" + keep);
}
}
}

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

Please help me. Thank you.
 
Alex Iordanoglou
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course it gives no error when you hide the error !

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

replace
by
and see what is your problem !

Arnaud
 
Parveen Aggarwal
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi...

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...

Cheers!!

Parveen
 
nani aman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic