-
Read a file but does not do it twice
Hi, hope someone can help as Java is a bit tricky!
I have javascript code which calls a java applet, and this applet reads a file and passes the file contents back to javascript. That all works fine.
The problem is that if I ask the java applet for the file contents again it gives me the contents that it read the first time round. As I am resetting the javascript variables it can only be the java code at fault.
javascript:
probs="";
p="";
probs = document.readerApplet.sendtext(p)
<APPLET CODE="Test4.class" NAME="readerApplet" WIDTH="1" HEIGHT="1"></APPLET>
and the java in Test4.class is:
import java.applet.*;
import java.io.*;
import java.awt.*;
public class Test4 extends Applet
{
String me;
String thisLine;
public void init()
{
readTextFromJar("devlist.htm");
}
public void readTextFromJar(String s)
{
try
{
InputStream is = getClass().getResourceAsStream(s);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while ((thisLine = br.readLine()) != null)
{
thisLine += thisLine;
me += thisLine;
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public String sendtext(String thetext)
{
return me;
}
}
Thanks.
-
I've realised that the file needs closing so I have added br.close();
This still gives the contents of the file that was read the first time. Somethings being cached somewhere and is not letting the file be read afresh.
Help still required please!
-
This posting has had 34 visits. Each from a person who knows more than me about Java, yet know one has helped. Very disappointing. Rest assured if you visit a JavaScript site I answer questions on I'll still help you all.
The working code (I figured out) is below if any one cares....
import java.applet.*;
import java.io.*;
import java.awt.*;
public class Test4 extends Applet
{
public String sendtext(String thetext)
{
String me="";
String thisLine="";
try
{
InputStream is = getClass().getResourceAsStream("devlist.htm");
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while ((thisLine = br.readLine()) != null)
{
me += thisLine;
}
br.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return me;
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks