I have the following code:
ArrayList<String> File_name = new ArrayList<String>();
try {
//Reading an XML file, created above
File fXmlFile = new File("C:\\Users\\V\\Documents\\diplwmatiki\\temp\\dokimi.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
//optional, but recommended
//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();
//Printing the attributes of the XML file
System.out.println("\nRoot element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("element");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("element : " + eElement.getAttribute("id"));
System.out.println("File_name : " + eElement.getElementsByTagName("name").item(0).getTextContent());
File_name.add(eElement.getElementsByTagName("name").item(0).getTextContent());
}
}
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println("\n");
System.out.println(File_name);
System.out.println("\n");
//Creating an list having all files of a directory
String path = "C:\\Users\\V\\Documents\\diplwmatiki\\SFBs";
String files;
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++)
{
if (listOfFiles[i].isFile())
{
files = listOfFiles[i].getName();
System.out.println(files);
}
}
I want to compare the two ArrayLists
created using files.retainAll(File_name);
, because I need to keep the files from the second list which their names exist in the first list as strings. RetainAll();
does not work because these are two different types: String
and File
. I am trying to make the filter as shown below, but I get syntax errors which when I try to correct them don't seem to work. Here's the filter:
String[] myFiles = folder.list(new FilenameFilter(){
for (int i=0; i < listOfFiles.length; i++){
public boolean accept;File folder; String fileName;{
return fileName.startsWith(File_name[i]);
System.out.println(folder);
}
});
I get the errors insert or delete tokens at the first and the last line. I don't use retainAll()
in this filter.
Sorry for the long post and thank you!