我有以下代码:
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);
}
}
我想比较ArrayLists
使用创建的两个files.retainAll(File_name);
文件,因为我需要保留第二个列表中的文件,它们的名称作为字符串存在于第一个列表中。RetainAll();
不起作用,因为这是两种不同的类型:String
和File
. 我正在尝试制作如下所示的过滤器,但出现语法错误,当我尝试纠正它们时似乎不起作用。这是过滤器:
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);
}
});
我在第一行和最后一行收到错误插入或删除标记。我不在retainAll()
这个过滤器中使用。对不起,很长的帖子,谢谢!