473,765 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

read a text file from an applet

jhardman
3,406 Recognized Expert Specialist
I am having a hard time opening a text file from an applet. I remembered after an attempt or two that there were obvious security issues here, so I switched the code to pull a text file off the internet, and there is obviously some naming or reference issue that I can't get my head around. Here is my code:
Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.Font;
  3. import java.awt.Graphics;
  4. import java.awt.FontMetrics;
  5. import java.io.*;
  6. import java.net.*;
  7.  
  8.  
  9. public class solong17 extends java.applet.Applet {
  10.  
  11.     String letter = new String();
  12.  
  13.     public void paint(Graphics screen) {
  14.         //Color bk = new Color(0,0,0);
  15.         //Color tx = new Color(255,255,255);
  16.         Font f = new Font("Arial", Font.BOLD, 18);
  17.         FontMetrics fm = getFontMetrics(f);
  18.         setBackground(Color.black);
  19.         screen.setColor(Color.white);
  20.         screen.setFont(f);
  21.         String s = "So long, and thanks for all the fish.";
  22.         int x = (size().width - fm.stringWidth(s)) / 2;
  23.         int y = size().height / 2;
  24.         screen.drawString(s, x, y);
  25.     }
  26.  
  27.     TextArea lt;
  28.  
  29.  
  30.     public void init() {
  31.  
  32.         try {
  33.             URL page = new
  34.             URL("http://www.worksonline.org/works/default.htm");
  35.         }
  36.         catch (MalformedURLException e) {
  37.             System.out.println("Bad URL: http://www.worksonline.org" +
  38.                 "/works/default.htm");
  39.         }
  40.  
  41.         URLConnection conn = null;
  42.         InputStreamReader in;
  43.         BufferedReader data;
  44.         StringBuffer buf = new StringBuffer();
  45.         String line;
  46.         //System.out.println(page);
  47.  
  48.         try {
  49.             conn = this.page.openConnection(); //error is here
  50.             conn.connect();
  51.             in = new InputStreamReader(conn.getInputStream());
  52.             data = new BufferedReader(in);
  53.             /*FileReader file = new FileReader("solong16.java");
  54.             BufferedReader buff = new
  55.                 BufferedReader(file);
  56.             boolean eof = false;*/
  57.             while ((line = data.readLine()) != null) {
  58.                 buf.append(line + "\n");
  59.             }
  60.  
  61.             //buff.close();
  62.         } catch (IOException e) {
  63.             System.out.println("Error -- " + e.toString());
  64.         }
  65.  
  66.         letter = buf.toString();
  67.  
  68.         lt = new TextArea(letter, 5,50, 1);
  69.         Color bk = new Color(10,10,25);
  70.         lt.setBackground(bk);
  71.         lt.setForeground(Color.white);
  72.         lt.setEditable(false);
  73.         Font tf = new Font("Arial", Font.BOLD, 18);
  74.         lt.setFont(tf);
  75.         add(lt);
  76.     }
  77. }
The error (line 49) says "page" isn't found, so I think there is some naming or reference convention I'm missing.

I'm just trying this task to "get my feet wet" with java. I'm heavily entrenched in Visual Basic, and really wanted something completely different, and I'm having a hard time understanding what must be some real basics.

Jared
Jan 6 '08 #1
14 3001
r035198x
13,262 MVP
Remove the "this" on line 49 where you have this.page ...
Jan 7 '08 #2
jhardman
3,406 Recognized Expert Specialist
Remove the "this" on line 49 where you have this.page ...
Thanks for your attention, but that was one of the first things I tried and it doesn't change the error.
Expand|Select|Wrap|Line Numbers
  1. cannot find symbol
  2. symbol: variable page
With the "this." the carot indicating the error location points to the dot, without it, the carot points to the p, otherwise there is no difference in the error.

Jared
Jan 7 '08 #3
BigDaddyLH
1,216 Recognized Expert Top Contributor
Your error is here:

Expand|Select|Wrap|Line Numbers
  1. try {
  2.             URL page = new URL("...");
  3.      }
That declares page as a variable local to the try block, so it can't be used after the block. Declare it in a wider scope, for instance:

Expand|Select|Wrap|Line Numbers
  1. URL url = null;
  2. try {
  3.             page = new URL("...");
  4.      }
You shouldn't soldier on if that url is bad, though, right?
Jan 7 '08 #4
jhardman
3,406 Recognized Expert Specialist
Your error is here:
Thanks, that did the trick. I hadn't realized that the try block had any locale.
You shouldn't soldier on if that url is bad, though, right?
he he. So I tried it with a URL that existed and I got an access denied exception, so back to the drawing board, but at least that's one question down. Thanks a lot.

Jared
Jan 7 '08 #5
BigDaddyLH
1,216 Recognized Expert Top Contributor
he he. So I tried it with a URL that existed and I got an access denied exception, so back to the drawing board, but at least that's one question down. Thanks a lot.
An untrusted applet can only open connections back on the server from which it was downloaded. This is for security. Otherwise, an applet downloaded through a firewall could try to hack into machines hidden behind the firewall.

