Java Applet Beginner Question - File Access

Posted on 2007-11-28
Last Modified: 2013-11-23
I am writing my first java applet.. so this question HOPEFULLY will be VERY easy for you experts.

1. Have my webpage call a public function in my applet with one argument.
2. Have the applet READ FROM a text file ON THE SERVER, in the same directory as the applet, and return a text value from that file.
3. Have the webpage display that value.

<applet name='TownDataBase' code=TownDataBase.class width=10 height=10></applet>
* In a function on that same page, i am calling the public member of the applet..
* I then use a "document.write" to put the value in the page.

FULL JAVA APPLET LISTING (This is the problem)
import java.applet.*;
import java.awt.*;

public class TownDataBase extends Applet {
  public TownDataBase()
  public void start()

  public void stop()

  public void destroy()

  public void init()

  public void paint(Graphics g)

  public String GetData(String valin)
    //Init our return value
    String RetVal = "";

    //Try to open the file and read from it
      File fl = new File("MapData.txt");
      FileReader fr = new FileReader(fl);
      BufferedReader br = new BufferedReader(fr);
      RetVal = br.readLine();
    } catch (IOException e)       {
      RetVal = "Error";
    //Return it
    return "Checking " + valin + "... " + RetVal;

- I read somewhere that in an unsigned applet.. (which this is..) you can NOT access files using the methods i used.. instead you are supposed to use URL's.. and resources.. etc.. which I am not sure how to do.  

How do i open my file and read just a single line of data from it?? For now, thats all i want it to do.. once i can get it open, then i can massage the data any way I want.. please just help me get the dang file open.  Thank you.

- Andrew
Question by:LunaSkye
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 17

Assisted Solution

contactkarthi earned 50 total points
ID: 20365737
LVL 86

Expert Comment

ID: 20365750

BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("MapData.txt")));

Open in new window

LVL 86

Accepted Solution

CEHJ earned 200 total points
ID: 20365756
Or rather
BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/MapData.txt")));

Open in new window

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 20365851
contactkarthi and CEHJ,

Both of the solutions did what i wanted.. Thank you for the input.
I like CEHJ's solution better though, because it is only one line, and doesnt require me to reference another library (*).
I can see that they are doing pretty much the same thing..  however, they are both REALLY slow.. i mean like 10-15 seconds..
Can i expect the file access to be this slow typically? Is there a way to speed it up?

Thank you,
LVL 86

Expert Comment

ID: 20365874
That shouldn't be the case. Are you sure you're not witnessing slowness elsewhere?

Author Comment

ID: 20365886
I dont think so..
here is the webpage that is accessing the file ("TownName" is a variable) :

document.write("<H2> Welcome To " + TownName + " !!</H2>");

The first line "Welcome To.." is coming up immediately..
The second line.. the one that accesses the applet, is coming up like 10 seconds later..
LVL 86

Expert Comment

ID: 20366234
I would avoid doing document.write. Instead set the value of an element already there on the page

Author Closing Comment

ID: 31411422
I split the points because both answers were correct.  CEHJ gave a much simpler answer and explained things better. That is why I gave more points to them.  I appreciate the help!

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse buid path vs tomcat lib path 10 38
Delphi: barcode reading on android platform 1 73
Java Restore security prompts not working 10 135
Adjust the codes 3 31
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question