Java Path クラスを使用して、クライアント サーバー システムのディレクトリ リスト コードを作成しています。うまく機能するカスタムクラスを作成しました。ただし、結果をサーバーではなくクライアントに表示したいのですが、方法がわかりません。私はJavaにかなり慣れていないので、これがばかげた質問であればすみません。
オブジェクトfileobjをクライアントに返し、そこに出力するようなことはできますか? ディレクトリ構造がこのオブジェクトに格納されていると思いますか?
私は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;
}
}