开发者

Java. Read file from FTP but DON'T download it whole

开发者 https://www.devze.com 2022-12-30 02:22 出处:网络
I need to read CSV file header from FTP. As these files can be very huge, I don\'t开发者_如何学Go need to download them.

I need to read CSV file header from FTP.

As these files can be very huge, I don't开发者_如何学Go need to download them.

Is there a way to read first line of CSV file from FTP and abort connection?


Just read only the first line, ignore the remnant and close the stream. A smart FTP client won't buffer the entire stream in memory before providing anything for read.

Assuming you're using 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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号