Solved

Java Applet Beginner Question - File Access

Posted on 2007-11-28
8
654 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
hibernate jars 4 45
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO) 1 36
Setup GlassFish 4 22
passing enum to a method 4 23
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

828 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