Skip to content
Advertisement

Can you access files on the remote FTP server with RandomAccessFile?

I’m trying to read the file on the FTP remote-server using Apache Commons Net library.

retrieveFileStream returns InputStream and put it in BufferedReader.

But, I want to use RandomAccessFile (to use the seek() method).

I want to get Inputstream as a RandomAccessFile.

Is it possible?

FTPClient ftp = new FTPClient();
InputStream in = ftp.retrieveFileStream(remote_file_name);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in, "UTF-8"));

Advertisement

Answer

If you want to starting reading the remote file from a certain offset, use FTPClient.setRestartOffset. The FTP server needs to support REST command (most do).

ftp.setRestartOffset(offset);
InputStream in = ftp.retrieveFileStream(remote_file_name);
// Now you can read as many bytes as you need from 'in'

If you do not want to read the file to the end, and you want to reuse the connection for other operations, you need to call FTPClient.abort.


(You cannot convert InputStream to RandomAccessFile, nor use RandomAccessFile anyhow with files on FTP server)

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement