简体   繁体   中英

File writing with blob data

I am reading the blob data from Oracle database & writing it to a text file. I have two column in my database called Number & system. I have 100 counts in my table. But only the last row is writing in my text row. below is the code I have tried.

rs =stmt.executeQuery("select Number, system from system");
Blob lob = null;


while (rs.next()) {
  String RECID = rs.getString(1);
  System.out.println("Number"+ Number);

  lob=rs.getBlob("system");
  byte[] bdata = lob.getBytes(1, (int) lob.length());
  String text = new String(bdata);
  System.out.println("text"+ text);

  System.out.println("rs value"+ lob);
  String test=null;
  test=RECID+":"+text;
  FileOutputStream fos = new FileOutputStream("C:/DataRead/system.txt");
  DataOutputStream dos = new DataOutputStream(fos);
  dos.writeBytes(test);

  dos.close();
}
}
catch (Exception e) {
   e.printStackTrace();
}

}
}

In text file i am getting 100th record only other 99 rows are not writing.

You are replacing existing text each time in your while loop.

Try this:

FileOutputStream fos = new FileOutputStream("C:/DataRead/system.txt");
DataOutputStream dos = new DataOutputStream(fos);
while (rs.next()) {
    String RECID = rs.getString(1);
    System.out.println("Number"+ Number);
    lob=rs.getBlob("system");
    byte[] bdata = lob.getBytes(1, (int) lob.length());
    String text = new String(bdata);
    System.out.println("text"+ text);

    System.out.println("rs value"+ lob);
    String test=null;
    test=RECID+":"+text;

    dos.writeBytes(test+"\n");
}
dos.close();

Replace the line in your code

FileOutputStream fos = new FileOutputStream("C:/DataRead/system.txt");

With:

FileOutputStream fos = new FileOutputStream("C:/DataRead/system.txt", true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM