1

I've written simple java code to display file list in Linux to use it for FTP program using TCP(I'm going step by step, using all features). I'm posting this question after googling for about half an hour.

import java.io.*;
class FileList
{
public static void main(String args[])
{
String dirname="/home/vaibhav/Test";
File f1=new File(dirname);
String sl[]=f1.list();
System.out.print(f1.list()+"......."+sl.length+" files found.");
for(int i=0;i<sl.length;i++)
{ 
System.out.print(sl[i]+" ");
}      
}
}

Output:

vaibhav@vaibhav-VirtualBox:~/Java$ javac FileList.java
vaibhav@vaibhav-VirtualBox:~/Java$ java FileList
Exception in thread "main" java.lang.NullPointerException
    at FileList.main(FileList.java:10)

My Directories Test and Java contents:

vaibhav@vaibhav-VirtualBox:~$ cd /home/vaibhav/Test
vaibhav@vaibhav-VirtualBox:~/Test$ dir
1  2
vaibhav@vaibhav-VirtualBox:~/Test$ ls -al
total 8
drwxrwxrwx  2 vaibhav vaibhav 4096 Jan 18 15:04 .
drwxr-xr-x 25 vaibhav vaibhav 4096 Jan 18 16:50 ..
-rw-rw-rw-  1 vaibhav vaibhav    0 Jan 18 15:04 1
-rw-rw-rw-  1 vaibhav vaibhav    0 Jan 18 15:04 2
vaibhav@vaibhav-VirtualBox:~/Test$ cd
vaibhav@vaibhav-VirtualBox:~$ cd /home/vaibhav/Java
vaibhav@vaibhav-VirtualBox:~/Java$ ls -al
total 20
drwxr-xr-x  2 vaibhav vaibhav 4096 Jan 18 16:52 .
drwxr-xr-x 25 vaibhav vaibhav 4096 Jan 18 16:50 ..
-rw-r--r--  1 vaibhav vaibhav  990 Jan 18 16:52 FileList.class
-rw-r--r--  1 vaibhav vaibhav  477 Jan 18 16:52 FileList.java
-rw-r--r--  1 vaibhav vaibhav  487 Jan 18 16:52 FileList.java~

When I changed String dirname to /home/vaibhav/Java which is pwd too, program executed well.

What was the issue? Thank you.

4
  • for loop line is line 10 Commented Jan 18, 2014 at 11:58
  • Mmmh ... I doubt this, why we don't get output from print. I think sl is null. What gives f1.isDirectory(). Commented Jan 18, 2014 at 12:03
  • no, sl isn't null. I added 2 files to it, before running code. Commented Jan 18, 2014 at 12:07
  • Is it related with pwd? Commented Jan 18, 2014 at 12:12

1 Answer 1

0

Better to try this:

File folder = new File("your/path");
File[] listOfFiles = folder.listFiles();

    for (int i = 0; i < listOfFiles.length; i++) {
      if (listOfFiles[i].isFile()) {
        System.out.println("File " + listOfFiles[i].getName());
      } else if (listOfFiles[i].isDirectory()) {
        System.out.println("Directory " + listOfFiles[i].getName());
      }
    }

Hope this helps you

Sign up to request clarification or add additional context in comments.

7 Comments

I tried that too before posting here. But one post stackoverflow.com/a/11643057/2642938 said that using list() is better than using listFiles(). My main question is "When I changed String dirname to /home/vaibhav/Java which is pwd too, program executed well."
can you explain more clearly what is your requirement exactly @VAL
To display file lists from .../Test dir. @SanthoshKumar544
so what ever the answer i posted definitely displays file lists from your desired path and also including directories @VAL
yeah, I don't know why it didn't display it before that! That's why I posted it here. Now after changing dirname multiple times its working well.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.