Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: If a file extension is known, but not the name, how do you find it?

  1. #1
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post

    Question If a file extension is known, but not the name, how do you find it?

    I'm working on a project that I started, and an attempting my first shot at networking so to speak. Lets say my program looks for "updates" by reading in a few lines from a textfile on a webserver. I can do that fine. Now lets say it wants to look for updates to plugins. If the base path to the plugin's update is known and the extention of the information file, how would I search for that file? I know that on some web servers, when you go up one in a directory, it lists the contents of that directory. Here's the code that i'm using:
    File[] listOfFiles = new File(currentURL.getPath()).listFiles(); 
    			for (int i = 0; i < listOfFiles.length; i++){
    			   if (listOfFiles[i].isFile()){
    			       if (listOfFiles[i].getName().endsWith(".info") || listOfFiles[i].getName().endsWith(".INFO"))
    			       {
    			          hasFoundInfoFile = true;
    			          InfoFile = listOfFiles[i].getAbsoluteFile();
    			       }
    			       if (hasFoundInfoFile)
    			    	   break;
    			   }
    			}
    but i'm getting null pointer exceptions on this line:
    for (int i = 0; i < listOfFiles.length; i++){

    I checked the path, and that exists, i even made sure that a file of the extension .info exists in that directory. What am I doing wrong?


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: If a file extension is known, but not the name, how do you find it?

    Have you looked in the API docs for java.io.File? There's an explanation in there of why you might be getting a null return value from listFiles(). You're doing the right kind of thing with your code, given what you say you want to achieve. While you're in the API docs for java.io.File, have a look at the directory listing methods that take a FileFilter or FilenameFilter argument.

  3. #3
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: If a file extension is known, but not the name, how do you find it?

    Ok, so java doesn't treat it as a directory. Is there a way around this without mirroring the whole directory on the local filesystem?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: If a file extension is known, but not the name, how do you find it?

    my program looks for "updates" by reading in a few lines from a textfile on a webserver.
    Can you explain where the code you show is executing?
    What does a server have to do with looking at files in a folder on your computer?
    The listFiles() method has an overload that takes a Filter you can use to chose what files to include.

    File[] listOfFiles = new File(currentURL.getPath()).listFiles();
    I'd do this in two steps:
    File theDir = new File(currentURL.getPath());
    // Here test  theDir 
    File[] listOfFiles = theDir.listFiles();

  5. #5
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: If a file extension is known, but not the name, how do you find it?

    Basically, My program pulls information from plugin developers "repositories" that they host on their web servers. In the base path that they give out to users is a file PLUGIN_NAME.repo. At runtime I do not know the exact name of PLUGIN_NAME, but only that it exists in the URL given and it has the extension of .repo. Does this help?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: If a file extension is known, but not the name, how do you find it?

    Can you explain where the code you show is executing? On the server or on your PC or ???

    What does a server have to do with looking at files in a folder on your PC?

    If the files are on a server and your code is executing on your PC, is there a way for you to ask the server for a list of files it has locally on its computer? What protocol are you using?

  7. #7
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: If a file extension is known, but not the name, how do you find it?

    The code executes client side. My program is opened by the end user on their computer. When given a web url entered by the user(ex: "http://example.com/Plugin/"), the program should try to search IANA &mdash; Example domains for *.repo. The first time it finds a match, it reads the contents in as a text file. The problem is that java doesn't appear to treat URLs as directories. I'm trying to avoid having to write server code, as it will be easier for the audience i'm targeting to just upload a folder with some files to a web server than to have them run a server applet 24/7

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: If a file extension is known, but not the name, how do you find it?

    The problem is that java doesn't appear to treat URLs as directories.
    Can you explain this a bit? What class are you using the URL with?
    It could depend on the protocol you are using in the URL.

  9. #9
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: If a file extension is known, but not the name, how do you find it?

    Here's an example of the structure i'm using: "http://techwizmc.co.cc/Test/"
    In that url, there is a file called project.repo. Using standard directory listing methods, I get null pointer exceptions, I think because java doesn't treat this as a directory, as
    File path = new File("http://techwizmc.co.cc/Test/");
    if(!path.isDirectory()){
        System.out.println("error");
    }
    always executes.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: If a file extension is known, but not the name, how do you find it?

    As far as I know the File class looks on the local PC.
    Otherwise the server would need to cooperate using some protocol, like FTP. Not all servers support that protocol.

  11. #11
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: If a file extension is known, but not the name, how do you find it?

    I was afraid of that. I think I might have to change the standard then, and make the name of the file match the name of the last directory. Thanks for the help though.

  12. #12
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: If a file extension is known, but not the name, how do you find it?

    Norm is right - you can't do that. URL.getPath() returns the string that ends up in the middle part of the first line in a HTTP request. It's completely useless as a File path on the client side. If your webserver supports directory listings out-of-the-box, you could do what you want by requesting the directory listing page from the webserver and parsing the returned page. It would be grim. Browse archive.ubuntu.com/ for an example Apache directory listing.

Similar Threads

  1. How to add file extension to JFileChooser.showSaveDialog() ?
    By camboy8 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 25th, 2012, 09:23 AM
  2. Cannot find Symbol?
    By defmetalhead in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 5th, 2011, 08:48 AM
  3. can any one plz find the error.
    By shalini_sns in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 23rd, 2011, 05:26 PM
  4. find LCS
    By Anemone_ in forum Algorithms & Recursion
    Replies: 1
    Last Post: November 28th, 2010, 02:03 PM
  5. where can i find the tmp/foo directory?
    By Idy in forum Java IDEs
    Replies: 11
    Last Post: January 19th, 2010, 11:21 AM