【问题标题】:File writing with blob data使用 blob 数据写入文件
【发布时间】:2013-12-26 09:08:07
【问题描述】:

我正在从 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 行没有写入。

【问题讨论】:

  • 您在每次迭代时重新打开输出文件,肯定会清除以前的版本。
  • 感谢 Alexis,请建议如何打开每个迭代的输出文件

标签: java file blob


【解决方案1】:

您每次都在 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();

【讨论】:

  • 感谢 Bhushan,但所有数据都写在一行中。如何在每行之后启用\n
  • @user2742540 您可以使用\n 换行。我已经相应地编辑了代码。
  • @user2742540 欢迎您。如果它解决了您的问题,您可以接受答案。
【解决方案2】:

替换代码中的行

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

与:

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

【讨论】:

    猜你喜欢
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    相关资源
    最近更新 更多