2012-09-25 40 views
1

我尝试导出使用下面的代码Feed文件:如何使用java导出文件?

String appname = "abc"; 
String path = "//home/exportfile//"; 
String filename = path + "Uncorrelated_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("Userid~First_Name~Last_Name~Facility/CBO~Title~Menu"); 
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 

     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)) 
       { 
        if (workforceid == null) 
        { 
         userid = (String) l.getAttribute("Userid"); 
         fn = (String) l.getAttribute("First Name"); 
         ln = (String) l.getAttribute("Last Name"); 
         menu = (String) l.getAttribute("Menu"); 

         List fac = l.getAttribute("Facility/CBO"); 
         List title = l.getAttribute("Title"); 

         for (Object fac1 : fac) 
         { 
          for (Object title1 : title) 
          { 
           // Output file 

           out 
            .write(userid + "~" + fn + "~" + ln + "~" + fac1 + "~" + title1 + "~" 
             + menu); 
           out.newLine(); 
           out.flush(); 

          } 
         } 
        } 
       } 
      } 
     } 
    } 

    ret = "true"; 
} 
// Close file and return 
out.close(); 
return ret; 

当我执行上面的代码,我收到以下错误:

An unexpected error occurred: java.lang.Exception: sailpoint.tools.GeneralException: BeanShell script error: Sourced file: inline evaluation of: import java.io.FileWriter; import java.io.File; import java.io.B . . . '' : The collection, array, map, iterator, or enumeration portion of a for statement cannot be null. : at Line: 80 : in file: inline evaluation of: ``import java.io.FileWriter; import java.io.File; import java.io.B . . . '' : for (Object title1 : title) { BSF info:

我知道,在我的饲料文件,标题栏中有几个空白记录,所以理想情况下,代码应该将除标题栏外的所有内容写入文件中。任何人都可以帮助我在哪里做代码更改?

回答

0
    List fac = l.getAttribute("Facility/CBO"); 
        List title = l.getAttribute("Title"); 

        for (Object fac1 : fac) 
        { 
         for (Object title1 : title) 

fac或title被设置为null,然后您试图迭代它。你应该在执行循环之前引入一些空的检查。