Results 1 to 4 of 4
  1. #1
    vasouli is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default How to compare an arrayList<String> with an arrayList<File> in Java?

    I have the following code:
    Java 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);
    			}
    		}
    		
    		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]);
    		    }
    		}
    		
    		System.out.println("\n");
    		System.out.println(retainedFiles);
    		System.out.println("\n");
    I want to compare the two ArrayLists created using
    Java Code:
    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.
    Java Code:
    RetainAll();
    does not work because these are two different types:
    Java Code:
    String
    and
    Java Code:
    File
    . How can I do this comparison? 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.Reading through posts, I noticed that a FilenameFilter may be needed. But I don't know how to implement such thing.

    Sorry for the long and confusing post and thank you!
    Last edited by vasouli; 10-11-2013 at 04:59 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default Re: How to compare an arrayList<String> with an arrayList<File> in Java?

    You have to write the comparison yourself. What does it mean to compare a String to a File? Are you trying to compare the file name to the String? Should the file contain the String? Something else? Java can't read your mind, so you have to tell it exactly what you want it to do.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    vasouli is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default Re: How to compare an arrayList<String> with an arrayList<File> in Java?

    I want to read the array with the strings which contains filenames and then in the files arrays to delete all the other files except those there, that their filenames are in the string array. That's the comparison and I don't know how to do this!

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default Re: How to compare an arrayList<String> with an arrayList<File> in Java?

    Can you loop through the list of Files and print out their file names?
    Can you compare Strings?
    Can you remove stuff from a list?
    Can you use if statements?

    If so, all you have to do is combine those steps.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 0
    Last Post: 09-21-2013, 12:28 AM
  2. Replies: 12
    Last Post: 07-07-2011, 09:49 PM
  3. Replies: 12
    Last Post: 11-26-2010, 09:33 AM
  4. Replies: 7
    Last Post: 11-22-2010, 02:26 PM
  5. Replies: 2
    Last Post: 04-21-2008, 12:43 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •