Java的.从FTP读取文件但不要整体下载

Ale*_*xey 5 java ftp file stream

我需要从FTP读取CSV文件头.

由于这些文件非常庞大,我不需要下载它们.

有没有办法从FTP读取第一行CSV文件并中止连接?

Bal*_*usC 13

只读第一行,忽略残余并关闭流.在提供任何要读取的内容之前,智能FTP客户端不会将整个流缓冲在内存中.

假设您正在使用Apache Commons Net FTPClient:

BufferedReader reader = null;
String firstLine = null;

try {
    InputStream stream = ftpClient.retrieveFileStream(ftpFile.getName());
    reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
    firstLine = reader.readLine();
} finally {
    if (reader != null) try { reader.close(); } catch (IOException logOrIgnore) {}
}

doYourThingWith(firstLine);
Run Code Online (Sandbox Code Playgroud)