繁体   English   中英

使用Blob数据写入文件

[英]File writing with blob data

我正在从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");

带有:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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