Код: Выделить всё
public static void updateCounter(String name) {
int score = 0;
ArrayList namesArray = new ArrayList();
CSVParser parser = new CSVParserBuilder()
.withSeparator(',')
.withIgnoreQuotations(true)
.build();
try (Writer writer = new FileWriter("output.txt",true)) {
ColumnPositionMappingStrategy
strategy = new ColumnPositionMappingStrategy();
StatefulBeanToCsv sbc = new StatefulBeanToCsvBuilder(writer)
.withSeparator(CSVWriter.DEFAULT_SEPARATOR)
.withMappingStrategy(strategy)
.build();
} catch (IOException e) {
throw new RuntimeException(e);
}
List beans;
try {
beans = new CsvToBeanBuilder(new FileReader("data.csv"))
.withType(Person.class)
.build()
.parse();
for (Person p : beans) {
if (p.getName().equals(name)) {
score = score = p.getscore() + 1;
}
}
CSVReader reader;
{
try {
String[] line;
reader = new CSVReader(new FileReader("data.csv"));
{
try {
while ((line = reader.readNext()) != null) {
for (String cell : line) {
if (cell.equals(name)) {
BufferedWriter writer;
{
try {
writer = new BufferedWriter(new FileWriter("data.csv"));
writer.write(score);
writer.close();
} catch (IOException IOerr) {
throw new RuntimeException(IOerr);
}
}
}
}
}
} catch (CsvException ex) {
throw new RuntimeException(ex);
}
reader.close();
}
} catch (IOException IOerr) {
throw new RuntimeException(IOerr);
}
}
} catch (FileNotFoundException ex) {
throw new RuntimeException(ex);
}
}
Код: Выделить всё
import com.opencsv.bean.CsvBindByPosition;
public class Person {
@CsvBindByPosition(position = 0)
private String Name;
@CsvBindByPosition(position = 1)
private int calledTimes;
public String getName(){
return Name;
}
public void setName(String Name){
this.Name = Name;
}
public int getCalledTimes(){
return calledTimes;
}
public void setCalledTimes(int calledTimes){
this.calledTimes = calledTimes;
}
}

Подробнее здесь: https://stackoverflow.com/questions/790 ... encsv-java
Мобильная версия