如何使用Java JSch库逐行读取远程文件? [英] How to use Java JSch library to read remote file line by line?

查看:296
本文介绍了如何使用Java JSch库逐行读取远程文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java逐行读取文件,这非常简单(在stackoverflow.com上有多个解决方案),但需要注意的是该文件位于远程服务器上,并且它不可能获得本地副本(它是单个.txt文件中数百万亚马逊评论的大量集合)。

I am trying to use Java to read a file line by line, which is very simple (there are multiple solutions for this on stackoverflow.com), but the caveat here is that the file is located on a remote server, and it is not possible to get a local copy (it is a massive collection of millions of Amazon reviews in a single .txt file).

JSch附带两个示例类,用于将文件复制到远程主机或从远程主机复制文件,即ScpTo和ScpFrom。我有兴趣逐行从远程主机读取文件; ScpFrom会尝试将整个内容复制到本地文件中,这需要很长时间。

JSch comes with two example classes that copy files to and from remote hosts, namely ScpTo and ScpFrom. I am interested in reading the file from the remote host line by line; ScpFrom would try to copy the whole thing into a local file, which would take ages.

这是ScpFrom的链接: http://www.jcraft.com/jsch/examples/ScpFrom.java.html

Here is a link to ScpFrom: http://www.jcraft.com/jsch/examples/ScpFrom.java.html

我会尝试货运代码然后修改它以逐行读取远程文件而不是写入本地文件,但是一旦作者声明了一个字节数组并开始从远程文件中读取字节,大部分代码都是希腊语。我承认这是我几乎不了解的事情; BufferedReader提供了更高级别的接口。基本上我想这样做:如何使用Java逐行读取大文本文件?

I would try to cargo cult the code there and then modify it to read a remote file line by line rather than write to a local file, but most of the code is Greek to me once the author declares a byte array and starts reading bytes from the remote file. I'll admit this is something I have almost no understanding of; BufferedReader provides a much higher level interface. Essentially I want to do this: How to read a large text file line by line using Java?

除了使用BufferReader,它还可以逐行读取远程文件,如果提供了主机名和用户凭证(密码等),即RemoteBufferReader?

except using a BufferReader that can also read remote files line by line, if provided the host name and user credentials (password, etc.), i.e. RemoteBufferReader?

这是我写的测试代码;如何使用JSCh逐行读取远程文件?

This is the test code I've written; how do I read in the remote file line by line using JSCh?

public class test2
 {
    static String user = "myusername";
    static String host = "user@remotehost";
    static String password = "mypasswd";
    static String rfile = "/path/to/remote/file/on/remote/host";
    public static void main(String[] args) throws FileNotFoundException, IOException, JSchException
    {
        JSch jsch=new JSch();
        Session session=jsch.getSession(user, host, 22);
        session.setPassword(password);
        session.connect();
        // exec 'scp -f rfile' remotely
        String command="scp -f "+rfile;
        Channel channel=session.openChannel("exec");
        ((ChannelExec)channel).setCommand(command);

        // get I/O streams for remote scp
        OutputStream out=channel.getOutputStream();
        channel.connect()
        //no idea what to do next

    }
 }


推荐答案

要通过ssh操作文件,最好使用sftp而不是scp或纯ssh。 Jsch内置了对sftp的支持。打开会话后,执行此操作以打开sftp频道:

To manipulate files through ssh, you're better off using sftp than scp or pure ssh. Jsch has built-in support for sftp. Once you've opened a session, do this to open an sftp channel:

ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");

打开sftp频道后,有方法读取远程文件让你以 InputStream 的形式访问文件的内容。如果您需要逐行阅读,可以将其转换为 Reader

Once you've opened an sftp channel, there are methods to read a remote file which let you access the file's content as an InputStream. You can convert that to a Reader if you need to read line-by-line:

InputStream stream = sftp.get("/some/file");
try {
    BufferedReader br = new BufferedReader(new InputStreamReader(stream));
    // read from br
} finally {
    stream.close();
}

使用尝试使用资源语法,您的代码可能看起来更像这样:

Using try with resources syntax, your code might look more like this:

try (InputStream is = sftp.get("/some/file");
     InputStreamReader isr = new InputStreamReader(stream);
     BufferedReader br = new BufferedReader(isr)) {
    // read from br
}

这篇关于如何使用Java JSch库逐行读取远程文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