I have this working method which lists files from a ftp server, but I want to modify it to use it to download only csv files from the server. Here's the method:
private static void printFileDetails(FTPFile[] files) {
DateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (FTPFile file : files) {
String details = file.getName();
if (file.isDirectory()) {
details = "[" + details + "]";
}
details += "\t\t" + file.getSize();
details += "\t\t" + dateFormater.format(file.getTimestamp().getTime());
System.out.println(details);
}
}
This is what I tried:
private static ArrayList<String[]> CsvFiles(FTPFile[] files)
{
// get list of files on given path
for (FTPFile file : files) {
String details = file.getName();
//String[] CsvFiles;
ArrayList<String> CsvFiles = new ArrayList<>();
CsvFiles.add(details);
}
return CsvFiles[];
}
At the return CsvFiles[]; netbeans is showing '.class' expected error, but that's not the only problem, I'm not even sure if the whole method is correct. Any help would be much appreciated.
we can use FTPFileFilter to filter files that we want to list
FTPFileFilter filter=new FTPFileFilter(){
@Override
public boolean accept(FTPFile ftpFile) {
return (ftpFile.isFile() && ftpFile.getName().endsWith(".csv"));
}
};
FTPFile[] files=sessionForFtp.Files("directoryName", filter);
Do a details.endswith(".csv") on the file name.
(NOTE!! Un-tested code)
if(details.endsWith(".csv")) {
CsvFiles.add(details);
}
With your code (and some clean up):
private static ArrayList<String> CsvFiles(FTPFile[] files)
{
ArrayList<String> CsvFiles = new ArrayList<>();
// get list of files on given path
for (FTPFile file : files)
{
if(file.getName().endsWith(".csv")) {
CsvFiles.add(details);
}
}
return CsvFiles;
}
Your code looks like a puzzle.
ArrayList<String>
instead of ArrayList<String[]>
. Smth like this:
private static ArrayList<String> getCsvFiles(FTPFile[] files){
ArrayList<String> csvFiles = new ArrayList<>();
for (FTPFile file : files) {
String details = file.getName();
//if file is csv
csvFiles.add(details);
}
return csvFiles;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.