?
Solved

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

Posted on 2003-02-25
10
Medium Priority
?
169 Views
Last Modified: 2010-03-31

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
Comment
Question by:javagirip
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8016473
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
 
LVL 35

Expert Comment

by:TimYates
ID: 8016485
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
 
LVL 35

Expert Comment

by:TimYates
ID: 8016488
No...sorry....my mistake :-(

I'll go and hide over here....just try to ignore me ;-)
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 86

Expert Comment

by:CEHJ
ID: 8016510
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
 

Author Comment

by:javagirip
ID: 8016667
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
 

Author Comment

by:javagirip
ID: 8016695
or who ever answers my question!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8016737
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
 

Author Comment

by:javagirip
ID: 8017168
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 180 total points
ID: 8017323
Try

System.out.println(fileName.substring(fileName.lastIndexOf(" ") + 1));
0
 

Author Comment

by:javagirip
ID: 8017905
Great!

Thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question