Need help on Applet file streaming


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Need help on Applet file streaming

  1. #1
    Join Date
    Feb 2004
    Posts
    3

    Need help on Applet file streaming

    Hi!
    I'm new to java, java applets and this forum. To learn more about java applets I started making a version of the old breakout. As the gameplay proceeded I needed levels of course. But here is the problem. When I try get a file using a FileReader object, and then reading it byte by byte to build the level, it works fine on my local machine. But when I try to open it on a remote computer I just get the error message "Applet crashed." Now I know that applets have limitations on file streams, although I can't figure out exactly what I am allowed to do. Is there no way that I can download a levelfile (consisting of a series of chars representing blocks) and put it in a char vector? Later I was planning to make a level editor too, so you can make levels on you local computer and then upload them. Will I have the same problems with this?
    I am very green when it comes to java and applets so please keep responses in a fairly low tech language, otherwise I cannot understand them

    I am thankful for any help I can get!

    Thanks for your time!

    /Magnus Andersson

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    applets are not allowed to access the local file system, network (other than the server they came from) or use the java native interface.

    all pose security risks that could destroy user data

    make your levels in text files on the server, then load them off the server upon each new level.. (use URL, and download each level from the server)

    or, include them in the applet's jar file.. afaicr, applets may load resources out of their own jars, but nothing else
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    Feb 2004
    Posts
    3
    So when I use FileReader and tries to read a file from the folder "/levels/FILE.txt" it tries to open it on the local machine and not from the server that provides the applet? How exactly would I read and write textfiles on the server?

  4. #4
    Join Date
    Feb 2004
    Posts
    808
    use a URL, and download the content?

    heres a quick and nasty demonstration:

    Code:
    public class test112{
    
      public static void main(String[] argv)throws Exception{
        java.net.URL u = new java.net.URL("http://www.google.com");
        
        java.io.InputStream is = u.openStream();
        
        byte[] buf = new byte[1024];
        
        for(int bytesRead = is.read(buf); bytesRead != -1; bytesRead = is.read(buf)){
          System.out.write(buf, 0, bytesRead);
        }
        
      }
    }
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  5. #5
    Join Date
    Feb 2004
    Posts
    3
    Ah! That's exactly what I needed! Thanks alot for the answer!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center