Просто вы можете читать поля csv-файла и изменять/редактировать и сохранять все поля в другой CSV-файл.
Например, если у меня есть один CSV-файл (user.csv), и этот файл содержит 2 колонки username
и password
, и я хочу, чтобы обновить эти поля, кодирование здесь:
String text = null;
string filename="user.csv";
try {
FileInputStream fis = new FileInputStream(filename);
DataInputStream myInput = new DataInputStream(fis);
while ((text = myInput.readLine()) != null) {
// System.out.println(text);
String[] strar = text.split(",");
String username = strar[0];
String password = strar[1];
// so here you can update both username and password and save into another
csv file
string write=username +","+password ;
WriteToCsv(write);
}
} catch (IOException ex) {
ex.printStackTrace();
enter code here
//save into csv file here
public static synchronized void WriteToCsv(String s) {
PrintWriter pw = null;
try {
File f = new File(newfile);
pw = new PrintWriter(new FileWriter(f, true));
pw.println(s);
pw.flush();
pw.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
pw.close();
}
}