0

I am trying to read a file on a remote machine using org.apache.commons.vfs2. I am able to locate the file on that machine and store it into the FileObject class but I can't seem to find a way to read the contents of the file using that FileObject.

I read it on the home.apache.org that the getContent() method returns the file's contents. However, I am not getting back the expected results from it.

Below is my code:

import org.apache.commons.vfs2.*;
import org.apache.commons.vfs2.auth.StaticUserAuthenticator;
import org.apache.commons.vfs2.impl.DefaultFileSystemConfigBuilder;

public class ReadGridConfig {

    public static void RemoteGrid() throws IOException {
        String domain="#############.com";
        String userName="######";
        String password="#########";
        String remoteFilePath = "\\\\##.#.##.###\\C$\\Grid\\node_5555.json";


        //domain, username, password
        UserAuthenticator auth=new StaticUserAuthenticator(domain, userName, password);
        FileSystemOptions opts=new FileSystemOptions();
        DefaultFileSystemConfigBuilder.getInstance().setUserAuthenticator(opts, auth);

        FileObject fo=VFS.getManager().resolveFile(remoteFilePath,opts);

        System.out.println(fo.exists());
        System.out.println(fo.getContent());

        fo.close();

    }
}

These are the results I got back on the console:

Picked up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=true
Connected to the target VM, address: '###.#.#.#:#####', transport: 'socket'
OpenJDK 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended
11:24:06.760 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Using "C:\Users\H344195\AppData\Local\Temp\vfs_cache" as temporary files store.
11:24:06.839 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.tar.TarFileProvider" because required class "org.apache.commons.compress.archivers.tar.TarArchiveOutputStream" is not available.
11:24:06.840 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.bzip2.Bzip2FileProvider" because required class "org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream" is not available.
11:24:06.846 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.ftp.FtpFileProvider" because required class "org.apache.commons.net.ftp.FTPFile" is not available.
11:24:06.846 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.ftps.FtpsFileProvider" because required class "org.apache.commons.net.ftp.FTPFile" is not available.
11:24:06.847 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.http.HttpFileProvider" because required class "org.apache.commons.httpclient.HttpClient" is not available.
11:24:06.847 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.https.HttpsFileProvider" because required class "org.apache.commons.httpclient.HttpClient" is not available.
11:24:06.847 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.http4.Http4FileProvider" because required class "org.apache.http.client.HttpClient" is not available.
11:24:06.848 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.http4s.Http4sFileProvider" because required class "org.apache.http.client.HttpClient" is not available.
11:24:06.849 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.sftp.SftpFileProvider" because required class "com.jcraft.jsch.JSch" is not available.
11:24:06.850 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.webdav.WebdavFileProvider" because required class "org.apache.commons.httpclient.HttpClient" is not available.
11:24:06.850 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.tar.TarFileProvider" because required scheme "tar" is not available.
11:24:06.850 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.tar.TarFileProvider" because required scheme "bz2" is not available.
11:24:06.851 [main] DEBUG org.apache.commons.vfs2.impl.StandardFileSystemManager - Skipping provider "org.apache.commons.vfs2.provider.hdfs.HdfsFileProvider" because required class "org.apache.hadoop.fs.FileSystem" is not available.
11:24:06.874 [main] DEBUG org.apache.commons.vfs2.cache.SoftRefFilesCache - putFile: file:////##.#.##.###/C$/Grid/node_5555.json
true
org.apache.commons.vfs2.provider.DefaultFileContent@5c909414

System.out.println(fo.exists()); came back as true. So I know that I am able to locate the file.

But for System.out.println(fo.getContent()); I was expecting to get back the contents of the .json file, but instead I got org.apache.commons.vfs2.provider.DefaultFileContent@5c909414 which does not match the actual contents of the file.

Here is the screenshot of the fo FileObject during debugging.

Ritzies
  • 33
  • 1
  • 7
  • 1
    `FileObject#getContent()` returns a [`FileContent`](https://home.apache.org/~elserj/commons/commons-vfs-2.1/apidocs/org/apache/commons/vfs2/FileContent.html) object, as documented. From there, I assume you would want to use [`FileContent#getInputStream()`](https://home.apache.org/~elserj/commons/commons-vfs-2.1/apidocs/org/apache/commons/vfs2/FileContent.html#getInputStream()) which gives you an `InputStream`. Then do what you need to do in order to convert the bytes into a JSON string. – Slaw Sep 04 '19 at 16:02
  • @Slaw That worked perfectly. Thank you so much! – Ritzies Sep 04 '19 at 16:28

1 Answers1

1

as @Slaw said you need to get the InputStream from the File content and then read the bytes from InputStream.

try {
    FileSystemManager fileSystemManager = VFS.getManager();
    FileObject fileToread = fileSystemManager.resolveFile("<file-path>");
    BufferedInputStream inputStreamReader = new BufferedInputStream(fileToread.getContent().getInputStream());
    String content = "";
    while (inputStreamReader.available() > 0) {
        char c = (char) inputStreamReader.read();
        content = content.concat(String.valueOf(c));
    }
    //do something from content
} catch (FileSystemException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
ThilankaD
  • 1,021
  • 1
  • 13
  • 24