2014-09-04 1002 views
5

我想用Java來逐行讀取一個文件,這很簡單(在stackoverflow.com上有多個解決方案),但是這裏需要注意的是該文件位於遠程服務器上,是不可能獲得本地副本(這是在單個.txt文件中的數百萬亞馬遜評論的大量集合)。如何使用Java JSch庫逐行讀取遠程文件?

JSch附帶兩個將文件複製到遠程主機或從遠程主機複製文件的示例類,即ScpTo和ScpFrom。我對從遠程主機逐行讀取文件感興趣; ScpFrom會嘗試將整個事件複製到本地文件中,這需要很長時間。

這裏是ScpFrom鏈接:http://www.jcraft.com/jsch/examples/ScpFrom.java.html

我想嘗試貨物邪教代碼那裏,然後修改它以逐行讀取遠程文件行而不是寫入到本地文件,但大部分代碼一旦作者聲明瞭一個字節數組並開始從遠程文件中讀取字節,就是希臘語。我承認這是我幾乎不瞭解的東西; BufferedReader提供了一個更高層次的接口。本質上我想這樣做:How to read a large text file line by line using Java?

除了使用一個BufferReader,也可以逐行讀取遠程文件,如果提供的主機名和用戶憑證(密碼等),即RemoteBufferReader?

這是我寫的測試代碼;我如何使用JSCh逐行讀取遠程文件?

public class test2 
{ 
    static String user = "myusername"; 
    static String host = "[email protected]"; 
    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 

    } 
} 
+0

我會使用'cat'(在遠程主機上)而不是'scp'。然後只需讀取命令行的標準輸出。 – Rob 2014-09-04 05:10:39

+1

你可以使用jsch通過sftp打開一個輸入流,然後逐行讀取 – nablex 2014-09-04 06:13:55

回答

13

要通過ssh處理文件,最好使用sftp而不是scp或純ssh。 Jsch內置了對sftp的支持。一旦你開了一個會議,這樣做是爲了打開一個SFTP通道:

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

一旦你開了一個SFTP通道,there are methods閱讀這讓你的遠程文件訪問該文件的內容作爲一個InputStream

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

完美,這正是我所需要的。謝謝! – user3898238 2014-09-05 22:29:31

+0

這是標準的sftp功能。您不需要使用「exec」通道運行命令來讀取/傳輸文件。 – 2014-09-15 10:53:38

+0

我試圖打開遠程Windows計算機的sftp並獲取內容,但是我收到了filenotfound異常。該文件與指定的路徑一起存在。有沒有其他人認爲我需要做的Windows? InputStream stream = channel.get(「C:\\ 1.xml」); – artos 2015-07-28 09:23:55

0

JSch庫是可以用來讀取SFTP服務器文件的功能強大的庫:如果你需要讀取行由行,你可以轉換爲Reader。下面是測試代碼一行地讀SFTP位置線文件

 JSch jsch = new JSch(); 
     Session session = null; 
     try { 
      session = jsch.getSession("user", "127.0.0.1", 22); 
      session.setConfig("StrictHostKeyChecking", "no"); 
      session.setPassword("password"); 
      session.connect(); 

      Channel channel = session.openChannel("sftp"); 
      channel.connect(); 
      ChannelSftp sftpChannel = (ChannelSftp) channel; 

      InputStream stream = sftpChannel.get("/usr/home/testfile.txt"); 
      try { 
       BufferedReader br = new BufferedReader(new InputStreamReader(stream)); 
       String line; 
       while ((line = br.readLine()) != null) { 
        System.out.println(line); 
       } 

      } catch (IOException io) { 
       System.out.println("Exception occurred during reading file from SFTP server due to " + io.getMessage()); 
       io.getMessage(); 

      } catch (Exception e) { 
       System.out.println("Exception occurred during reading file from SFTP server due to " + e.getMessage()); 
       e.getMessage(); 

      } 

      sftpChannel.exit(); 
      session.disconnect(); 
     } catch (JSchException e) { 
      e.printStackTrace(); 
     } catch (SftpException e) { 
      e.printStackTrace(); 
     } 

請參考blog的整個程序。