Java的。从FTP读取文件,但不要下载整个 [英] Java. Read file from FTP but DON'T download it whole

查看:241
本文介绍了Java的。从FTP读取文件,但不要下载整个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



由于这些文件可能非常大,我不需要下载它们。 b
$ b

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

解决方案

<只读只有第一行,忽略剩余和关闭流。一个聪明的FTP客户端在提供任何内容之前不会缓冲内存中的整个流。



假设您正在使用 Apache Commons Net FTPClient

 的BufferedReader读者=空; 
字符串firstLine = null;

尝试{
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);


I need to read CSV file header from FTP.

As these files can be very huge, I don't 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);

这篇关于Java的。从FTP读取文件,但不要下载整个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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