File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes applet and servlet communication error: java.io.EOFException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "applet and servlet communication error: java.io.EOFException" Watch "applet and servlet communication error: java.io.EOFException" New topic
Author

applet and servlet communication error: java.io.EOFException

may Lee
Ranch Hand

Joined: Nov 20, 2003
Posts: 68
My environment: Applet is in Apache2(redhat9), Servlet is supported by Tomacat4.1.30 From website, Applet sends a filename to servlet, servlet reads this file line by line (each line contains three data, like 3.45 4.56 -1.23), and change each line to a data array, then send the data array back to Applet by serialized object and post method.

Problem: the applet got the error:java.io.EOFException
If servlet only reads line(inBuff.readLine),comments the part that converts each line to the data array(see my code below), no error. If adding this part, get the above error. Why? Is there any class or method not supported by Tomcat servlet in my code? Thansk for any help!!

Servlet side code to read the file:

void readdata (long arg5){ /**arg5 is the file name from applet**/

char[] tempchar={'0','0','0','0','0','0','0'};

File ff=new File(filepath+Long.toString(arg5));

/******begin reading data into the float array channel********/

try{

if(ff.exists()){

try{

BufferedReader inBuff=new BufferedReader(new FileReader(filepath+Long.toString(arg5)));

inBuff.readLine();//read the first line of DAQ file

for(int i=0;i<datanum;i++){

String temp=inBuff.readLine();
int k=0,m=0;
/***the part to conver each line to the data array***/

//for(int j=0;j<temp.length();j++){

//if(temp.charAt(j)!=' '&&temp.charAt(j)!='\t'){

//tempchar[k]=temp.charAt(j);

//k++;

//if(j==temp.length()-1){

//String s=String.copyValueOf(tempchar);

//float f=Float.parseFloat(s);

//channel3[i][m]=f;
//channel3[i][m]=0;

//}

//}

//else{

//String s=String.copyValueOf(tempchar);

//float f=Float.parseFloat(s);

//channel3[i][m]=f;
//channel3[i][m]=0;

//m++;k=0;

//tempchar[6]='0';

//}

//}

}

inBuff.close();

}catch(FileNotFoundException e)

{df.servleterror2=true;df.servleterrormessage2=e.toString();}
catch(IOException e)

{df.servleterror2=true;df.servleterrormessage2=e.toString();}

}

else{

for(int p=0;p<channelnum3;p++)

for(int q=0;q<datanum;q++)

channel3[q][p]=0;

}

}catch(SecurityException e)
{df.servleterror2=true;df.servleterrormessage2=e.toString();}

}/****end of readdata*****/
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Try using a the CODE tag below the Add Reply button to post code in a thread.

The CODE tag will retain your formatting like indentation, which makes the code easier to read.

Unfortunately, I can't really read and understand your code the way that it is posted.

However, it appears that you are not checking when you reach the end of the file.

That is what an EOFException is. End Of File Exception.

Good Luck

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
may Lee
Ranch Hand

Joined: Nov 20, 2003
Posts: 68
Yes, I didn't check the end of the file. In fact, I know there are total 401 lines in each file, first line is date, no need to convert, just convert the other 400 lines to data array, so when I use to readLine(), I set the datanum to be 400 here (for(int i=0;i<datanum;i++){) so no need to know if it is file end or not, then I close the inbuff.close()

In this case, how could I get the error End of File Exception?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

But obviously it is reaching the end of the file. Set it to two hundred if you have to have a number, and see if you get the error too.

Maybe there is something wrong with the file, or there aren't carriage returns, or something such that you get your EOFException.

Basically Java will not throw an EOFException unless it reached the end of the file.

Good Luck

Mark
may Lee
Ranch Hand

Joined: Nov 20, 2003
Posts: 68
I set to 200, still the same error.
I found that:

Each time I comment out the part that converts each line to data array, no EOFException error at applet side, it means the function of readLine() is working well.

Each time I add the part of converting each line to data array, it shows EOFException on applet side. So it means the part of converting is not working with applet and servlet communication? But I cannot find what is wrong Any suggestion please?


Servlet side code to read the file:

void readdata (long arg5){ /**arg5 is the file name from applet**/
char[] tempchar={'0','0','0','0','0','0','0'};
File ff=new File(filepath+Long.toString(arg5));
/******begin reading data into the float array channel********/
try{
if(ff.exists()){
try{
BufferedReader inBuff=new BufferedReader(new
FileReader(filepath+Long.toString(arg5)));
inBuff.readLine();//read the first line of DAQ file
for(int i=0;i<datanum;i++){
String temp=inBuff.readLine();
int k=0,m=0;
/***the part to conver each line to the data array***/
/***if I commnet out this part, no error***/
// for(int j=0;j<temp.length();j++){
// if(temp.charAt(j)!=' '&&temp.charAt(j)!='\t'){
// tempchar[k]=temp.charAt(j);
// k++;
// if(j==temp.length()-1){
// String s=String.copyValueOf(tempchar);
// float f=Float.parseFloat(s);
// channel3[i][m]=f;
// }
// }
// else{
// String s=String.copyValueOf(tempchar);
// float f=Float.parseFloat(s);
// channel3[i][m]=f;
// m++;k=0;
// tempchar[6]='0';
// }
// }
}
inBuff.close();
}catch(FileNotFoundException e)
catch(IOException e)
else{
for(int p=0;p<channelnum3;p++)
for(int q=0;q<datanum;q++)
channel3[q][p]=0;
}
}catch(SecurityException e)
}/****end of readdata*****/
may Lee
Ranch Hand

Joined: Nov 20, 2003
Posts: 68
Sorry the code is not clear in last post.

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

A better way to read a line input stream is:


Not everyone likes doing an assignement inside a "when" condition, but it's a little tidier than the alternatives. Also, I'm not sure all input streams throw end of file exceptions, but they all return null when attempting to read past the end of fileXXXX data.
[ June 22, 2004: Message edited by: Tim Holloway ]

Customer surveys are for companies who didn't pay proper attention to begin with.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Tim's method is preferred.

Also, for a way to debug to see how many lines it does read,

you can put a

System.out.println("Count: " + i);

this way you can see how many times it goes through the loop before hitting the End of File.

I really think that the file must not have carriage returns or line feeds, so it thinks that there is only one line.

But that is a complete guess.

Good Luck.

Mark
may Lee
Ranch Hand

Joined: Nov 20, 2003
Posts: 68
I tried Tim's method, same error when I add the convert part.
I tried to only use readLine() and display the line, it works, I mean, it read line by line.
Also when I use applet to do the servlet part function, it is working with the same code. So there must be some secret error not found. Keep working to find it.

I am wondering, is there a way to do step by step debug of applet and servlet communication programs?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

What ahppened when you tried this?


System.out.println("Count: " + i);

this way you can see how many times it goes through the loop before hitting the End of File.



What was the last count given?

Mark
may Lee
Ranch Hand

Joined: Nov 20, 2003
Posts: 68
I found the error, it is array dimention mistake. I use channel3[400][3] in the part that convert string to float data, after I write the new part,I define it as channel3[3][400], but I still copy the part of converting, so get error. Interesting thing is, it told me EOFException error, not array index error, which led me to think about my file end,...
anyway, found the misktake finally!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: applet and servlet communication error: java.io.EOFException
 
Similar Threads
Use URL in applet to read ASCII files in loop
'class' or 'interface' expected
HELP~~
String index out of bound at index 0?! HELP!
Two Dimensional Arrays