Output to appear in ascending order

Could someone please help me to get the contents of my output file in ascending order.

I am reading a filemnf.xml kept inside myjar, the contents of it are

<assets>

</assets>

I am writing the contents of the filemnf.xml to an output file, and am also adding thepdf filenames found in the directory.

The contents of the output file after running the program are

<assets>

<pdf_files name="assets/123_001_ch_03.pdf"/>

<pdf_files name="assets/123_001_ch_02.pdf"/>

<pdf_files name="assets/123_001_ch_01.pdf"/>

</assets>

But I need my output to be in the following order

<assets>

<pdf_files name="assets/123_001_ch_01.pdf"/>

<pdf_files name="assets/123_001_ch_02.pdf"/>

<pdf_files name="assets/123_001_ch_03.pdf"/>

</assets>

My code is

void fileMNF(File RootDir, String jTextField1Text)

{

String filename=jTextField1Text.substring(0,jTextField1Text.lastIndexOf("."))+"_mnf.xml";

String Assetsdir=RootDir+"\\"+"assets";

File files=new File(Assetsdir);

String[] lists=files.list();

try

{

int val;

char ch;

InputStream is=getClass().getResourceAsStream("/Inputs/mnf.xml");

File dstfile=File.createTempFile("mnf",".xml");

dstfile.deleteOnExit();

FileOutputStream fos =new FileOutputStream(dstfile);

int b;

while((b = is.read()) != -1)

{

fos.write(b);

}

fos.close();

File inputfile=new File(dstfile.getAbsolutePath());

File outputfile=new File(filename);

BufferedReader in =new BufferedReader(new FileReader(inputfile));

BufferedWriter out =new BufferedWriter(new FileWriter(outputfile));

String line;

while ((line=in.readLine()) !=null)

{

if(line.startsWith("<assets>"))

{

for(int i=0;i<lists.length;i++)

{

if(lists[i].endsWith(".pdf"))

{

line = PDFLineInsert(line,lists[i]);

}

}

}

out.write(line);

out.newLine();

out.flush();

}

in.close();

out.close();

}

catch(Exception e)

{

System.out.println(e.getMessage());

}

}

private String PDFLineInsert(String line, String PDF)

{

String res ="";

res = line.replace("<assets>","<assets>\n\r<pdf_files name=\"assets/"+PDF+"\"/>");

return res;

}

[4289 byte] By [Simmya] at [2007-11-26 19:09:31]
# 1
Sort the list before you iterate.Kaj
kajbja at 2007-7-9 21:04:27 > top of Java-index,Java Essentials,New To Java...
# 2
You can implement a Comparator which compares only digits at the end of your file names and put all the file names in a List. The use Collections.sort(List list, Comparator comp) to sort them according to the digits at the end. Please check java.util.Comparator
Kiran_Joisa at 2007-7-9 21:04:27 > top of Java-index,Java Essentials,New To Java...
# 3

Can you please tell me what updations to make in my code

//System.out.println("before:"+lists);//The order of appearance is ascending

line = PDFLineInsert(line,lists);

//System.out.println("after:"+lists);//The order of appearance is ascending

Message was edited by:

Simmy

Message was edited by:

Simmy

Simmya at 2007-7-9 21:04:27 > top of Java-index,Java Essentials,New To Java...
# 4
> Can you please tell me what updations to make in my> codeRead all about it here: http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html
prometheuzza at 2007-7-9 21:04:28 > top of Java-index,Java Essentials,New To Java...
# 5

Your PDFLineInsert method is inserting your lines at the beginning of the file.

You should change your logic to write the new lines as you get them. something like this:

while ((line=in.readLine()) != null)

{

if(line.startsWith("<assets>"))

{

out.write(line);

out.newLine();

for(int i=0;i<lists.length;i++)

{

if(lists[i].endsWith(".pdf"))

{

out.write("><pdf_files name=\"assets/"+lists[i]+"\"/>");

out.newLine();

}

}

}

out.write(line);

out.newLine();

out.flush();

}

in.close();

out.close();

~Tim

EDIT: remove the extra '>' in the write statement inside the for loop. it is caused by a bug in the forum software

Message was edited by:

SomeoneElse

SomeoneElsea at 2007-7-9 21:04:28 > top of Java-index,Java Essentials,New To Java...
# 6

> private String PDFLineInsert(String line, String PDF)

>

> {

> String res = "";

> res = line.replace("<assets>",

> , "<assets>\n\r<pdf_files

> name=\"assets/"+PDF+"\"/>");

> return res;

> }

Ouch, I didn't even look at that method. Using replace all to "append" the data to the string is really ugly. Just write the data to the file directly or use a StringBuilder.

Kaj

kajbja at 2007-7-9 21:04:28 > top of Java-index,Java Essentials,New To Java...
# 7

Write a class like this...

class fileNameComparator implements Comparator

{

public int compare(Object fileName1, Object fileName2)

{

//Implement your logic to extract the digits at the end of your filename1 and fileName2

//Compare the two ints... If int of fileName1 > int of fileName2 then return 1

//If int of fileName1 < int of fileName2 then return -1

//else return 0

}

}

Now add all the fileNames (only Strings) to a an ArrayList having List reference. Call Collections.sort(yourList, fileNameComparator) and print the output. The list will be sorted the way you need. The sort method internally uses the logic you write in compare method of your Comparator.

Please let me know if this is not clear.

Kiran_Joisa at 2007-7-9 21:04:28 > top of Java-index,Java Essentials,New To Java...
# 8

> Write a class like this...

>

> class fileNameComparator implements Comparator

> {

>

> public int compare(Object fileName1, Object

> t fileName2)

> {

> //Implement your logic to extract the digits at the

> he end of your filename1 and fileName2

> //Compare the two ints... If int of fileName1 > int

> nt of fileName2 then return 1

> //If int of fileName1 < int of fileName2 then

> en return -1

> //else return 0

> }

> }

>

> Now add all the fileNames (only Strings) to a an

> ArrayList having List reference. Call

> Collections.sort(yourList, fileNameComparator) and

> print the output. The list will be sorted the way you

> need. The sort method internally uses the logic you

> write in compare method of your Comparator.

>

> Please let me know if this is not clear.

Except that his list is already sorted in the order he needs, the problem, as I stated in my post above, is the way he is writing the output to thte file, he is always replacing the first line with <first line><\n\r><new line>

~Tim

SomeoneElsea at 2007-7-9 21:04:28 > top of Java-index,Java Essentials,New To Java...
# 9
> Except that his list is already sorted in the order> he needsThe file system can return the files in any order so he should sort even though they are in correct order right now.Kaj
kajbja at 2007-7-9 21:04:28 > top of Java-index,Java Essentials,New To Java...
# 10
Thanks it's working.
Simmya at 2007-7-9 21:04:28 > top of Java-index,Java Essentials,New To Java...
# 11

> > Except that his list is already sorted in the

> order

> > he needs

>

> The file system can return the files in any order so

> he should sort even though they are in correct order

> right now.

>

> Kaj

True Enough

~Tim

SomeoneElsea at 2007-7-9 21:04:28 > top of Java-index,Java Essentials,New To Java...
# 12
Simmy, Is the Comparator working?
Kiran_Joisa at 2007-7-9 21:04:28 > top of Java-index,Java Essentials,New To Java...
# 13
The code given by SomeoneElse is working
Simmya at 2007-7-9 21:04:28 > top of Java-index,Java Essentials,New To Java...