Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 170
  • Last Modified:

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?
0
javagirip
Asked:
javagirip
  • 4
  • 4
  • 2
1 Solution
 
CEHJCommented:
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.
0
 
TimYatesCommented:
Isn;t this EXACTLY the same question as here:

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20527564.html

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

I'll go and hide over here....just try to ignore me ;-)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
CEHJCommented:
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.
0
 
javagiripAuthor Commented:
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)
0
 
javagiripAuthor Commented:
or who ever answers my question!!
0
 
CEHJCommented:
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(); }
    }
 }

}
0
 
javagiripAuthor Commented:
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?
0
 
CEHJCommented:
Try

System.out.println(fileName.substring(fileName.lastIndexOf(" ") + 1));
0
 
javagiripAuthor Commented:
Great!

Thanks
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now