Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now!
  #1  
Old August 1st, 2002, 01:30 AM
navneet77 navneet77 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 2 navneet77 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #2  
Old August 1st, 2002, 03:19 AM
bricker42 bricker42 is offline
Moderator =(8^(|)
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2002
Location: Sacramento, CA
Posts: 1,710 bricker42 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 38 sec
Reputation Power: 8
Send a message via AIM to bricker42
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.

Reply With Quote
  #3  
Old August 1st, 2002, 07:08 AM
navneet77 navneet77 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 2 navneet77 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #4  
Old August 1st, 2002, 09:25 AM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
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.

Reply With Quote
  #5  
Old August 1st, 2002, 04:07 PM
Som Som is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 52 Som User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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

Reply With Quote
  #6  
Old August 1st, 2002, 07:50 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
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

Reply With Quote
  #7  
Old August 1st, 2002, 09:34 PM
navneet77 navneet77 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 2 navneet77 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #8  
Old August 2nd, 2002, 09:04 AM
Som Som is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 52 Som User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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

Reply With Quote
  #9  
Old August 2nd, 2002, 03:34 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
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.

Reply With Quote
  #10  
Old August 2nd, 2002, 04:39 PM
Som Som is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 52 Som User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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

Reply With Quote
  #11  
Old August 2nd, 2002, 08:12 PM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
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

Reply With Quote
  #12  
Old August 5th, 2002, 01:00 PM
Som Som is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 52 Som User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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 !

Reply With Quote
  #13  
Old August 6th, 2002, 02:51 AM
navneet77 navneet77 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Posts: 2 navneet77 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #14  
Old August 8th, 2002, 08:42 AM
Nemi Nemi is offline
Clueless llama
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Feb 2001
Location: Lincoln, NE. USA
Posts: 2,353 Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level)Nemi User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 2 Days 12 h 35 m 19 sec
Reputation Power: 111
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.

Reply With Quote
  #15  
Old August 8th, 2002, 09:54 AM
Som Som is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 52 Som User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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");
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Reading file from applet

«