简体   繁体   中英

How to export in file using java?

I am trying to export the feed file using below code:

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;

When I execute the above code, I am getting the below error:

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:

I know that in my feed file, there are couple of blank records in title column, so ideally code should write everything in file except title column if its blank. can anyone pls help me where do i make change in code?

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

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

Either fac or title is being set to null and then you are attempting to iterate over it. You should introduce some null checking prior to executing the loops.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM