How to Remove a Row from a CSV File If That Row Contains a Certain Data in Java

Question

Source: https://stackoverflow.com/questions/66975750/how-to-remove-a-row-from-a-csv-file-if-that-row-contains-a-certain-data-in-java

I have a CSV file that basically mimics a database. My goal is to remove a row from the CSV if the row contains the username input that I provide.

Below is the current CSV file:

Jack chan,customer,jack@yorku.ca,jack12,3144134414,13 Arboretum,user2

Donald tusk,customer,donald@yorku.ca,donald1,1213141114,14 Arboretum,user3

tom jack,customer,tom11@yahoo.com,tom44,131344122,14 wells st,user34

jack,parking officer,12rfw@hmail.com,jack,12131131134,12ddcscs,peo1

jewel khan,parking officer,jkhan@hotmail.com,jwel12,2131412141,12 wliis str,peo2

shane li,parking officer,shane@gmail.com,shaneli,1343513414,13 mac st,peo33

james chang,parking officer,james15@gmail.com,james12,31452434114,13 chang st,peo77

my objective is to remove the row of say Shane li using his username "shaneli" and not causing any change to other data. but the current code I have is not causing the file's other data to change

the expected output csv file is row with shaneli gets deleted with other rows remaining intact:

Jack chan,customer,jack@yorku.ca,jack12,3144134414,13 Arboretum,user2

Donald tusk,customer,donald@yorku.ca,donald1,1213141114,14 Arboretum,user3

tom jack,customer,tom11@yahoo.com,tom44,131344122,14 wells st,user34

jack,parking officer,12rfw@hmail.com,jack,12131131134,12ddcscs,peo1

jewel khan,parking officer,jkhan@hotmail.com,jwel12,2131412141,12 wliis str,peo2

james chang,parking officer,james15@gmail.com,james12,31452434114,13 chang st,peo77

this is the code java code I have and I need a java solution:

private static String userPath = "/CSVs/database.csv";

public void removeUser(String name,String userType,String email,String userName,String phoneNumber,String address,String password) {

// FIX THIS

String tmpFile = "tmp.csv";

// String target1 = ""; String target2 =""; String target3 =""; String target4 =""; String target5 ="";String target6 ="";String target7 ="";

String target = "";

File oldFile = new File(userPath);

File newFile = new File(tmpFile);

System.out.println(userName);

try {

FileWriter fw = new FileWriter(tmpFile, true);

BufferedWriter bfw = new BufferedWriter(fw);

PrintWriter pw = new PrintWriter(bfw);

x = new Scanner(new File(userPath));

x.useDelimiter("[,\n]");

while (x.hasNext()) {

target = x.next();

if (!target.equals(userName)) {

pw.printf("%s,%s,%s,%s,%s,%s,%s\n", name, userType,email,userName,phoneNumber,address,password);

// pw.println(target + "," + target + "," + target + "," + target + "," + target + "," + target + "," + target);

}

}

x.close();

pw.flush();

pw.close();

oldFile.delete();

File dmp = new File(userPath);

newFile.renameTo(dmp);

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}

}

Answer

You need to delete the whole row containing specific data from a CSV file. The Java code will be rather long if you try to use the high-level language to do this.

It is very simple to accomplish the task in SPL, an open-source Java package. You just need one line of code, as shown below:

A

1

>file("tmp.csv").export@c(file("database.csv").import@wc().select(~(4)!=userNameToDelete))

 

SPL offers JDBC driver to be invoked by Java. Just store the above SPL script as removeUser.splx and invoke it in Java in the same way you call a stored procedure:

Class.forName("com.esproc.jdbc.InternalDriver");

con= DriverManager.getConnection("jdbc:esproc:local://");

st = con.prepareCall("call removeUser(?)");

st.setObject(1,"shaneli");

st.execute();

View SPL source code.