Solved

Java Applet Beginner Question - File Access

Posted on 2007-11-28
8
651 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
Comment Utility
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
Or rather
BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/MapData.txt")));

Open in new window

0
 
LVL 6

Author Comment

by:LunaSkye
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 86

Expert Comment

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

Author Comment

by:LunaSkye
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now