Read a file but does not do it twice


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Read a file but does not do it twice

  1. #1
    Join Date
    Dec 2003
    Posts
    3

    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.

  2. #2
    Join Date
    Dec 2003
    Posts
    3
    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!

  3. #3
    Join Date
    Dec 2003
    Posts
    3

    Unhappy

    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center