How to remove a row from a csv file if that row contains a certain data in java

 

问题

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 and my goal is to remove a row from that csv if the csv file contains that username input I provide

the current csv file is:

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();

}

}

Please advice Thanks in advance !!

解答

这个问题需要将 csv 中含有特定数据的整行删除。Java 实现则代码较长。

Java 下的开源包 SPL 很容易写,只要 1 句:


A

1

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

SPL提供了JDBC 供 Java 调用,把上面的脚本存为removeUser.splx,在 Java 中以存储过程的方式调用脚本文件:

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

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

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

st.setObject(1,"shaneli");

st.execute();

SPL 源代码:https://github.com/SPLWare/esProc

问答搜集