1

I am trying to read XML files from a Folder. After I read the files I am extracting an element and write that in a text file.

The files Name Looks like that

zey_3_20161020-092152_JK6_NO.xml
zey_57_20161020-092152_A_K6.xml
zey_256_20161020-092152_B_A.xml
zey_1000_20161020-092152_B_A.xml

But when I am reading the files I dont get the right order. I am getting the order like this :

zey_1000_20161020-092152_B_A.xml
zey_256_20161020-092152_B_A.xml
zey_3_20161020-092152_JK6_NO.xml
zey_57_20161020-092152_A_K6.xml

Here is my code:

private String rgId;
private NodeList rgIdList;
ArrayList<String> list = new ArrayList<String>();
ArrayList<String> counter = new ArrayList<String>();
String splitFile = null;

public ReadXML() throws ParserConfigurationException, SAXException
{
    try
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        File folder = new File("C:/...");
        File[] listFiles = folder.listFiles();

        for (File file : listFiles) {
            if (file.isFile()) {                
                System.out.println(file.getName());

                Document document = builder.parse(file);
                rgIdList = document.getElementsByTagName("id");

                if (rgIdList.getLength() > 0) {
                    rgId = rgIdList.item(0).getTextContent().toString();
                    list.add( rgId);
                }
            }
        }

        FileWriter fw = new FileWriter("C:/....");

        for (String str : list) {
            fw.write(str + "\r\n");
        }
        fw.close();

        System.out.println(list);
    } catch (IOException e) {
        System.out.println(e);
    }
}

public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException {

    new ReadXML();
}

Can anyone help how to sort the files in the right order? Thank you in advance

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
kuebra
  • 35
  • 1
  • 7
  • can you describe the order? is it alphabetical? – Martin Frank Oct 24 '16 at 11:37
  • 1
    What is the "right" order? Do you want list first the older files? The bigger files? Anyway you should sort the result list. See: http://stackoverflow.com/questions/7199911/how-to-file-listfiles-in-alphabetical-order – Marco A. Hernandez Oct 24 '16 at 11:38
  • Get list of XML files from input directory and sort it. It would be natural order of string as filename is string. Then iterate over List and load each file using new File with combination directory and filename as path. – Swaraj Oct 24 '16 at 11:38
  • The order is numeric.For example zey_3_20161020-092152_JK6_NO.xml The second part after the first underscore is the sort part like the 3 – kuebra Oct 24 '16 at 11:39

2 Answers2

1

Your problem is related to the fact that you rely on the order of the result of File#listFiles() while the order is not guaranteed as stated in the javadoc:

There is no guarantee that the name strings in the resulting array will appear in any specific order; they are not, in particular, guaranteed to appear in alphabetical order.

If you want a specific order you should sort the files yourself using Arrays.sort(T[] a, Comparator<? super T> c).

File[] listFiles = folder.listFiles();
Arrays.sort(listFiles, myComparator);
for (File file : listFiles) {
    ...
}
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
0

when you get your files you can simply order them using the sort methode from Arrays

File[] listFiles = folder.listFiles();
Arrays.sort(listFiles , new Comparator<String>()
  {

     @Override
     public int compare (String o1, String o2)
     {
        return o1.compareTo(o2); //or whatever sorting algorthim is desired
     }

  });

if you want to use the implemented compare from String you don't need an extra comparator:

File[] listFiles = folder.listFiles();
Arrays.sort(listFiles);
Martin Frank
  • 3,445
  • 1
  • 27
  • 47