Wednesday, July 3, 2024

Read a CSV file, copy the CSV file data, concatenate two columns from original file and write it to another csv file

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class CSVModifier {
public static void main(String[] args) {
String line;
String inputFilePath = "F:\\DOCS\\test_03072024.csv";
String outputFilePath = "F:\\DOCS\\updated_test_03072024.csv";

try  {

BufferedReader br = new BufferedReader(new FileReader(inputFilePath));
BufferedWriter bw = new BufferedWriter(new FileWriter(outputFilePath));

// Read the header
String header = br.readLine();
if (header != null) {
String[] headers = header.split(",");
int appIndex = -1;
int roleNameIndex = -1;

// Find the indices of "app" and "Role Name"
for (int i = 0; i < headers.length; i++) {
if (headers[i].trim().equalsIgnoreCase("app")) {
appIndex = i;
} else if (headers[i].trim().equalsIgnoreCase("Role Name")) {
roleNameIndex = i;
}
}

// Check if both columns were found
if (appIndex == -1 || roleNameIndex == -1) {
throw new IllegalArgumentException(
"Input CSV does not contain required columns 'Service' and 'Role Name'");
}

// Add the new column to the header
header += ",app|RoleName";
bw.write(header);
bw.newLine();

// Read and process each line
while ((line = br.readLine()) != null) {
String[] columns = line.split(",");
String app = columns[appIndex];
String roleName = columns[roleNameIndex];
// Concatenate "app" and "Role Name"
String newColumn = app + "|" + roleName;
// Add the new column to the line
line += "," + newColumn;
bw.write(line);
bw.newLine();
}
}
bw.close();

} catch (IOException e) {
e.printStackTrace();
}
}
}

Fetch Members from Workgroup

  import java.util.ArrayList;   import java.util.Iterator;   import java.util.List;   import sailpoint.api.ObjectUtil;   import sailpoint.ob...