2010-04-08 86 views
0
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(); 

      } 
    } 
} 

上面的代碼工作正常。 我的問題是將數據追加到同一個文本文件用java

  1. 「RS」的結果集包含了表中的一個記錄
  2. 「RS1」結果集包含表5的記錄
  3. 「RS2」的結果集包含表5的記錄

「rs」數據正在遞歸。

同時寫入同一個文本文件,輸出我得到像

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 

輸出我需要什麼東西在我的代碼改變..請建議

-----------------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 

回答

2
new FileOutputStream(f,true); 

您已經將append標誌設置爲true(第二個參數),因此請按照您對上一個結果集所做的操作進行操作。

參見:http://java.sun.com/javase/6/docs/api/java/io/FileOutputStream.html

+0

沒有probs。雖然我錯誤地添加了FileWriter對象的鏈接,而不是您建議的FileOutputStream。我的答案(我刪除,因爲它是類似於你的)包含一個FileWriter鏈接... – 2010-04-08 11:14:56

+0

wheather我需要做任何修改嗎? 「RS」的結果集包含一個數據和「RS1」的結果集包含10 data.how循環「RS2」在這裏。 請回復ASAP..Its急.. 而(rs.next()){ bw.write(rs.getString(1)== NULL 「」:rs.getString(1)) ; bw.write(「」); } – Manu 2010-04-08 11:16:30

+0

我有一些問題在這裏 – Manu 2010-04-08 11:52:10