5 Replies - 313 Views - Last Post: 08 April 2011 - 06:23 PM Rate Topic: -----

#1 JazzRockFeel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-April 09

File Class

Posted 08 April 2011 - 11:03 AM

Hello D.I.C was wondering if you guys could help me with a problem

basically I have a java program that takes two arguments from the command line like this --> findfile <PATH> <FILENAME> and searches in that path and all its sub-directories for the file name input. Right now I'm just looking to display all of the sub-directories and the files in them I assume you have to do this recursively. I have been able to get it to display all of the files in the specified path but not its sub-directories any techniques or advice?

thanks in advance guys you are the best here is my code and output
import java.io.*;

public class findfile {

    public static void main(String[] args) throws Exception {
        String pParam = " ";
        String nParam = " ";

        pParam = args[0];
        nParam = args[1];

        File pathFile = new File(pParam);
        File fileName = new File(nParam);
        String path = pathFile.getPath();
        String name = fileName.getName();

        if (pathFile.exists() && pathFile.isDirectory()) {
            System.out.println(path + " exists!");
            System.out.println("Searching...");
        } else {
            System.out.println("Path " + path + " does NOT exist!");
        }
        File[] contents = pathFile.listFiles();

        for (int i = 0; i < contents.length; i++) {
            
            System.out.println("\t" + contents[i].getName());

        }

        
    }
}


This post has been edited by JazzRockFeel: 08 April 2011 - 11:04 AM


Is This A Good Question/Topic? 0
  • +

Replies To: File Class

#2 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 90
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: File Class

Posted 08 April 2011 - 11:50 AM

I would check each file in contents:
File[] contents = pathFile.listFiles();


to see if it is a directory, and if so, listFiles() again using that file.
A recursive call might be,
public File findFile(File directory, String fileName)
{
  for(File f: directory.listFiles())
  {
    if(f.getName().equalsIgnoreCase(fileName))
    {
      return f;
    }
  }
  
  File searchFile = null;
  for(File f: directory.listFiles())
  {
    if(f.isDirectory())
    {
     searchFile = findFile(f,fileName);
     if(searchFile != null)
     return searchFile;
    }
  }
  return null;
}


I'm not sure if that's correct as I haven't tested it, but I'd see if the file I'm looking for is in the current folder, if it is, return it. If it isn't in the folder, look in each subfolder(recursively) to see if it is. if there are no more directories to search, return null(Base Case?)
Was This Post Helpful? 1
  • +
  • -

#3 JazzRockFeel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 17-April 09

Re: File Class

Posted 08 April 2011 - 01:37 PM

Thanks I got the general idea of what im supposed to do but i keep getting a ArrayIndexOutOfBounds Exception any ideas here is my updated code... it begins to look through the sub-directories but fails.

thanks again!!

import java.io.*;

public class findfile
{

    public static void main(String[] args) throws Exception
    {
        String pParam = " ";
        String nParam = " ";

        pParam = args[0];
        nParam = args[1];

        File pathFile = new File(pParam);
        File fileName = new File(nParam);

        String path = pathFile.getPath();
        String name = fileName.getName();

        if (pathFile.exists() && pathFile.isDirectory())
        {
            System.out.println(path + " exists!");
            System.out.println("Searching...");
            System.out.println("-------------------------------------------->");
        } else {
            System.out.println("Path " + path + " does NOT exist!");
        }
        File[] contents = pathFile.listFiles();
        
        for(int i = 0; i < contents.length; i++)
        {
          if(contents[i].isDirectory())
          {
              File[] sub = contents[i].listFiles();
              System.out.println(sub[i].getName());
          }
        }


    }
     
}






Was This Post Helpful? 0
  • +
  • -

#4 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 90
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: File Class

Posted 08 April 2011 - 03:48 PM

Well, that's because when you iterate over the files, and then stumble across a directory, you don't start off the index at 0. Instead, you use the first for loops index number. For instance, could have 500 files in a directory, and on the 250th one, come across a directory, which you would call listFiles(), and then try to get the 250th file in that sub directories name. that sub directory might not have 250 files in it.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: File Class

Posted 08 April 2011 - 06:11 PM

this is kind of ackward

pParam = args[0];
nParam = args[1];


You save the first argument into nParams
then just erase that saved value with the second arfument
Was This Post Helpful? 0
  • +
  • -

#6 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 90
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: File Class

Posted 08 April 2011 - 06:23 PM

one of them is a p, the other an n (P-N).

He needs to rethink how he's writing his search function.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1