Solved

Java Applet Beginner Question - File Access

Posted on 2007-11-28
8
656 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
[X]
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
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
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!

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

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 …
In this post we will learn different types of Android Layout and some basics of an Android App.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

690 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