import java.io.*;
import java.util.*;
 
public class MainTwo {
public static void main(String[] args) {
        for(File file : File.listRoots()){ 
            search(file);
        }
    }

    public static void search(File f) 
    {  
        ArrayList<File> arrayList = new ArrayList<File>();
            f.listFiles();
            arrayList.add(f);
            
            System.out.println (f.getName());
        
        
    }
}

Why does it print nothing?

i just tried a differrent approach, this code also prints out nothing

import java.io.*;
import java.util.*;
 
public class MainTwo 
{
    public static void main(String[] args) 
    {
        File[] filesOnComputer = File.listRoots(); 
        {
            ArrayList<File> listOfFiles = new ArrayList<File>();
            for(int index = 0; index < filesOnComputer.length; index++)
            {
                listOfFiles.add(filesOnComputer[index]);    
                System.out.println (filesOnComputer[index].getName());
            }
        }
    }
}

why does it print out nothing??

Do you mean prints nothing, or just "c:"? Because your code just seems to print the results of the listRoots. You do call listFiles, but you do nothing with it.

i see two issues:
1) First u r calling listRoots, tht must print C:\
2) Secondly, u r not trying to get the iterate through the list of files that u got...

Let see ur code:
File[] filesOnComputer = File.listRoots();

this part shall give u the root of the directory structure (in most cases it would be C:, in case it is partioned it might also give D:)

then u will have to chk whether this is a directory, and if it is then u will have to call ur search function with little modification as:

public static void search(File f) 
    {  
        ArrayList arrayList = new ArrayList();
        File[] files= f.listFiles(); // get all files under the direcrory
        for(int index = 0; index < files.length; index++)
        {
            System.out.println ("here we are---"+files[index].getName());

            arrayList.add(files[index].getName());
        }      
            System.out.println (f.getName());


    } 

u might need to also call this recursively to see if the next selection is a file or a directory.

Try this :

public static void main(String[] args) 
    {
        File[] filesOnComputer = File.listRoots(); 
        {
            ArrayList listOfFiles = new ArrayList();
            System.out.println (filesOnComputer.length);

            for(int index = 0; index < filesOnComputer.length; index++)
            {
                listOfFiles.add((File)filesOnComputer[index]);    
                if(filesOnComputer[index].isDirectory()){
                System.out.println ("here we are-"+filesOnComputer[index].getPath());
                search((File)listOfFiles.get(index));
                }
                else
                    System.out.println ("This is not the directory-"+filesOnComputer[index].getPath());
            }
        }
    }
    public static void search(File f) 
    {  
        ArrayList arrayList = new ArrayList();
        File[] files= f.listFiles();
        for(int index = 0; index < files.length; index++)
        {
            System.out.println ("here we are---"+files[index].getName());

            arrayList.add(files[index].getName());
        }      
            System.out.println (f.getName());


    }

wow nicely explained thanks are you a teacher?

yep next step recurssion

Rather than the pre-1.5 old-style for loops and un-typed lists, replace

ArrayList arrayList = new ArrayList();
File[] files= f.listFiles(); // get all files under the direcrory
for(int index = 0; index < files.length; index++)
{
System.out.println ("here we are---"+files[index].getName());

arrayList.add(files[index].getName());
}

with

ArrayList arrayList<File> = new ArrayList<File>();
for(File ff : f.listFiles()) {
  System.out.println ("here we are---"+ff.getName());
  arrayList.add(ff.getName());
}

i got the recursion working, couldnt quite implent the new for loop, moved on to the next step, ei. trying to compare files in two ArrayLists, and the first hiccup is as follows:

ArrayList arrayList = new ArrayList();
            ArrayList arrayListAgain = new ArrayList();
            
            File[] files= f.listFiles();    
            for(int index = 0; index < files.length; index++)
            
            if (files[index].isFile())
            {
                 
                System.out.println ("here we are---"+files[index].getName());
               
                arrayList.add(files[index]);
                arrayListAgain.add(files[index]);
                
                if (arrayList(files[index]).length() == arrayListAgain(files[index]).length() && arrayList(files[index]).hashCode() != arrayListAgain(files[index]).hashCode())
                {
                    //do something;
                }
            }

