2012-08-03 64 views
0

我試圖用下面的代碼導出4列。最後一列組織是List。如何將列表操作爲字符串並在單獨的行中打印

String appname = "abc"; 
    String path = "//home/exportfile//"; 
    String filename = path + "ApplicationExport-" + appname + ".txt"; 
    String ret = "false"; 

    QueryOptions ops = new QueryOptions(); 
    Filter[] filters = new Filter[1]; 
    filters[0] = Filter.eq("application.name", appname); 
    ops.add(filters); 

    List props = new ArrayList(); 
    props.add("identity.name"); 

    // Do search 
    Iterator it = context.search(Link.class, ops, props); 

    // Build file and export header row 
    BufferedWriter out = new BufferedWriter(new FileWriter(filename)); 
    out.write("IdentityName,UserName,WorkforceID,Organization"); 
    out.newLine(); 

    // Iterate Search Results 
    if (it != null) { 
     while (it.hasNext()) { 

      // Get link and create object 
      Object[] record = it.next(); 
      String identityName = (String) record[0]; 
      Identity user = (Identity) context.getObject(Identity.class, identityName); 

      // Get Identity attributes for export 
      String workforceid = (String) user.getAttribute("workforceID"); 

      // Get application attributes for export 
      String userid = ""; 

      List links = user.getLinks(); 
      if (links != null) { 
       Iterator lit = links.iterator(); 
       while (lit.hasNext()) { 
        Link l = lit.next(); 
        String lname = l.getApplicationName(); 
        if (lname.equalsIgnoreCase(appname)) { 
         userid = (String) l.getAttribute("User Name"); 
         List organizations = l.getAttribute("Organization"); 

         StringBuilder sb = new StringBuilder(); 
         String listItemsSeparator = ","; 

         for (Object organization : organizations) { 
          sb.append(organization.toString()); 
          sb.append(listItemsSeparator); 
         } 

         org = sb.toString().trim(); 

        } 
       } 
      } 

      // Output file 
      out.write(identityName + "," + userid + "," + workforceid + "," + org); 
      out.newLine(); 
      out.flush(); 
     } 

     ret = "true"; 
    } 

    // Close file and return 
    out.close(); 
    return ret; 

上面的代碼的輸出將be.for例如:

IdentityName,UserName,WorkforceID,Organization 

dthomas,dthomas001,12345,Finance,HR 

我如何獲得在下面的方式輸出

IdentityName,UserName,WorkforceID,Organization 

dthomas,dthomas001,12345,Finance 

dthomas,dthomas001,12345,HR 

什麼,在哪裏我需要改變碼?

回答

0

你必須一行寫入文件每個組織。因此,基本上,不要將用戶的所有組織連接到字符串構建器,並將輸出語句移到遍歷組織的for循環中。

但它很難提供一個工作的例子,因爲你是你,並不包括尚未編譯代碼...


您應該會稍微接近於解決方案:

if (links != null) { 
    Iterator lit = links.iterator(); 
    while (lit.hasNext()) { 
    Link l = lit.next(); 
    String lname = l.getApplicationName(); 
    if (lname.equalsIgnoreCase(appname)) { 
     userid = (String) l.getAttribute("User Name"); 
     List organizations = l.getAttribute("Organization"); 
     for (Object organization : organizations) { 
     // Output file 
     out.write(identityName + "," + userid + "," + workforceid + "," + organization); 
     out.newLine(); 
     out.flush(); 
     } 
    } 
    } 
} 
+0

我試過刪除了stringbuilder,併爲輸出做了For循環,但沒有得到任何幫助.. – user1565893 2012-08-03 20:16:22

+0

它工作..感謝Andreas_D – user1565893 2012-08-03 20:54:46

0

刪除此最內for塊和相關聯的變量:

StringBuilder sb = new StringBuilder(); 

for (Object organization : organizations) 
{ 
    sb.append(organization.toString()); 
    sb.append(listItemsSeparator); 
} 

org = sb.toString().trim(); 

移動organizationsif (it != null) {塊的聲明:

// Get application attributes for export 
String userid = ""; 
List organizations = null; 
List links = user.getLinks(); 

if (it != null) 
{ 
    Iterator lit = links.iterator(); 
    while (lit.hasNext()) 
    { 
     Link l = lit.next(); 
     String lname = l.getApplicationName(); 
     if (lname.equalsIgnoreCase(appname)) 
     { 
      userid = (String) l.getAttribute("User Name"); 
      organizations = l.getAttribute("Organization"); 

,然後改變該文件輸出代碼:

// Output file 
out.write(identityName + "," + userid + "," + workforceid + "," + org); 
out.newLine(); 

要這樣:

// Output file 
for (Object organization : organizations) 
{ 
    out.write(identityName + "," + userid + "," + workforceid + "," + organization.toString()); 
    out.newLine(); 
} 
+0

我早些時候試過這個選項。但是我被以下錯誤卡住了: java.io.FileWriter; import java.io.File; import java.io.B。 。 。 '':無法遍歷類型:class bsh.Primitive:at Line:81:in file:inline evaluation of:import java.io.FileWriter; import java.io.File; import java.io.B。 。 。 '':for(Object organization:organizations) – user1565893 2012-08-03 20:14:51

相關問題