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]

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
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.
> 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