I am retrieving data from DB BLOB(csv) data into CSV files . While writing the data into files each and every time the files are getting over write . Below the code I used for writing the files . If I close the writer inside the for loop then , only one file gets writes others are not getting loaded. If I closed outside the loop then same file gets overwrites every time .
//code:
while (rs.next()) {
String filename = rs.getString("file_name");
String path = ( calPath + filename );
File file = new File(path);
FileOutputStream output = new FileOutputStream(file ,true);
Blob blob = rs.getBlob("file_data");
byte[] bytes1 = blob.getBytes(1l, (int) blob.length());
for (int i = 0; i < bytes1.length; i++) {
output.write(bytes1);
//output.close();
}
output.close();
}
//connection.close();
Due to your inner for Loop it writing file again and again. so remove your inner for loop it will work.
while (rs.next()) {
String filename = rs.getString("file_name");
String path = ( calPath + filename );
File file = new File(path);
FileOutputStream output = new FileOutputStream(file ,true);
Blob blob = rs.getBlob("file_data");
byte[] bytes1 = blob.getBytes(1l, (int) blob.length());
// for (int i = 0; i < bytes1.length; i++) {
output.write(bytes1);
//output.close();
// }
output.close();
}
i think you should check file first. you can write if file not exist
File file = new File(path);
if(!file.exists() && !file.isDirectory()) {
// do your writing?
}
EDIT: also you can compare sizes if you want more specific comparison
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.