Need to print the filenames that are not present inside an xml file
The name of all the.txt files are supposed to be present inside anxml file.
I need to print the name of the.txt files that arepresent in thexml file, which I am able to do so.
But I am unable to print the name of the.txt files that arenot present in thexml file. Please help me with my code.
My code is
void CheckFile(File RootDir, String file)throws IOException
{
String[] lists=RootDir.list();//listing the files of a directory
File DriverFile=new File(file);
int val;
char ch;
File inputfile=new File(file);
File inputfileTmp=File.createTempFile(inputfile.getName(),"tmp");
BufferedReader br=null;
BufferedWriter bw=null;
String line;
try
{
br=new BufferedReader(new FileReader(inputfile));
bw=new BufferedWriter(new FileWriter(inputfileTmp));
LineNumberReader lineRead=new LineNumberReader(br);
while((line=lineRead.readLine())!=null)
{
if(line.endsWith(".txt\">"))
{
for(int i=0;i<lists.length;i++)
{
if(lists[i].endsWith(".txt"))
{
if(line.contains(lists[i]))
{
System.out.println("lists[i]_"+lists[i]);
//Prints only the name of the .txt files contained inside the xml file
}
if(!line.contains(lists[i]))
{
System.out.println("lists[i]_"+lists[i]);
//Prints the name of all the .txt files
//But I need to print only the name of the .txt files which are not present inside the xml file
}
}
}
}
bw.write(line);
bw.newLine();
}
}
catch(NullPointerException n)
{
System.out.println(n.getMessage());
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
finally
{
try
{
br.close();
}
catch(Exception e)
{
}
try
{
bw.close();
}
catch(Exception e)
{
}
}
if(inputfile.delete())
inputfileTmp.renameTo(inputfile);
}
>
[4177 byte] By [
sony_tja] at [2007-11-26 19:08:19]

I'd extract the two lists into two collections and manipulate them afterwards.
Could you please tell me what modifications to do in my code
> > if(line.contains(lists[i]))
> {
>System.out.println("lists[i]_"+lists[i]);
>//Prints only the name of the .txt files contained inside the xml file
> }
>
A quick fix would be to change the above if statement as follows:
if ((lists[i] != null) && (line.contains(lists[i]))) {
System.out.println("lists[" + i + "]_" + lists[i]);
lists[i] = null;
//Prints only the name of the .txt files contained inside the xml file
}
Then delete the if statement that comes after it and after the while loop add the following:
for (int i = 0; i < lists.length; i++) {
if ((lists[i] != null) && (lists[i].endsWith(".txt"))) {
System.out.println("lists[" + i + "]_" + lists[i]);
}
}
Edit: Note that this is probably not the best fix, but it is a quick fix.
This is about the same as your other post with the xml files... I hope you aren't writing 2 separate functions to do these 2 tasks? But now I see how you process that file, it's completely irrelevant that you are using an XML file at all.
I think you should split this up into 2 parts:
- parse the XML file into some collection of Strings ending with txt"/> or even only the string that should correspond to a filename. In that case you can compare using equals which will avoid matching a.txt when your xml file contains ba.txt...
- for each filename in the directory check if one of the Strings in the above collection contains the filename. Put this test in a separate function that returns a true in case the filename in there and false otherwise...
If you would like to reuse this for different types of extensions, make the extension variable. You could also add a FileNameFilter as I said in your other post!
My modified code as per your directions
void CheckFile(File RootDir, String file) throws IOException
{
String[] lists=RootDir.list();
File DriverFile=new File(file);
int val;
char ch;
File inputfile=new File(file);
File inputfileTmp=File.createTempFile(inputfile.getName(),"tmp");
BufferedReader br=null;
BufferedWriter bw=null;
String line;
try
{
br=new BufferedReader(new FileReader(inputfile));
bw=new BufferedWriter(new FileWriter(inputfileTmp));
LineNumberReader lineRead=new LineNumberReader(br);
while((line=lineRead.readLine())!=null)
{
if(line.endsWith(".txt\">"))
{
for(int i=0;i<lists.length;i++)
{
if(lists[i].endsWith(".txt"))
{
if ((lists[i] != null) && (line.contains(lists[i])))
{
System.out.println("1_lists[" + i + "]_" + lists[i]);
lists[i] = null;
//Prints only the name of the .txt files contained inside the txt file
}
}
}
}
bw.write(line);
bw.newLine();
}
for (int i = 0; i >< lists.length; i++)
{
if ((lists[i] != null) && (lists[i].endsWith(".txt")))
{
System.out.println("2_lists[" + i + "]_" + lists[i]);
}
}
}
catch(NullPointerException n)
{
System.out.println(n.getMessage());
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
finally
{
try
{
br.close();
}
catch(Exception e)
{
}
try
{
bw.close();
}
catch(Exception e)
{
}
}
if(inputfile.delete())
inputfileTmp.renameTo(inputfile);
}
XML file is
I am checking if the filenames are present in the given xml file
<?xml version="1.0" encoding="tf-8"?>
<!DOCTYPE constrct PBLIC "AAAAAAAAA" "sss.dtd" [
><!ENTITY fm00123001 SYSTEM "123_001_fm.txt">
<!ENTITY ch01123001 SYSTEM "123_001_ch01.txt">
<!ENTITY ch02123001 SYSTEM "123_001_ch02.txt">
]>
<constrct version="2.2.2" isbn="123" id="id_123_001_000001">
&fm123001;
<maincontent id="id_123_001_000002" page-nm="i">
&ch01123001;
&ch02123001;
</maincontent>
&bk01123001;
</constrct>
After I run the program, the output is
1_lists[4]_123_001_fm.txt
1_lists[2]_123_001_ch01.txt
null
and also my xml file gets deleted after the line <!ENTITY fm00123001 SYSTEM "123_001_fm.txt">
My filenames in the directory are
log.txt
123_001_ch01.txt
123_001_ch02.txt
123_001_fm.txt
I need print the filenames that are not present in the xml file. Here it should be log.txt
Well, you are getting a null pointer exception (You would do well to add a printStackTrace to those catch blocks or at least give more info in your print statement in those blocks)
change
if(lists[i].endsWith(".txt"))
to
if ((lists[i] != null) && (lists[i].endsWith(".txt")))
And, come on, some analysis you should be able to do yourself.
Edit:
And you seriously want to reconsider simply renaming the tempfile without checking as to whether there was an exception or not. The file was truncated because an exception caused an abort the try block and so the file was not completely written, but you still deleted the original and replaced it with the partially written tempfile.
Thanks a lot for the solution.