Results 1 to 4 of 4
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    9

    Default Is there a way to read a file directory

    Best java friends

    Is there a way to read a file directory in a specific folder inside the work directory form a applet . The following code does not work in an applet:

    directory = new File(dirname);
    if (directory.isDirectory() == false) {
    if (directory.exists() == false)

    files = directory.list(); // stores the list of file names


    willemjav

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    For security reasons, you can't actually..... ( im not sure )

    try to search it on the web....
    specially the article about Advantage/disadvantage of Applets....

    sukatoa.

  3. #3
    DonCash's Avatar
    DonCash is offline Moderator
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    240
    Rep Power
    9

    Default

    This code will list all the files in a given directory. In this case its '.' which is the current working directory:

    Java Code:
    String path = ".";
    File folder = new File(path);
    File[] listOfFiles = folder.listFiles(); 
    	   
    for (int i = 0; i < listOfFiles.length; i++) {
    		
    if (listOfFiles[i].isFile()) {
    files = listOfFiles[i].getName();
    
    System.out.println(files);
    
     }
    }
    Did this post help you? Please me! :cool:

  4. #4
    Lycrios is offline Member
    Join Date
    Sep 2011
    Posts
    1
    Rep Power
    0

    Smile Re: Is there a way to read a file directory

    Quote Originally Posted by DonCash View Post
    This code will list all the files in a given directory. In this case its '.' which is the current working directory:

    Java Code:
    String path = ".";
    File folder = new File(path);
    File[] listOfFiles = folder.listFiles(); 
    	   
    for (int i = 0; i < listOfFiles.length; i++) {
    		
    if (listOfFiles[i].isFile()) {
    files = listOfFiles[i].getName();
    
    System.out.println(files);
    
     }
    }
    I cleaned up your code a little.

    Java Code:
    String path = "C:/";
    File folder = new File(path);
    File[] listOfFiles = folder.listFiles();			   
    for (int i = 0; i < listOfFiles.length; i++) {
    	if (listOfFiles[i].isFile()) {
    		String files = listOfFiles[i].getName();
    		System.out.println(files);
    	}
    }

Similar Threads

  1. How to get Current Directory through File
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-05-2008, 11:14 AM
  2. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:13 AM
  3. signaling file(s) have reached in directory
    By ashu261 in forum Advanced Java
    Replies: 0
    Last Post: 02-04-2008, 08:45 AM
  4. Read file
    By tajinvillage in forum Threads and Synchronization
    Replies: 0
    Last Post: 01-29-2008, 10:10 AM
  5. How to Move a File to Another Directory
    By Ada in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 02:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •