我正在使用 java Path 类为客户端-服务器系统编写目录列表代码。我为它创建了一个自定义类,效果很好。但是,我想在客户端而不是服务器上显示结果,我无法弄清楚如何。我对Java相当陌生,所以如果这是一个愚蠢的问题,请原谅。
将对象文件对象返回给客户端并将其打印出来之类的操作会起作用吗?因为我相信目录结构被存储在这个对象中?
我通过 RMI 执行此操作,以下是实现代码:
//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();
}
这是客户类:
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;
}
}