If you want to read a file from an SFTP server in Java, especially when the filename includes today's date, the JSch library is a reliable tool. Here's how to connect, read, and print file contents, with or without a date in the filename.
✅ Example 1: Basic File Read from SFTP
java
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
public class SFTPReadExample {
public static void main(String[] args) {
String host = "sftp.example.com";
String user = "username";
String password = "password";
int port = 22;
String remoteFilePath = "/path/to/remote/file.txt";
Session session = null;
ChannelSftp channelSftp = null;
try {
JSch jsch = new JSch();
session = jsch.getSession(user, host, port);
session.setPassword(password);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
InputStream inputStream = channelSftp.get(remoteFilePath);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (channelSftp != null && channelSftp.isConnected()) channelSftp.disconnect();
if (session != null && session.isConnected()) session.disconnect();
}
}
}
✅ Example 2: Read SFTP File with Today’s Date in Filename
java
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
public class SFTPReadWithDateExample {
public static void main(String[] args) {
String host = "sftp.example.com";
String user = "username";
String password = "password";
int port = 22;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String todayDate = dateFormat.format(new Date());
String remoteFilePath = "/path/to/remote/file_" + todayDate + ".txt";
Session session = null;
ChannelSftp channelSftp = null;
try {
JSch jsch = new JSch();
session = jsch.getSession(user, host, port);
session.setPassword(password);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
InputStream inputStream = channelSftp.get(remoteFilePath);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (channelSftp != null && channelSftp.isConnected()) channelSftp.disconnect();
if (session != null && session.isConnected()) session.disconnect();
}
}
}
🧠 Key Concepts:
channelSftp.get(remoteFilePath)
– Retrieves the file as an InputStream
.SimpleDateFormat
– Used to dynamically generate filenames based on today’s date.BufferedReader
– Efficiently reads file line by line.- Dynamic Paths – Great for daily logs or timestamped reports.
⚠️ Tips:
- Ensure the file path and date format match your SFTP naming convention.
- Add a check for
FileNotFoundException
in case the file for today's date doesn’t exist. - For large files, consider processing the stream instead of printing.