Link to home
Start Free TrialLog in
Avatar of javagirip
javagirip

asked on

How do I read the file names in a server directory?


How do I read the file names from a server directory. For example, in the following example I want to read the all the file names in the "book" directory.

"http://www/google.book"

I tried constructing a File object as

File f1 = new File("http://www/google.book");
if(f1.isDirectory())
{
String s[] = f1.list();
//then read the String Array
}

but I am getting null pointer exception. I think it is not accepting the format of the path I am sending.

Any idea?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You can't. If the server has an FTP service, you can list the files. If it's simply a web page, you might be lucky that the response to a request for a directory lists the files in that directory, which you could theoretically parse.
Isn;t this EXACTLY the same question as here:

https://www.experts-exchange.com/questions/20527564/How-to-read-a-server-side-file.html

Do you have the page where you created the question bookmarked?
No...sorry....my mistake :-(

I'll go and hide over here....just try to ignore me ;-)
Well the question does appear there too (at the end). Had i realised that before, i wouldn't have answered this one, as the last time i answered the other question, somebody else got the points for implementing my answer.
Avatar of javagirip
javagirip

ASKER

Hi CEHJ, Can you tell me how I read the files from an ftp site? (this time I will make sure you get the points)
or who ever answers my question!!
import sun.net.ftp.*;
import java.io.*;
import java.util.*;
import sun.net.*;


public class FtpList2 {


  public static void main(String[] args){
    if(args.length < 4){
     System.out.println("Usage: java FtpList <host> <user name> <password> <remote directory>");
     System.exit(-1);
    }
    FtpClient client = null;
    TelnetInputStream tis = null;
    BufferedReader in = null;
    String fileName = null;
    try {
      client = new FtpClient(args[0]);
      client.login(args[1],args[2]);
      client.cd(args[3]);
      tis = client.list();
      in = new BufferedReader(new InputStreamReader(tis));
      while ((fileName = in.readLine()) != null){
        System.out.println(fileName);
      }
    }
    catch (IOException e){
      e.printStackTrace();
    }
    finally {
      try { in.close(); } catch(IOException e) { e.printStackTrace(); }
    }
 }

}
Excellent, it works good, but I just need file name, but I am getting lot other stuff like:

-rw-r--r--   1 11742    997          342 Feb 25 04:22 .dir
-rw-r--r--   1 11742    997          345 Feb 24 12:22 file1.txt
-rw-r--r--   1 11742    997          262 Feb 24 12:22 file2.txt
-rw-r--r--   1 11742    997          288 Feb 24 12:22 file3.txt

Here I just need file1.txt,file2.txt,file3.txt

ANy idea?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great!

Thanks