Results 1 to 6 of 6

Thread: [RESOLVED] Java - Get list of files in C:\Windows directory

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Posts
    341

    Resolved [RESOLVED] Java - Get list of files in C:\Windows directory



    Hi everyone.
    I am just finding my way into java programming world. Can anyone please show me a sample code to list the items in my C: directory; such as:
    list of file names, their sizes/length etc?

    presently this is how far I've gone:

    Code:
    import java.lang.*;
    import java.lang.String.*;
    import java.io.*;
    import java.io.File;
    
    
    public class ListDirContents
    	{
    	//Define variables
    	//----------------
    
    	int i;
    
    	String flname;
    
    	String flsize;
    	
    	//create new instance of File
    	//---------------------------
    
    	File newfile = new File("C:/WINDOWS"); 
    
    	String filelist[] = newfile.list();
    
    	}
    	
    public class WriteDirContents Extends ListDirContents
    	{
    	public static void main (String[] args) throws ioException
    		{
    
    		for(i=0;i<filelist.length;++i) // Print out all files
    			{ 
    			flname = filelist.getName();
    		 
    			System.out.println("The file name is: " + flname);
    			
    			flsize = filelist.getLength();
    
    			System.out.println("The file size is: " + flsize); 
    			}
    		}
    
    	}
    Any assistance will be greatly appreciated.
    GiftX.

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java - Get list of files in C:\Windows directory

    The next time you post your code on the web, please make sure it works before posting. Anyway, I've got rid of some useless variables and added the iterative feature. Hope this helps
    Code:
    import java.io.File;
    
    public class Util {
    
        public static void main(String[] a) {
    	System.out.println("Scouting folder: " + "C:\\Windows");
    	listFiles("c:/windows", true);
        }
    
        private static void listFiles(String root, boolean iterative) {
    	File[] filesList = new File(root).listFiles();
    	for (int i = 0; i < filesList.length; ++i) {
    	    if (filesList[i].isFile()) {
    		System.out.println("File name: " + filesList[i].getName());
    		System.out.println("\t\tSize = " + filesList[i].length());
    	    } else if (iterative) {
    		System.out.println("Scouting folder: " + filesList[i].getPath());
    		listFiles(filesList[i].getPath(), true);
    	    }
    	}
        }
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java - Get list of files in C:\Windows directory

    Oh and by the way, you never need to import java.lang
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  4. #4
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Java - Get list of files in C:\Windows directory

    Quote Originally Posted by ComputerJy
    The next time you post your code on the web, please make sure it works before posting.
    If it was working, he wouldn't be posting here asking for help, would he?


    Has someone helped you? Then you can Rate their helpful post.

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java - Get list of files in C:\Windows directory

    Quote Originally Posted by manavo11
    If it was working, he wouldn't be posting here asking for help, would he?
    Good point but I meant a code that compiles successfully
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2007
    Posts
    341

    Re: Java - Get list of files in C:\Windows directory

    Hey 'Jy'.
    Thanks for your post. It worked. However, in response to your post, if the code compiles and works would I be posting it? I guess not.

    Manavo11, thanks for having my back on the above.

    Much appreciation to you all.

    GiftX.

Posting Permissions

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



Click Here to Expand Forum to Full Width