Results 1 to 7 of 7

Thread: Filelist

  1. #1
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Filelist

    Hi,

    I have been playing around with files in a small java Application I am making.
    It Works, but I am not completly satisfied with my code. I think there could be a easier way of doing it.

    Heres my case:
    I have a path to where my files are. I then wants to list my files recursively, split out the Complete path (path + subdirectory) and the filename.
    Then I write the result to a MySQL table.
    I have accomplished this. Heres the code:
    Java Code:
    public void ListSource(String pathname) {
            
            File folder  = new File(pathname);
            File[] files = folder.listFiles();
    
            if (files == null) return;
            
            for (File file : files) {
                if ( file.isDirectory() ) {
                    ListSource( file.getAbsolutePath() );
                    file.getAbsoluteFile();
                    String path = file.toString();
                }else {
                    file.getAbsoluteFile();
                    long modified      = file.lastModified();
                    Date modifiedDate  = new Date(modified);
                    String filename    = file.toString();
                    String outfile     = filename;
                    String [] splittet = outfile.split("\\\\");
                    String st          = splittet[0];
                    String directory1  = splittet[1];
                    String directory2  = splittet[2];
                    String directory3  = splittet[3];
                    String fileName    = splittet[4];
                    String directory = "\\" + directory1 + "\\" + directory2 + "\\" + directory3 + "\\";
                    
                    SourceTable.insert(fileName, directory, pathname, modifiedDate);
                }
                }
            }
    However the drawback of this is that this only works if the path and subdirectorys are exactly like this.
    I could write som if conditions like this:
    Java Code:
    if(splittet.length == 5)
    and so on...

    But there must be a easier way of identify the filename and show the Complete path?
    Any suggestions?

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Filelist

    Yep. Get the name via File.getName(). Get the absolutePath via File.getAbsolutePath(). Then use substring on absolutePath to cut off the filename at the end.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Filelist

    Thanks for a good pointer in the right direction. :)

    Here is my code now:
    Java Code:
    String fileName    = file.getName();
    String directory   = file.getAbsolutePath();
    long modified      = file.lastModified();
    Date modifiedDate  = new Date(modified);
    But I am not sure how to use substring.
    When I run this, the string Directory contains the complete path including the filename.

    Do you have an example?

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,743
    Rep Power
    5

    Default Re: Filelist

    Why ask for one? Google "java substring example" and get one of the hundred that already exists. Then you don't ask people in this forum to do what has been done countless times before, and you save yourself waiting time.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Filelist

    A pointer, you want to remove the filename from the path and you know how long the filename is.
    API: String.substring(int,int)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    asai is offline Senior Member
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    115
    Rep Power
    0

    Default Re: Filelist

    I might be a little slow... But how do I now how long the filename is?
    The substring(int,int) I understand, but how to find the int,int?

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: Filelist

    What are the two ints for? What does the API say? I think you haven't read the API docs.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Posting Permissions

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