I'm not a fan of applets. Another option would we to write this as a normal application. Does it have to be an applet?
Jan 7 '08 #6
jhardman
3,406 Recognized Expert Specialist
An untrusted applet can only open connections back on the server from which it was downloaded. This is for security. Otherwise, an applet downloaded through a firewall could try to hack into machines hidden behind the firewall.
I figured the problem was something like that. The applet is just hosted on my computer, I just browsed to the HTML file that displays it, I didn't use a server at all. Any hint about getting my own applet to be trusted? Or should I just put it up on the same domain as the file I was trying to download?
I'm not a fan of applets. Another option would we to write this as a normal application. Does it have to be an applet?
Not at all. I was trying this project to "get my feet wet" and I thought to myself, "Opening, displaying (and next step: manipulating) a text file would be a good exercize." I settled on putting it in an applet because it seemed to me that formatting and positioning are simpler there. I'm just a noob when it comes to java, and some of that stuff is still kind of confusing. In ASP I use VBScript to write the code, and HTML/CSS to do layout and formatting, and I kind of like that. Writing layout in Java seems daunting. If I were to write this as a stand-alone app, I'm not even sure what I should put it in. That's the kind of thing that makes me want to not even bother starting.

Jared
Jan 7 '08 #7
BigDaddyLH
1,216 Recognized Expert Top Contributor
I find the opposite: it's easier to develop a GUI as a standalone application rather than as an applet. The main reason is that you don't have to fight with the browser.

Here for example, is a standalone application that displays the contents of a URL:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.net.*;
  3. import javax.swing.*;
  4.  
  5. public class TextAreaExample implements Runnable {
  6.     public void run() {
  7.         JTextArea textArea= new JTextArea(20, 40);
  8.         try {
  9.             URL url = new URL("http://java.sun.com");
  10.             InputStream in = url.openStream();
  11.             textArea.read(new InputStreamReader(in), url);
  12.         } catch (IOException e) {
  13.             throw new RuntimeException(e);
  14.         }
  15.         JFrame f = new JFrame("TextAreaExample");
  16.         f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  17.         f.getContentPane().add(new JScrollPane(textArea));
  18.         f.pack();
  19.         f.setLocationRelativeTo(null);
  20.         f.setVisible(true);
  21.     }
  22.  
  23.     public static void main(String[] args) throws IOException {
  24.         SwingUtilities.invokeLater(new TextAreaExample());
  25.     }
  26. }
If some of these methods are unfamiliar, why not take the Swing tutorial?

Sun's Swing Tutorial
Jan 7 '08 #8
jhardman
3,406 Recognized Expert Specialist
If some of these methods are unfamiliar, why not take the Swing tutorial?

Sun's Swing Tutorial
Thanks, I'll take your advice.

Jared
Jan 7 '08 #9
r035198x
13,262 MVP
Thanks, I'll take your advice.

Jared
Judging from your background, I'd suggest also that you consider going the JSP way.
Jan 8 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
9329
by: Doug van Vianen | last post by:
Hi, I am working on an Applet which provides some mouse practice for new computer users in our local seniors' computer club. The applet contains several cards, in a card layout, which are displayed to the user one after the other as needed to present some particular mouse operation. This all works fine. One card (card 4 below) includes a textarea and a button. What I wish to do is have the user use the mouse to select some text from the...
14
3055
by: Michael Levin | last post by:
I've got the following problem. I'm a biologist and I have a device at work which monitors my frog habitat. The device has a bunch of sensors, and runs an embedded html server with some java functions defined which know how to read the hardware sensorts. I access it from wherever I am via any browser, and it displays the measurements (a set of simple numbers) as Java applets in a simple html page. One entry in this page (a table cell) looks...
5
6337
by: Doug | last post by:
I would appreciate a response on this. I have clients who will be placing text files information about in a network directory for example: http://www.protegen.com/clients/ Is it possible to read these text files using either vbscript or javascript. What I am attempting to do is to read these files and then create charts for viewing by each client. A direct email to me at douglas.taylor@protegen.com will help.
2
1580
by: Thomas Magma | last post by:
Hello, I have a case where an applet .class file may or may not be present. In the case when it is not present I get a big gray box where the applet is suppose to be. This is understandable, but undesirable when someone is viewing or printing the page. Is there a way to test first if the .class file is present before hand in order to avoid the big gray box. Thanks.
1
2071
by: Roberto Gallo | last post by:
Hi, I have an applet running inside an html document and I need to read and to set some of html form's textfield and textarea input from my applet. How can I do that? Do I need a javascript? Thank you, Roberto Gallo
2
1164
by: ying | last post by:
I need develop an asp.net script which will run on web server. The script need read documents from client's machine(know the filename, need check if the file exist or not), convert the file to base64 code and bring back to server and insert to a sql database. This is the first time I develop an asp.net project. So anyone can help me on how to passdown the filename to client machine and read the file back? It's similar to attach a document...
3
4390
by: Jimbo | last post by:
I have a website & applet produced under Windows-XP. It works fine. I took it to a machine running Windows 98 & an earlier version of Java to test what happens when I open the web page using IE6 & FireFox. The html invoking the applet is: <applet code="assumptions.class" archive="popproj.jar" width='760' height='340'> This applet was coded using Java 1.5.0_08-b03.</applet> Instead of the expected text, both browsers said "Loading Java...
2
1405
by: dimitryous | last post by:
Hello, I just would like to read a specific (/Downloads/) directory of the client machine in order to get the latest file: My code look for text files like "quotes.txt, quotes-1.txt, ... quotes-42.txt etc..." and would like to point to the last one (quotes-42.txt). I know that I will have to sign the applet later... Right now I'm able to read a specific file (quotes.txt) but what if there is more in the sequence? A loop, but how to...
1
3216
by: sandeepk84 | last post by:
Hi all... I am having an applet program for playing an audio file. It runs normally... but when it's run from browser, its showing the filepermission error. java.security.AccessControlException: access denied (java.io.FilePermission "filename" read) how to solve this? i have tried all the ways...tried changing the java.policy file by adding grant permission, etc. also i created a java.policy.applet file in my directory where the applet...
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8831
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7375
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5419
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2805
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.