Hmm my compiler friend tells me cannot find symbol - method arrayList(Java.io.File)??

The java class library for ArrayList was not helpful to me.

i got the recursion working, couldnt quite implent the new for loop, moved on to the next step, ei. trying to compare files in two ArrayLists, and the first hiccup is as follows:

ArrayList arrayList = new ArrayList();
            ArrayList arrayListAgain = new ArrayList();
            
            File[] files= f.listFiles();    
            for(int index = 0; index < files.length; index++)
            
            if (files[index].isFile())
            {
                 
                System.out.println ("here we are---"+files[index].getName());
               
                arrayList.add(files[index]);
                arrayListAgain.add(files[index]);
                
                if (arrayList(files[index]).length() == arrayListAgain(files[index]).length() && arrayList(files[index]).hashCode() != arrayListAgain(files[index]).hashCode())
                {
                    //do something;
                }
            }

Hmm my compiler friend tells me cannot find symbol - method arrayList(Java.io.File)??

The java class library for ArrayList was not helpful to me.

maybe you mean something like this?

ArrayList<File> arrayList = new ArrayList<File>();
    ArrayList<File> arrayListAgain = new ArrayList<File>();
    
    File[] files= f.listFiles();    
    for(int index = 0; index < files.length; index++)
    
    if (files[index].isFile())
    {
         
        System.out.println ("here we are---" + files[index].getName());
       
        arrayList.add(files[index]);
        arrayListAgain.add(files[index]);
        
        if (arrayList.get(index).length() == arrayListAgain.get(index).length() && 
          arrayList.get(index).hashCode() != arrayListAgain.get(index).hashCode())
        {
            //do something;
        }
    }

thanks jaka.ramdani


I tried making the modifcation you siggested, but the compiler error with that was:

cannot find symbol - method length

and then i tried using (files[index]) in the if statement, and then it said cannot find symbol - method get


so frustrating!

oh my fault i didnt make all the changes it does compile, rep for you!

oh my fault i didnt make all the changes it does compile, rep for you!

glad it helps .. thanks for the rep.

would some one be kind enough to copy and paste and compile this code, I get no results to the terminal, my criteria should bring a few results

import java.io.*;
import java.util.*;
 
public class MainFourA 
{
    public static void main(String[] args)    
    {
        File[] filesOnComputer = File.listRoots(); 
   
            ArrayList listOfFiles = new ArrayList();
            System.out.println (filesOnComputer.length);
            for(int index = 0; index < filesOnComputer.length; index++)
            {
                listOfFiles.add((File)filesOnComputer[index]); 
                if(filesOnComputer[index].isDirectory())
                {
                    System.out.println ("here we are-"+filesOnComputer[index].getPath());
                    search((File)listOfFiles.get(index));
                }
                else
                System.out.println ("This is not the directory-"+filesOnComputer[index].getPath());
            }
        }
    
    public static void search(File f) 
    {
        try
        { 
            ArrayList<File> arrayList = new ArrayList<File>();
            ArrayList<File> arrayListAgain = new ArrayList<File>();
    
            File[] files= f.listFiles();    
            for(int index = 0; index < files.length; index++)
            
            if (files[index].isDirectory())
            {
                search(files[index]);
            }

            else if (files[index].isFile())
            {
         
                System.out.println ("here we are---" + files[index].getName());
       
                arrayList.add(files[index]);
                arrayListAgain.add(files[index]);
        
                if (arrayList.get(index).length() == arrayListAgain.get(index).length() && 
                arrayList.get(index).hashCode() != arrayListAgain.get(index).hashCode())
                {
                    
                        System.out.println (arrayList.get(index).getName() + ", " + arrayList.get(index).length() + " bytes."); 

                }
    } 
            

            System.out.println (f.getName());
        }
            catch (Exception e)
            {
            }
        }
    }

it compiles, if you comment out the big if statement, then lots of results come to the terminal window

I hope know one is getting annoyed by me!

it does print now:

this is some sample output

$R3QSQ3M
here we are---$R49HV83.txt
$Recycle.Bin
here we are---autoexec.bat

confusing...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.