I want to use InputStream to read a file. but I don't want to use the filename in the html code of the applet (like param name=inputfile value="data1.txt"> I used this code but it doesn't work (actually nothing is shown when i open the html file). I had it working before but somehow now it doesn't. This is my code:
class Node { int x; int y; int set; int first; int next; int w; int h; String name; }
class Edge { int rndd_plus; /* initial vertex of this edge */ int rndd_minus; /* terminal vertex of this edge */ int len; /* length */ int select; String name; }
public class Kruskal extends Applet implements MouseListener { String inputfile = "data1.txt"; int n,m; int num,den; int u, usel, step; Node v[] = new Node[100]; Edge e[] = new Edge[200]; int idx[] = new int[200];
int findNode(String name) { for (int i=0; i<n; i++) if (v.name.equals(name)) return i; return -1; }
public void paintNode(Graphics g, Node n, FontMetrics fm) { String s; int x = n.x; int y = n.y; int w = fm.stringWidth(n.name) + 10; int h = fm.getHeight() + 4; n.w = w; n.h = h;
Maybe the Security Manager restrict your applet to get the file.
Where is your "Data1.txt" located? Is it on the same computer where the applet runs? or is it on the server? If Data1.txt is on the server, than you will be able to read this file fairly easy; however, you must supply the accurate URL to the file. You didn't post the HTML, and the URL in your code is not complete, so I figure that you didn't supply the CODEBASE parameter.
But if the data1.txt is on the machine where the applet runs, and this machine is not the server where the applet classes resides in, then you should to know something about Security Manager and the browser specific security capability.
This is my html code: <title>Kruskal Shortest Path Algorithm</title> <applet code="Kruskal.class" width=565 height=400> </applet> and data1.txt is in same directory as Kruskal.class/java, not on a server but on my pc and changed the code into (but still doesn't work):
String mdname = "inputfile"; if ( mdname != null){ inputfile = new String(mdname);
try { InputStream is; is = new URL(getCodeBase),mdname).openStream(); input_graph(is);