FileVisitor using Path class - how to 'return' the directory structure?

182 Views Asked by At

I am writing a directory listing code for a client-server system using java Path class. I created a custom class for that which works fine. However, I want to display the results on Client rather than Server, which I am not able to figure out how. I am fairly new to Java so please excuse me if this is a silly question.

Will doing something like returning object fileobj to client and printing it out there work? because I believe the directory structure is being stored in this object?

I am doing this through RMI and following is the Implementation code:

//this method is called from Client and is getting executed at Server side.
           public void DoDir(String dirpath) 
           {
           Path fileobj = Paths.get(dirpath);
           DirListing visitor = new DirListing();
          try{
            Files.walkFileTree(fileobj, visitor);
           //Just a thought:will returning fileobj and printing it on Client work?
           //If yes, how do I override toString here? 
     }
     catch(IOException e){
e.printStackTrace();
     }

This is the custome class:

class DirListing extends SimpleFileVisitor<Path> 
{

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
        throws IOException 
        {
            System.out.println("Just visited " + dir);
            return FileVisitResult.CONTINUE;
        }

@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
        throws IOException 
        {
            System.out.println("About to visit " + dir);
            return FileVisitResult.CONTINUE;
        }

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
        throws IOException 
        {
            if (attrs.isRegularFile()) 
            {
                System.out.print("Regular File: ");
            }
            System.out.println(file);
            return FileVisitResult.CONTINUE;
        }

@Override
public FileVisitResult visitFileFailed(Path file, IOException exc)
        throws IOException 
        {
            System.err.println(exc.getMessage());
            return FileVisitResult.CONTINUE;
        } 

}

0

There are 0 best solutions below