Following is my code to read the file and rename it afterwards. Im using apache commons.net 3.0.1
.
client.connect(localhost);
boolean login = client.login("username", "password");
if(login){
System.out.println("login successful");
boolean chdir = client.changeWorkingDirectory("/home/folder1/child/");
String url = client.printWorkingDirectory(); // EDIT
FTPFile[] result = client.listFiles(url, filter);
if (result != null && result.length > 0) {
for (FTPFile aFile : result) {
try{
String filename = aFile.getName();
InputStream is= client.retrieveFileStream(filename);
br = new BufferedReader(new InputStreamReader(is));
while((line = br.readLine()) != null){
System.out.println("the line is"+line);
}
}
finally{
if(br!=null){
try{
br.close();
String oldFilename =url + "/" +aFile.getName();
String newFilename = "PRO"+aFile.getName();
boolean rename = client.rename(oldFilename, newFilename);
if(rename){
System.out.println("renamed");
}
else{
System.out.println("Error in renaming");
}
}
The file deosn't get renamed and the program prints
error in renaming files (cz boolean rename = false).
I have refereed to different examples. But all seems to show the same problem. The file is picked after filter and read without any issues.
If anyone could point to what I'm doing wrong here, that'd be very helpful.
Here, the url
is String url = client.printWorkingDirectory();
I have tried with both relative path and absolute path. And giving full path only to the oldFilename
and just the filname to the newFilename
. Both did not work.
EDIT
Before changing the directory, the url will be /
which is root.
After changing the directory, the url will be /home/folder1/child/
. This is the where the files exists.