?
Solved

Java Applet Beginner Question - File Access

Posted on 2007-11-28
8
Medium Priority
?
657 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 200 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 800 total points
ID: 20365756
Or rather
BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/MapData.txt")));

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
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.
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

765 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