0

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!

Vasouli
  • 47
  • 1
  • 8

2 Answers2

1

You could do it at least in two ways, using File.getName()

  1. simply iterate by your lists and compare your String name with invoking getName() on your File element from the second list.
  2. Or make another list from your File names, resulting in a another String list, and then use retainAll()
Eel Lee
  • 3,513
  • 2
  • 31
  • 49
  • Maybe I didn't express my question correctly. I think this answers the opposite. I mean by doing as above, with `retainAll()` will get the strings, not the files. But I need to get the files with the `retainAll()`. Thank you, anyway! – Vasouli Oct 10 '13 at 11:47
1

First, you code won't work at all given its current structure. File_name won't exist when you call files.retainAll(File_name); Also, it's bad practice to start variables with a capital letter. That said, you could do the following once that's fixed, putting all the retained files in a new ArrayList:

ArrayList<File> retainedFiles = new ArrayList<File>();
for (int i = 0; i < listOfFiles.length; i++) {
    if (listOfFiles[i].isFile())  {
        files = listOfFiles[i].getName();
        if (File_name.contains(files))
            retainedFiles.add(listOfFiles[i]);
    }
}

(edited to reflect below comment)

tom
  • 2,704
  • 16
  • 28
  • Ok I corrected the File_name in order to exist outside the try/catch. But the above code, I think it gives you the names of the files, but I want the have the actual files. For example I have a directory which consists 21 files and I put them in the array files and there are three filenames of these files as strings in the array File_name. What I want to do is from these files in the files array to have only these three that their filenames are in the File_name array as actual files. Sorry for my confusing message – Vasouli Oct 10 '13 at 17:47
  • Ok, I've updated the example to use a ArrayList Does that help? – tom Oct 10 '13 at 18:16
  • I've tried it but it only prints an empty array [ ]. Reading through posts, I noticed that a FilenameFilter may be needed. But I don't know how to implement such thing. Any help would be welcome. Thank you, anyway!!! – Vasouli Oct 10 '13 at 18:53
  • Given the errors in the code you posted, its probably your new code thats broken. Post up your revised code. – tom Oct 10 '13 at 19:17
  • I've edited my first post and posted my revised code there – Vasouli Oct 10 '13 at 19:45