[英]How do I create separate output file for each sql record?
public void selectdata(Connection conn, String database){
String table = "Mo"; // using Mo table,
String comandoSql = "SELECT * FROM " + database + "."+"dbo"+ "."+ table;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(comandoSql);
System.out.println("Command sql: ");
while (rs.next()) {
String DocumentName = rs.getString(2);
doc1 = DocumentName;
try {
File file = new File("///.txt"); // path to output folder.
FileWriter fileWritter = new FileWriter(file,true);
BufferedWriter out = new BufferedWriter(fileWritter);
out.write(doc1);
out.newLine();
out.close();
} catch (IOException e1) {
System.out.println("Could not write in the file");
}
}
} catch (SQLException e) {
e.printStackTrace();
}
this generates the output file but contains all the records in one file, my problem is how to generate separate file for each record don't want to do the post process in order to generate the separate files? 这将生成输出文件,但将所有记录都包含在一个文件中,我的问题是如何为每个记录生成单独的文件,不想为了生成单独的文件而进行后期处理? any help??????
任何帮助?????? not fancy for scanner, because don't know how many lines will be in one record(it is a kind of multiple documents which stored into database table!!!!)
扫描仪不喜欢,因为不知道一条记录中将有多少行(它是存储在数据库表中的一种多文档!!!)
Just create a new file for each record: 只需为每个记录创建一个新文件:
int counter = 0;
while (rs.next()) {
String DocumentName = rs.getString(2);
doc1 = DocumentName;
try {
File file = new File("///" + counter + ".txt"); // path to output folder.
FileWriter fileWritter = new FileWriter(file,true);
BufferedWriter out = new BufferedWriter(fileWritter);
out.write(doc1);
out.newLine();
out.close();
counter++;
} catch (IOException e1) {
System.out.println("Could not write in the file");
}
}
Just create a counter variable or something to differentiate each file. 只需创建一个计数器变量或用于区分每个文件的内容即可。 Append the counter to the file name so it will create a new file for each record...
将计数器添加到文件名,以便为每个记录创建一个新文件...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.