Solved

Java Applet Beginner Question - File Access

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

Objectives:
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.


PARTIAL WEB PAGE LISTING (This works fine):
<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..
document.TownDataBase.GetData(TownName)
* 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.*;
import java.io.*;

public class TownDataBase extends Applet {
  //Constructor
  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
    try
    {
      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;
  }
}



NOTES:
- 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
0
Comment
Question by:LunaSkye
  • 4
  • 3
8 Comments
 
LVL 17

Assisted Solution

by:contactkarthi
contactkarthi earned 50 total points
ID: 20365737
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20365750
Try


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

Open in new window

0
 
LVL 86

Accepted Solution

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

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 6

Author Comment

by:LunaSkye
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 (java.net.*).
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,
Andrew
0
 
LVL 86

Expert Comment

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

Author Comment

by:LunaSkye
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>");
document.write(document.TownDataBase.GetData(TownName))

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..
0
 
LVL 86

Expert Comment

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

Author Closing Comment

by:LunaSkye
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!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java DateChooser? 3 36
javap bin 2 34
difference of if loops 23 49
Capture logon name 13 73
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This article will show, step by step, how to integrate R code into a R Sweave document
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

810 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