August 1st, 2002, 01:30 AM
-
Reading file from applet
Hi,
Sorry this is not a servlet question but still please help me.
I have an applet in a jar file and antoher file which i need to read from the applet.
the applet is snake.SnakeApplet and i have put the file (Scene1) which i need to read in the snake directory.
I have tried a lot of code but all i get is null
I tried the following
InputStream istream = this.getClass().getClassLoader().getResourceAsStream("snake/Scene1");
InputStream istream = this.getClass().getClassLoader().getResourceAsStream("Scene1");
InputStream istream = this.getClass().getResourceAsStream("Scene1");
Can anyone plese help me
Regards
Navneet
August 1st, 2002, 03:19 AM
-
Try using either FileInputStream or FileReader, depending on whether you need an InputStream or a Reader. Both take a "path/to/file.name" as the argument to their constructors.
August 1st, 2002, 07:08 AM
-
Ya, i tried that but as the applet is running in a browser it does not allow me to acceess the file like that and gives me access denied
August 1st, 2002, 09:25 AM
-
This will work:
Code:
package test;
import java.awt.BorderLayout;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JApplet;
import javax.swing.JLabel;
/**
* @author TBRYANT
*
*/
public class TestApplet extends JApplet {
public void init() {
String host = getCodeBase().getHost();
StringBuffer buf = new StringBuffer();
try {
URL url = new URL("http://" + host + "/test.txt");
InputStream in = url.openStream();
int c = 0;
while((c = in.read()) != -1) {
buf.append((char)c);
}
in.close();
}
catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
JLabel label = new JLabel(buf.toString());
getContentPane().add(label, BorderLayout.CENTER);
}
}
Put the file you want to open in the same directory as the applets jar file. You can open files if they are on the same server the applet came from, but you need to connect back to it to open them.
August 1st, 2002, 04:07 PM
-
Dynamic file
If the file is very big and if it is dyanmic ( for eg log file) ..how do incorporate this. Because it is takes a while for the applet to get loaded & sometimes hangs coz of the huge file size.
Any help in this matter is appreciated
August 1st, 2002, 07:50 PM
-
How big of a file are you talking about? As far as I know, you have only one choice if you want to access data from an applet. Either have it open a URL and get it from the browser, or have the data embeded in the web page as an applet param. I have a web page where the jsp dynamically builds the data for the applet and puts it in a param tag. You can dynamically suplly the data by using a method call in the jsp:
Code:
<applet>
<param name=data value="<%= bean.getAppletData() %>">
</applet>
Then you can parse the data in the applet using the getParameter(String) method. Just as an example.
Hope that helps
August 1st, 2002, 09:34 PM
-
Hi Nemi,
Thanks for your help But the file that i am tring to use in inside the same jar file where the applet is.
Cant i read it from the Jar file
August 2nd, 2002, 09:04 AM
-
Hi Nemi,
This is wrt to same question posted by Navneet.
Rt now I have an applet which reads the log file frm the server. The log file is abt 5-6Mb file, Sometimes it takes forever to read the file and hangs. I am using URL() methods to read the data.
In case i want to make the applet dynamic so that file is refereshed , how do i do it?
And we don't want to use JSP.
Thanks
Som
August 2nd, 2002, 03:34 PM
-
OK, after messing with this for a while I have figured out how to access files in the jar file. You MUST make sure that the resource to be opened is in the same directory IN THE JAR FILE as the applet.
So for instance, my example:
Code:
package test;
import java.awt.BorderLayout;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import javax.swing.JApplet;
import javax.swing.JLabel;
/**
* @author TBRYANT
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class TestApplet extends JApplet {
public void init() {
String host = getCodeBase().getHost();
StringBuffer buf = new StringBuffer();
try {
InputStream in = TestApplet.class.getResourceAsStream("test.txt");
int c = 0;
while((c = in.read()) != -1) {
buf.append((char)c);
}
in.close();
}
catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
JLabel label = new JLabel(buf.toString());
getContentPane().add(label, BorderLayout.CENTER);
}
}
The applet is in a package called 'test'. If you open the jar file, the class file is contained in 'test/TestApplet.class'. Open with winzip and you can see the directory structure. Now, for the applet to be able to access it, the test.txt file MUST be in the test directory also (i.e., "test/test.txt"). If it is in the root of the jar file, it will not be accessable from the applet using the getResourceAsStream(String) method.
Som
I assume you are using a dial-up connection when you say it takes forever and times out. I bet it does if the file is 5-6 meg. I can see no alternative other than possibly opening the URL and reading only so many bytes to show on screen at a time. You could use a button to have the user click and retrieve the next so many bytes of info. That way you are not receiving such a large quantity at one time.
You could use the read method that lets you specify how many bytes you want to read, and on subsequent calls you could use the skip method to get back to the same position. Either that or you could experiment leaving the stream open and calling for the next so many bytes when the user clicks the button. Not sure if it would time out if not used for a given amount of time. Probably. In any case, that should give you some ideas.
Hope this helps.
August 2nd, 2002, 04:39 PM
-
Thanks Nemi for your insight...
Rt now i am using a button to refresh the data...I was thinking onlyto display last few dates log files...Can I do do this like using a str method & display only for last few dates..orcan u suggest me something..
Thanks once again
August 2nd, 2002, 08:12 PM
-
Not sure what you are saying exactly. I would need more details about the specifics of your problem to get any more into it. I rarely give the answer to a problem to someone, usually just point them in the right direction. If you look at the InputStream methods you will see that there are several methods to get the size of the stream and to specify where you want to start reading from. I would suggest using these to display only a portion of the data at a time so as not to overwhelm the users connection. GL
August 5th, 2002, 01:00 PM
-
The applet I have rt now displays the entire contents of the log file, but I need to display only last 300-400lines from the log file. So how do i approach this? And also since the log file is dyanmic the data gets updated on the server, can i make the applet dynamic something like a news ticker or stock quotes getting updated...Thanks !
August 6th, 2002, 02:51 AM
-
Hi Som,
I have an idea but I am not sure if you can do that but i guess it is possible.
Can you write some script on the server like a java servlet or a perl script and read the log file from the server and return only the last few lines of the log file and then call this script from the applet. This way you will not have to download the whole log file and writing such script in perl should be easy
hope that helps
Navneet
August 8th, 2002, 08:42 AM
-
Looking into the inputstream methods available, all appear to linearly read through the stream, even using the skip() method. Therefore, I cannot see how you would be able to read only the last 300 lines without reading all the previous ones as well.
Given that, I cannot see how you would implement this without some sort of server side scripting. A JSP, Servlet, or Perl script would allow you to open the file and output only the last 300 lines to the applet. I cannot see another way.
August 8th, 2002, 09:54 AM
-
Or can I just read the last few bytes & display..Infact the applet is displaying the last few(which i can specify) lines. But since I using java.util package, I am getting an error displaying the applet asthe broswers supprot Jvm1.1.5 & they need a plug in ..
Is there any workaround apart from using plugin
here is my code
BufferedReader in = new BufferedReader(new InputStreamReader(hpCon.getInputStream()));
String line = null;
Vector strList = new Vector();
int numToDisplay = 50;
while ((line=in.readLine())!=null)
{
strList.addElement(line);
if (strList.size() > numToDisplay) {
strList.remove(0);
}
}
Enumeration enum = strList.elements();
while (enum.hasMoreElements()) {
String strLine = (String)enum.nextElement();
larea.append(strLine + "\n");
}