I want every unique serial number records to be in a separate file
That means
data containing sno. 1 ...will be in one separate file named after the sno

My problem , is i dont get all the records of sno.1 in the separate file.
I see all records of sno.2 in another separate file.

How can i achieve all the records of sno.1 to be in the separate file.
Pls see my code and help me figure out the mistake.
I tried by changing the flush and close statements in different places in the code ..to achieve all records of sno.1 in the separate file..But i didnot get to solve the problem.

Your resultset isn't ordered (that is there is no ORDER BY in the query), so how do you know what order the rows are going to come back in?
If you have a serialId 1 row followed by a serialId 2 row, followed again by a serialId 1 row then the first one will be overwritten.

The other option (though I, personally, would go for ordered) is to set the append flag in the FileWriter constructor to true. However this might mean earlier runs will have rows appended to them, depending on what you're doing.

Hi , The above peice of code is to create a unique separate file for sno.1 records only .
If you see the if clause , gets the first unique record and the else part will continue adding records with the same sno. into the file.
In the if clause , i write lines to add records to the file .FileWriter constructor , BuffereWriter constructor .
I also wrote a flush statement their. If i close the writer after flush. Records are not entering into the file.

Thats why , i added the finally block at the end ..adding flushing and closing statements .(pls see my first mail with the full code )

The program crates a cumulative file for all data and separate files created upon each change in sno.

I have another file writer adding all the records into the cumulative file.

Thats why i keep the else part to create the separate file with unqiue records

That makes no senxe. The if statement is simply to decide whether to open a new writer or not. Since the if statement and the else statement contain several lines of the same code then that code should not be in either and should be outside of those statements.

What I wrote above is logically exactly the same as the code you wrote using if/else.

write2Sep.close()...you already do it later on. And you do that before creating the new one.

This may well not cure your problem, but leaving them open has the potential to cause a problem.

Yes Sir , I did change the code as you said , putting the writer in the if clause and it does create 2 separate files.
But i still have the problem , of not having all the records with sno.1 (that is my first separate file does not contain all the records)
where do you suggest me to close the writer .

I am not really knowing , how to use the flush and close method , before i create another separate file .Because first time i create the file in the if clause and every new file is created in the if clause.

I said earlier, just before opening the new FileWriter, ensure you close write2Sep (if it's not null). If you don;t do that then you don't close anything except the last file and I can't guarantee what will happen.

You also only need to close write2Sep. That will close the FileWriter at the same time.
In fact you can change this:

Java Code:

write2Sep.close(); // This is the close I'm talking about
fw=new FileWriter(SeparateFile, false);
write2Sep = new BufferedWriter(fw);

to this:

Java Code:

write2Sep.close(); // This is the close I'm talking about
write2Sep = new BufferedWriter(new FileWriter(SeparateFile, false));

Exception in thread "main" java.lang.NullPointerException
And it points to the write2Sep.close();
statement .

Java Code:

BufferedWriter write2Sep = null;
QUERY is written here
oldSno="";
while(resultset.next()){
:
:
:
:
if(!(oldSno.equals(sno))){
SeparateFile = location + "\\" +yyyy+"_"+sno+"_RXG_"+type+".csv";
write2Sep.close(); // This is the close I'm talking about
write2Sep = new BufferedWriter(new FileWriter(SeparateFile, false));
}
:
:
:
Sir,
before even we create the write2Sep ..how can we close it at the first instance ..
that is what is happening here...and it gives me a nullpointer exception.
I am not knowing ..how i can create new separate files with different names based on the sno ...if i dont write the write statement in the if clause...
ANy more help ..is appreciated .
Thank You for the patience ...

I said earlier, just before opening the new FileWriter, ensure you close write2Sep (if it's not null). If you don;t do that then you don't close anything except the last file and I can't guarantee what will happen.

You also only need to close write2Sep. That will close the FileWriter at the same time.
In fact you can change this:

Java Code:

write2Sep.close(); // This is the close I'm talking about
fw=new FileWriter(SeparateFile, false);
write2Sep = new BufferedWriter(fw);

to this:

Java Code:

write2Sep.close(); // This is the close I'm talking about
write2Sep = new BufferedWriter(new FileWriter(SeparateFile, false));