Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: Java
Expertise: Beginner
Mar 21, 1997



Application Security Testing: An Integral Part of DevOps

Reading Text File From Server

I know I can't read files from a local machine through my browser, but I can't seem to read a file off my originating server either. There's a way to do this, right?

Is there an example of reading a text file from my server and displaying it — one that will run from a browser?

The example below worked with Java Runner and Netscape. The main drawback is its inability to write back to the file. Unfortunately, URLs don't have associated output streams for this. To read andwrite to files on the source host, you must implement a server to which applets can send read and write requests.

import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;

public class Reader extends Applet {

   private TextField path;
   private ErrorFrame f = new ErrorFrame();
   URL source;
   TextArea display = new TextArea(8, 40); // 8 lines x 40 chars
   String next;
   AppletContext context;

   public void init() {
      add(path = new TextField("", 30));
      add(new Button("HIDE FILE"));
      context = getAppletContext();

   public boolean action(Event e, Object arg) {
      if (e.target == path) {
         try {
            String file = path.getText();
            source = new URL(getCodeBase(), file);
            DataInputStream in = new DataInputStream(source.openStream());
            // erase display
            while(null != (next = in.readLine()))
               display.appendText(next + "\n");
         catch(IOException ioe) {
           // f.error(ioe); my error dialog
      else if (arg.equals("HIDE FILE")) {

         return true;
      return false;

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date