i think i have converted my application to an applet..... it use to read lines from a .txt file i have on my hard drive but now i want it to read a .txt file from a URL:

import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton.*;
import javax.swing.event.ListSelectionEvent;
import java.net.*;
import java.io.*;


public class RosterSorter extends JApplet {

 
  ArrayList<String[]> rosterList = new ArrayList<String[]>();

  // The TableModel controls all the data:
  class MyTableModel extends AbstractTableModel {
        private String[] columnNames = { "Name" , "Age", "Pos", "Nat", "St", "Tk", "Ps", "Sh", "Ag", "Kab", "Tab", "Pab", "Sab", "Gam", "Sub", "Min", "Mom", "Sav", "Con", "Ktk", "Kps", "Sht", "Gls", "Ass", "DP", "Inj", "Sus"};
        

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
            return rosterList.size();
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

      
        public Object getValueAt(int row, int col) 
        {    
             return rosterList.get(row)[col];
                              
        }

        
    }



     [B]private void textRead() [/B]
  {
     BufferedReader br = null;
     
     try { 

    URL                url; 
    URLConnection      urlConn; 
    DataInputStream    dis; 

    url = new URL("http://afterextratime.net/game/ita/1/Laz.txt"); 

    urlConn = url.openConnection(); 
    urlConn.setDoInput(true); 
    urlConn.setUseCaches(false); 

    dis = new DataInputStream(urlConn.getInputStream()); 
    String s; 
  
      while ((s = dis.readLine()) != null) { 
      String [] rowfields = s.split("[ ]+");
        rosterList.add(rowfields);
        s = dis.readLine(); 
    } 
    dis.close(); 
    } 
    catch (MalformedURLException mue) {} 
    catch (IOException ioe) {} 
  } 

public void init() {
      textRead();
      
    Container cp = getContentPane();
     TableModel myData = new MyTableModel(); 
           JTable table = new JTable(myData);
    
    table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
            table.setAutoCreateRowSorter(true);
            table.setRowSelectionAllowed(true);
            table.setColumnSelectionAllowed(false);
            
        
    cp.add(new JScrollPane(table));
    
    
  }

  

  public static void run(JApplet applet, int width, int height) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(applet);
    frame.setSize(width, height);
    applet.init();
    applet.start();
    frame.setVisible(true);
  }
}

the JTable shows up but it it wont read the file and display the values in the table :S although the file is virtually identical to the one on my harddrive.

Hello,

Heres another way to read the file:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

public class TestURLReading {

  static protected void readURL() {
    String u = "http://afterextratime.net/game/ita/1/Laz.txt";
	URL url;
    InputStream is;
    InputStreamReader isr;
    BufferedReader r;
    String str;

    try {
      System.out.println("Reading URL: " + u);
      url = new URL(u);
      is = url.openStream();
      isr = new InputStreamReader(is);
      r = new BufferedReader(isr);
      do {
        str = r.readLine();
        if (str != null)
          System.out.println(str);
      } while (str != null);
    } catch (MalformedURLException e) {
      System.out.println("Invalid URL");
    } catch (IOException e) {
      System.out.println("Can not connect");
    }
  }

  static public void main(String args[]) {
         readURL();
  }
}

Tested and working perfectly.

Regards,

These below code you can use for readin text file from URL.

String charset = "utf-8";
URL url = new URL(your text file URL);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
InputStream inputStream = null;
inputStream = url.openStream();
StringBuffer s = new StringBuffer();
if(charset==null"".equals(charset)){
charset="utf-8"; }
String rLine = null;
BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream,charset));
PrintWriter printWriter = null;
FileOutputStream fo = new FileOutputStream(fileName);
OutputStreamWriter writer = new OutputStreamWriter(fo, "utf-8");
printWriter = new PrintWriter(writer);
while ( (rLine = bReader.readLine()) != null) {
String line = rLine;
int str_len = line.length();
if (str_len > 0) {
s.append(line);
printWriter.println(line);
printWriter.flush();

I wrote one example to download an image URL to your local PC. You can use this java code

SNIP

You can also check this code to download TEXT file from URL

SNIP

Thanks,

Binod Suman
SNIP

Edited 6 Years Ago by peter_budo: URLS and Fake Sig snipped, Adding code tags to old post

This article has been dead for over six months. Start a new discussion instead.