Is there any exception thrown there?
What happens when you try to read it in the applet?
Main Topics
Browse All TopicsHi,
I have the following lines of code in a java applet, I am trying to read a file in the same directory on the server as the applet. When I use an applet viewer to do it, it will work but when I put it in a htm page it wont. I suspect that I need to use codeBase method somewhere in it, but I have never tried to do this before.
Any help greatly appreciated
FileInputStream myFile = null;
try
{
myFile = new FileInputStream(fileName);
boolean eof = false;
int i=30;
while (!eof)
{
int byteValue = myFile.read(); // read the stream
if (byteValue == -1)
eof = true; //exit loop as end of file is reached
else
g.drawString("Integer value "+ byteValue",30, i);
i+=20; //shifts down to next line
}
}
catch (IOException e)
{
System.out.println("Could not read file: " + e.toString());
}
finally
{
try
{
myFile.close();
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Hi, Many thanks to you both for your comments.Currently as it stands I have an applet with the above code in residing in a folder on my desktop, the files it wants to open are sat in the same folder as it. When I use applet viewer, it has no problem opening the files as I want, when I try to run the web page with the applet embedded in it, it just wont open the files.... I dont know if this is something to do with the security settings etc in place on my computer. I always allow the applet to run when prompted, so I could not see it being a problem. At the moment I am not to worried about getting to run over the web or whatever, just to get it working on my computer is what I really need! Again many thanks for your help!
John
Hi, Thanks again to both of you, I understand now what both of you are telling me, its just that I don't exactly know how to solve the problem by integrating any of the code into the above as I am not exactly sure how it works!
I know I will have to import java.net.*
then, how do I modify this so that I get a stream of the integers from the file, as is happening with my above code:
try {
URL url;
URLConnection urlConn;
DataInputStream dis;
url = new URL("http://webserver.our-
urlConn = url.openConnection();
urlConn.setDoInput(true);
urlConn.setUseCaches(false
dis = new DataInputStream(urlConn.ge
String s;
toDoList.clear();
while ((s = dis.readLine()) != null) {
toDoList.addItem(s);
}
dis.close();
}
catch (MalformedURLException mue) {}
catch (IOException ioe) {}
}
}
I have tried the following, unfortunately it keeps telling me that there is no nextByte, I just want to read each byte out of the stream as an integer and use the integer in another part of my program.
try {
URL url;
URLConnection urlConn;
DataInputStream dis;
url = new URL(getCodeBase().toString
// Note: a more portable URL:
//url = new URL(getCodeBase().toString
urlConn = url.openConnection();
urlConn.setDoInput(true);
urlConn.setUseCaches(false
dis = new DataInputStream(urlConn.ge
int s;
while ((s = dis.nextByte) != null) {
//not null against an int not allowed
//output s as an integer here
}
dis.close();
}
catch (MalformedURLException mue) {}
catch (IOException ioe) {}
This problem is such a headache! I had my original code working no problem, would it be possible for either of you to modify my original posted code so it does the same job, but is able to read the files from where ever they are stored?? I normally dont ask for my code to be modified, and generally work it out myself, but on this occasion I am really stuck and don't understand how to do it and need it rather urgently!!!
Thanks for all your help!
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.
I will leave the following recommendation for this question in the Cleanup topic area:
Split: ksivananth {http:#19378656} & keyurkarnik {http:#19378673} & objects {http:#19385727}
Any objections should be posted here in the next 4 days. After that time, the question will be closed.
girionis
EE Cleanup Volunteer
Business Accounts
Answer for Membership
by: ksivananthPosted on 2007-06-27 at 23:59:36ID: 19378656
check this URL,
va/edu/pj/ pj010023/
http://www.devdaily.com/ja