Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Posts
    2
    Rep 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
  2. #2
  3. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    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.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Posts
    2
    Rep 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
  6. #4
  7. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    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.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    52
    Rep Power
    12

    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
  10. #6
  11. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    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
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Posts
    2
    Rep 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
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    52
    Rep Power
    12
    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
  16. #9
  17. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    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.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    52
    Rep Power
    12
    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
  20. #11
  21. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    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
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    52
    Rep Power
    12
    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 !
  24. #13
  25. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2002
    Posts
    2
    Rep 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
  26. #14
  27. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    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.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    52
    Rep Power
    12
    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");
    }
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo