Java 使用blob数据写入文件

Java 使用blob数据写入文件,java,file,blob,Java,File,Blob,我正在从Oracle数据库读取blob数据并将其写入文本文件。我的数据库中有两列名为Number&system。我的桌子上有100个计数。但只有最后一行在我的文本行中写入。下面是我尝试过的代码 rs =stmt.executeQuery("select Number, system from system"); Blob lob = null; while (rs.next()) { String RECID = rs.getString(1); System.out.println

我正在从Oracle数据库读取blob数据并将其写入文本文件。我的数据库中有两列名为Number&system。我的桌子上有100个计数。但只有最后一行在我的文本行中写入。下面是我尝试过的代码

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();
}

}
}

在文本文件中,我得到了第100条记录,只有99行没有写入

每次在while循环中都要替换现有文本

试试这个:

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();

替换代码中的行

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


您在每次迭代时都会重新打开输出文件,最明显的是清除了以前的版本。谢谢Alexis,请告知如何打开每个iTeartionTanks Bhushan的输出文件,但所有数据都写在一行中。如何在每次启动后启用\nline@user2742540您可以对新行使用
\n
。我已经相应地编辑了代码。@user2742540欢迎您。如果答案解决了你的问题,你可以接受。
FileOutputStream fos = new FileOutputStream("C:/DataRead/system.txt", true);