I wrote the following method with Java which download a file for me, from the Server to my local computer.
public void downloadcsv() {
String server = "servername.host";
int port = 21;
String user = "username";
String pass = "password";
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
String remoteFile = "/serverpath/daten.csv";
File downloadFile = new File("localpath/daten.csv");
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));
boolean success = ftpClient.retrieveFile(remoteFile, outputStream);
outputStream.close();
if (success) {
System.out.println("File has been downloaded successfully.");
}
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
So far so good. Now, I want to delete the content of the file on the server.
Just the content, because I need the file to write in it with another program.
Is there a fast way to do that, while or after the download?
Now, I just delete the file with another method and create a new file.
But that seems to be inefficient. Thanks for help.