Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    chalupabatman is offline Member
    Join Date
    Jul 2016
    Posts
    12
    Rep Power
    0

    Default Scan Directory For File Extensions

    Greetings! Attempting to write my 1st java program as a file scanner. What I want to do is given an input directory, this tool will scan the directory and output all files in the directory that have the specified file extension. I have no syntax errors in my code, but I am not getting anything output. Did I improperly code something here?
    Java Code:
    import java.io.File;
    import java.util.List;
    
    import org.apache.commons.io.FileUtils;
    
    
    public class TestClass {
    	public static void main(String[] args) {
    		File dir = new File("C:\\Test\\");
    		String[] fileExtensions = { ".txt" };
    		List<File> files = (List<File>) FileUtils.listFiles(dir, fileExtensions, true);
    		for (File file : files) {
    			System.out.println(file);
    			System.console().writer().println(file);
    		}
    	}
    }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Scan Directory For File Extensions

    Change System.console().writer().println(file) to System.out.println(file)

    Edit: oh wait, that's already in there. Isn't the list just empty then? What does: System.out.println( files.size() ); say?
    Last edited by SurfMan; 07-21-2016 at 03:10 PM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    chalupabatman is offline Member
    Join Date
    Jul 2016
    Posts
    12
    Rep Power
    0

    Default Re: Scan Directory For File Extensions

    Quote Originally Posted by SurfMan View Post
    Change System.console().writer().println(file) to System.out.println(file)

    Edit: oh wait, that's already in there. Isn't the list just empty then?
    The list shouldn't be empty. The directory C:\Test contains 1 file test1 which is a .txt file.

    I am not showing file extensions on my PC could that be the culprit since when you open it in windows explorer it is test1?

    Scan Directory For File Extensions-capture.png

    EDIT -->
    Running
    Java Code:
    System.out.println( files.size() );
    Also returns nothing to the console.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Scan Directory For File Extensions

    What about changing TXT to txt or vice versa...
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Scan Directory For File Extensions

    Oh, and ALWAYS make Windows show the extension. It has nothing to do with Java, but it will make youre life so much easier.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    chalupabatman is offline Member
    Join Date
    Jul 2016
    Posts
    12
    Rep Power
    0

    Default Re: Scan Directory For File Extensions

    I altered my code to the below but still not seeing my file returned from java syntax
    Java Code:
    String[] fileExtensions = { ".TXT", "TXT", ".txt" }
    I like to hide the extensions as I know what the images are for, why do you recommend showing them?

    EDIT
    The only thing I see in the console window when I run my syntax is:
    <terminated> TestClass [Java Application] C:\Program Files\Java\jre1.8.0_91\bin\javaw.exe (Jul 21, 2016, 9:18:42 AM)
    Last edited by chalupabatman; 07-21-2016 at 03:20 PM.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Scan Directory For File Extensions

    Instead of using the Apache import, why not just use File.listFiles() and return an array to see what is there. You can always
    use a FileExtension filter later on (and it will be completely Java API with no third party requirements).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Scan Directory For File Extensions

    It's treating the '.' as part of the extension.
    Just put the extension in, no '.'
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Scan Directory For File Extensions

    Quote Originally Posted by chalupabatman View Post
    I altered my code to the below but still not seeing my file returned from java syntax
    Java Code:
    String[] fileExtensions = { ".TXT", "TXT", ".txt" }
    Funny how you missed "txt" which works :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  10. #10
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Scan Directory For File Extensions

    Java Code:
    Collection<File> files = FileUtils.listFiles(new File("."), new String[]{"txt"}, true);
    for (File file : files) {
      System.out.println("file = " + file);
    }
    Gives me:
    Java Code:
    file = ./src/bsh/util/README.txt
    file = ./resources/readme.txt
    file = ./oem/cmat/README.txt
    file = ./oem/cmat/scripting/examples/logfile.txt
    file = ./oem/cmat/eula.txt
    file = ./oem/default/README.txt
    file = ./oem/default/scripting/examples/logfile.txt
    file = ./oem/default/eula.txt
    file = ./oem/default/uac.txt
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  11. #11
    chalupabatman is offline Member
    Join Date
    Jul 2016
    Posts
    12
    Rep Power
    0

    Default Re: Scan Directory For File Extensions

    @SurfMan - I could not get your syntax to execute on my machine? However as you stated altering my syntax to the below works, BUT it throws an error in the console at the end. This is syntax
    Java Code:
    import java.io.File;
    import java.util.List;
     
    import org.apache.commons.io.FileUtils;
     
     
    public class TestClass {
        public static void main(String[] args) {
            File dir = new File("C:\\Test\\");
            String[] fileExtensions = { "txt" };
            List<File> files = (List<File>) FileUtils.listFiles(dir, fileExtensions, true);
            for (File file : files) {
                System.out.println(file);
                System.console().writer().println(file);
            }
        }
    }
    This is the error I get
    Exception in thread "main" java.lang.NullPointerException
    at TestClass.main(TestFileMoveClass.java:22)

  12. #12
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Scan Directory For File Extensions

    The answer is in the Exception: TestFileMoveClass.java:22
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  13. #13
    chalupabatman is offline Member
    Join Date
    Jul 2016
    Posts
    12
    Rep Power
    0

    Default Re: Scan Directory For File Extensions

    Quote Originally Posted by SurfMan View Post
    The answer is in the Exception: TestFileMoveClass.java:22
    I do not follow. Does that mean line 22? If it is line 22 in my syntax there is only a closing bracket on that line...

  14. #14
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Scan Directory For File Extensions

    Something on line 22 in class TestFileMoveClass is null and you are invoking a method on it. That, or you are adding a null to a collection that does not allow nulls, calling get() on a Map with a null-key, etc)

    Please post unaltered code and output from the same run. There is not even a mention of that class in your previous example, so the exception and the code you show do not match.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  15. #15
    chalupabatman is offline Member
    Join Date
    Jul 2016
    Posts
    12
    Rep Power
    0

    Default Re: Scan Directory For File Extensions

    Quote Originally Posted by SurfMan View Post
    Something on line 22 in class TestFileMoveClass is null and you are invoking a method on it. That, or you are adding a null to a collection that does not allow nulls, calling get() on a Map with a null-key, etc)

    Please post unaltered code and output from the same run. There is not even a mention of that class in your previous example, so the exception and the code you show do not match.
    The code I posted is my actual code. I think the naming difference comes into play with it was originally named TestFileMoveClass --> But I right clicked and renamed it to just TestClass. The syntax in my initial post and below are all of the code I have
    Java Code:
    import java.io.File;
    import java.util.List;
     
    import org.apache.commons.io.FileUtils;
     
     
    public class TestClass {
        public static void main(String[] args) {
            File dir = new File("C:\\Test\\");
            String[] fileExtensions = { "txt" };
            List<File> files = (List<File>) FileUtils.listFiles(dir, fileExtensions, true);
            for (File file : files) {
                System.out.println(file);
                System.console().writer().println(file);
            }
        }
    }
    EDIT -- and line 22 in Eclipse is the closing bracket for the for loop

  16. #16
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Scan Directory For File Extensions

    You don't *have* line 22. Look at the code you posted: line 22 does not even exist. How can you say it's the actual code?

    Run it again, with THIS code and paste the ACTUAL OUTPUT. All of it. No modifications, no altering, no cutting and snipping. Just as-is.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  17. #17
    chalupabatman is offline Member
    Join Date
    Jul 2016
    Posts
    12
    Rep Power
    0

    Default Re: Scan Directory For File Extensions

    I removed a few comment lines before I pasted in here. With the comments added in it ends up being 24 lines. Here is a straight copy/paste with comments left intact
    Java Code:
    import java.io.File;
    import java.util.List;
    
    import org.apache.commons.io.FileUtils;
    
    
    public class TestClass {
    	public static void main(String[] args) {
    		File dir = new File("C:\\Test\\");
    		//Declaring the file extensions to search for
    		String[] fileExtensions = { "txt", "TXT", "mp4", "avi", "mkv", "m4a" };		
    		//Creating a List<> of file names to move
    		List<File> files = (List<File>) FileUtils.listFiles(dir, fileExtensions, true);
    		for (File file : files) {
    			/**
    			 * This is where to code the actual move 
    		    */	
    			//Files.move(Paths.get(arg0), Paths.get());
    			System.out.println(file);
    			System.console().writer().println(file);
    			System.out.println( files.size() );
    		}
    	}
    }

  18. #18
    chalupabatman is offline Member
    Join Date
    Jul 2016
    Posts
    12
    Rep Power
    0

    Default Re: Scan Directory For File Extensions

    I found the culprit, the line throwing the issue was this
    Java Code:
    System.console().writer().println(file);

  19. #19
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Scan Directory For File Extensions

    How did you arrive at files.size() -- line 21?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  20. #20
    chalupabatman is offline Member
    Join Date
    Jul 2016
    Posts
    12
    Rep Power
    0

    Default Re: Scan Directory For File Extensions

    Quote Originally Posted by jim829 View Post
    How did you arrive at files.size() -- line 21?

    Regards,
    Jim
    Suggestion in Post #2

Page 1 of 2 12 LastLast

Similar Threads

  1. Lucene File Extensions supported and unsupported
    By shanti.dhanus in forum Lucene
    Replies: 0
    Last Post: 11-06-2012, 10:34 AM
  2. checking file extensions
    By gisler in forum New To Java
    Replies: 3
    Last Post: 12-05-2009, 01:25 PM
  3. Replies: 2
    Last Post: 05-11-2009, 10:07 AM
  4. Declaring Custom File Extensions
    By Jesdisciple in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 05-07-2008, 10:44 PM
  5. How to compare file extensions?
    By Rubin in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 09:51 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
  •