使用java将数据附加到同一文本文件
以上代码工作正常。 我的问题是使用java将数据附加到同一文本文件,java,Java,以上代码工作正常。 我的问题是 “rs”结果集在表中包含一条记录 “rs1”结果集在表中包含5条记录 “rs2”结果集在表中包含5条记录 “rs”数据正变得递归 在写入同一个文本文件时,我得到的输出如下 SimpleDateFormat formatter = new SimpleDateFormat("ddMMyyyy_HHmmSS"); String strCurrDate = formatter.format(new java.util.Date()); String s
SimpleDateFormat formatter = new SimpleDateFormat("ddMMyyyy_HHmmSS");
String strCurrDate = formatter.format(new java.util.Date());
String strfileNm = "Customer_" + strCurrDate + ".txt";
String strFileGenLoc = strFileLocation + "/" + strfileNm;
String Query1="select '0'||to_char(sysdate,'YYYYMMDD')||'123456789' class_code from dual";
String Query2="select '0'||to_char(sysdate,'YYYYMMDD')||'123456789' class_code from dual";
String Query3="select param from dual";
try {
Statement stmt = null;
ResultSet rs = null;
Statement stmt1 = null;
ResultSet rs1 = null;
stmt = conn.createStatement();
stmt1 = conn.createStatement();
stmt2 = conn.createStatement();
rs = stmt.executeQuery(Query1);
rs1 = stmt1.executeQuery(Query2);
rs2 = stmt2.executeQuery(Query3);
File f = new File(strFileGenLoc);
OutputStream os = (OutputStream)new FileOutputStream(f,true);
String encoding = "UTF8";
OutputStreamWriter osw = new OutputStreamWriter(os, encoding);
BufferedWriter bw = new BufferedWriter(osw);
while (rs.next() ) {
bw.write(rs.getString(1)==null? "":rs.getString(1));
bw.write(" ");
}
bw.flush();
bw.close();
} catch (Exception e) {
System.out.println(
"Exception occured while getting resultset by the query");
e.printStackTrace();
} finally {
try {
if (conn != null) {
System.out.println("Closing the connection" + conn);
conn.close();
}
} catch (SQLException e) {
System.out.println(
"Exception occured while closing the connection");
e.printStackTrace();
}
}
return objArrayListValue;
}
The above code is working fine. it writes the content of "rs" resultset data in text file
Now what i want is ,i need to append the
the content in "rs2" resultset to the "same text file"(ie . i need to append "rs2" content with "rs" content in the same text file)..
------------------edit part----------------
stmt = conn.createStatement();
stmt1 = conn.createStatement();
stmt2 = conn.createStatement();
rs = stmt.executeQuery(Query1);
rs1 = stmt1.executeQuery(Query2);
rs2 = stmt2.executeQuery(Query3);
while ( rs.next() ) {
while(rs1.next()){
while(rs2.next()){
bw.write(rs.getString(1)==null? "":rs.getString(1));
bw.write("\t");
bw.write(rs1.getString(1)==null? "":rs1.getString(1));
bw.write("\t");
bw.write(rs2.getString(1)==null? "":rs2.getString(1));
bw.write("\t");
bw.newLine();
}
}
}
但我需要像下面这样的输出
1 2 3
1 12 21
1 23 25
1 10 5
1 8 54
我需要在代码中更改哪些内容。。请指教
1 2 3
12 21
23 25
10 5
8 54
您已经将append标志设置为true(第二个参数),因此请执行与上一个结果集相同的操作
请参阅:无问题。尽管我错误地添加了指向FileWriter对象的链接,而不是您建议的FileOutputStream。我的答案(我删除了它,因为它与你的类似)包含一个FileWriter链接…我需要在这里做任何修改吗?“rs”结果集包含一个数据,“rs1”结果集包含10个数据。如何在此处循环“rs2”。请尽快回复,这很紧急。。当(rs.next()){bw.write(rs.getString(1)=null?”:rs.getString(1));bw.write(“”;})时,我真的不明白您在问什么。@PetarMinChev…请查看源程序中的编辑部分,以便按rs对结果进行分组?
-----------------edit part1------------------
Expected Result is
1 2 3
1 12 21
1 23 25
1 10 5
1 8 54
but output i got like
1 2 3
12 21
23 25
10 5
8 54
new FileOutputStream(f,true);