-4
File outFile = new File("D:\\output.txt");
BufferedWriter wb = new BufferedWriter(new FileWriter(outFile));


while (resultSet.next()) {
    int attr_id = resultSet.getInt("int_id");
    String stringValue = resultSet.getString("StringValue");
    String name = resultSet.getString("Name");
    int index = stringValue.indexOf(".");
    int valueLength = stringValue.length();
    if(isNumeric(stringValue)) {
        //if(index != -2 ) {
        if(index != (valueLength - 2)) {
            String string1 = Double.valueOf(stringValue).toString();
            System.out.println("converted values : " +string1);
            System.out.println("stringValue : " +stringValue);
            System.out.println("intValue : " +int_id);
            wb.write( stringValue + "," + int_id + "," + string1  );
            wb.newLine();
        }
    }
}

Above is my part of the code, from resultset i'm writing the data into a file. However the code is not printing values in output.txt file but i could see the result in console.
if i remove the commented line and comment if(index != (valueLength - 2)) { this line, the java code is creating output.txt with values.

What's wrong?

dertkw
  • 7,798
  • 5
  • 37
  • 45
Marjer
  • 1,313
  • 6
  • 20
  • 31
  • 3
    Your title is just asking for downvotes. Rewrite it to be more specific and include more details about why your code isn't working in the body of your question. – Mdev Jun 18 '14 at 16:48
  • Have you tried debugging through your code to see what `valueLength` is each time your code executes the `if` statement? – Mike Koch Jun 18 '14 at 16:48
  • possible duplicate of [BufferedWriter not writing everything to its output file](http://stackoverflow.com/questions/13426142/bufferedwriter-not-writing-everything-to-its-output-file) – Raedwald Jan 03 '15 at 11:19

2 Answers2

1

probably need to just call flush and/or close on your BufferedWriter when you are done.

CaptRespect
  • 1,985
  • 1
  • 16
  • 24
0

The reason why your code is not writing to the file is because you never push the data from the Writer to the actual file.

Think of BufferedWriters as an email you are sending to a coworker. When you use:

        wb.write( stringValue + "," + int_id + "," + string1  );

it is like you are typing the email to your coworker, and defining the message to them. BUT, since you did not press "send email", your coworker will never see the message. The same concept can be applied here.

You are filling the writer with a bunch of data, but you are never sending the data to the file that you are trying to write to. There are two ways that you can do this, the first being to "flush" the writer (see documentation HERE). You can do this by calling

    wb.flush();

This is the equivalent of pressing "send email", and will write the data to the file that you are editing. The second method would be to "close" the writer (see documenation HERE). The reason that this works is because the BufferedWriter's close method flushes the stream first, before closing the stream. This can be done by calling

    wb.close();

Although, it would not be wise to do so until you are fully done editing the file, as it can no longer be accessed.

The following is your code edited to flush the stream after every record you are writing, and then close the stream after all records have been processed. Note the locations of wb.flush() and wb.close().

File outFile = new File("D:\\output.txt");
    BufferedWriter wb = new BufferedWriter(new FileWriter(outFile));


    while (resultSet.next()) {
        int attr_id = resultSet.getInt("int_id");
        String stringValue = resultSet.getString("StringValue");
        String name = resultSet.getString("Name");
        int index = stringValue.indexOf(".");
        int valueLength = stringValue.length();
        if(isNumeric(stringValue)) {
            //if(index != -2 ) {
            if(index != (valueLength - 2)) {
                String string1 = Double.valueOf(stringValue).toString();
                System.out.println("converted values : " +string1);
                System.out.println("stringValue : " +stringValue);
                System.out.println("intValue : " +int_id);
                wb.write( stringValue + "," + int_id + "," + string1  );
                wb.newLine();
                wb.flush();
            }
        }
    }
    wb.close();
awksp
  • 11,764
  • 4
  • 37
  • 44
Mike Elofson
  • 2,017
  • 1
  • 10
  • 16