Using the File Class


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Using the File Class

  1. #1
    Join Date
    Sep 2003
    Posts
    16

    Using the File Class

    For a program I have been working on, I have been creating some functions that searches for files. For the purpose of the applet, all the files that are being searched for are within the same folder of the java applet is being ran. What I have been doing is using a URLConnection. However, this requires the exact name of the file and I wanted to add more functionality to it.

    I have been experimenting with the File Class to try and print out a list of all the files contained within the folder the applet is running from.
    Code:
         url = getCodeBase();
         try {
               uri = new URI(url.toString());
         } catch (URISyntaxException e) {
               setBackground(Color.YELLOW);
               // for no particular reason
         }
         f = new File(uri);
    However, I haven't had much success. I can drawString with the f.getName, etc. However, I cannot use the f.list() command to print out everything in the directory, or use some of the other functions associated with the File class.

    Should I be looking at another class to use instead of the File class within my applet? Thanks.

    [ArchAngel added CODE tags]
    Jerod Brown

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    Please post any error messages you've been getting.

    You do realise that applets run on the client, not the server?

    You do realise the security restrictions based upon applets?
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Sep 2003
    Posts
    16
    The messages I have been getting with the Java Console are pasted at the bottom, because there are quite a bit.

    Yes, I do realize that the applet runs on the client and not the server. What I am trying to figure out is if I can use the File class to examine files on the server. That is why I am trying to use the URI constructor method for the File.

    Yes, I also realize the security restrictions of applets. However, since I can essentially pull jpegs and other files from a server directory, is there not a method to examine the files before accessing them. Can I not see what jpegs are at a location before I decide which one I want to download?


    Perhaps it is my server settings ...


    Java Console Messages:

    java.security.AccessControlException: access denied (java.io.FilePermission C:\RADSS\Web read)

    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:270)

    at java.security.AccessController.checkPermission(AccessController.java:401)

    at java.lang.SecurityManager.checkPermission(SecurityManager.java:542)

    at java.lang.SecurityManager.checkRead(SecurityManager.java:887)

    at java.io.File.canRead(File.java:636)

    at fileDemo.paint(fileDemo.java:28)

    at sun.awt.RepaintArea.paint(RepaintArea.java:177)

    at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:260)

    at java.awt.Component.dispatchEventImpl(Component.java:3699)

    at java.awt.Container.dispatchEventImpl(Container.java:1623)

    at java.awt.Component.dispatchEvent(Component.java:3480)

    at java.awt.EventQueue.dispatchEvent(EventQueue.java:450)

    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:197)

    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)

    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:144)

    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:136)

    at java.awt.EventDispatchThread.run(EventDispatchThread.java:99)
    Jerod Brown

  4. #4
    Join Date
    Mar 2003
    Posts
    834
    I'm pretty sure you're going to have problems if you keep this as an applet. For example, if you were writing an application, you would write:
    Code:
    import java.net.*;
    import java.io.*;
    
    public class Test {
    	public static void main(String[] args) throws IOException { 
    		URL url = new URL("http://www.trycatch.net/");
    		File root = new File(url.getFile());
    		String[] contents = root.list();	
    		for (int i = 0; i < contents.length; i++) {
    			System.out.println(contents[i]);
    		}
    	}
    }
    ...which works fine. However, when you write an applet version:
    Code:
    import java.io.*;
    import java.net.*;
    import javax.swing.*;
    
    public class TestApplet extends JApplet {
    	public void init() {
    		try {
    			URL url = new URL("http://www.trycatch.net/");
    			File root = new File(url.getFile());
    			String[] contents = root.list();	
    			for (int i = 0; i < contents.length; i++) {
    				System.out.println(contents[i]);
    			}
    		}
    		catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    ...you get the following exception thrown:
    Code:
    ava.security.AccessControlException: access denied (java.io.FilePermission \ read)
    
    	at java.security.AccessControlContext.checkPermission(AccessControlContext.java:270)
    
    	at java.security.AccessController.checkPermission(AccessController.java:401)
    
    	at java.lang.SecurityManager.checkPermission(SecurityManager.java:542)
    
    	at java.lang.SecurityManager.checkRead(SecurityManager.java:887)
    
    	at java.io.File.list(File.java:912)
    
    	at TestApplet.init(TestApplet.java:10)
    
    	at sun.applet.AppletPanel.run(AppletPanel.java:348)
    
    	at java.lang.Thread.run(Thread.java:536)
    Sun agree with me (;-)):

    http://java.sun.com/sfaq/#prevent
    http://java.sun.com/sfaq/#socket
    ArchAngel.
    O:-)

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