我正在编写一个程序,该程序读取CSV文件并修改一些列。修改后,我必须将其写入一个新的CSV文件中进行修改。
我一直在读和修改,但当我把它写到一个新文件时,我只得到一行。我被震撼了。
帮我克服这个?
我使用的是openCSV,语言是Java。
我的代码:
CSVReader reader;
try
{
File file=new File("/home/srinivas/Desktop/saicharan/Cardsmod.csv");
if(file.createNewFile()){ }
reader = new CSVReader(new FileReader(filename));
String filewrite="/home/srinivas/Desktop/saicharan/Cardsmod.csv";
CSVWriter writer=new CSVWriter(new FileWriter(filewrite));
String[] row;
while((row = reader.readNext()) != null)
{
String str=new String();
String str1=new String();
for(int i=0;i<row.length;i++)
{
str1=str1+","+row[i];
if(row[i].equals("Card Text Listen"))
{
String [] nextLine = reader.readNext();
String [] nextLine1=reader.readNext();
str=str+nextLine[i]+nextLine1[i];
c2.met(str);
}
if(i==11)
{
String[] rowwrite=str1.split(",");
writer.writeNext(rowwrite);
}
}
}
writer.close();
}
谢谢。
我看到您读写同一个文件,所以您应该将所有行存储到一个列表中,然后在读取器完成后初始化编写器Exp:
String[] header;
String[] row;
int index = 0;
// processing the header;
header = reader.readNext();
for (int i = 0; i < header.length; i++) {
if (header[i].equals("Card Text Listen")) {
index = i;
break;
}
}
// processing data
while ((row = reader.readNext()) != null) {
for (int i = 0; i < row.length; i++) {
if (index == i) {
row[i] = "new value";
break;
}
}
writer.writeNext(row);
}
以下是实现的最佳有效方法
public void copyCsv(){
CSVReader reader2 = new CSVReader(new FileReader(ADDRESS_FILE));
List<String[]> allElements = reader2.readAll();
CSVWriter writr = new CSVWriter(new FileWriter(Path));
writr.writeAll(allElements);
writr.flush();
writr.close();
}
我使用Sham Khan的答案进行了以下应用程序。也许它对你有用。它根据.csv文件中的id完全更新行。
@Override
public void update(String tableName, Long rowId, List<String> values) {
List<String> select = select(tableName, rowId);
if (!select.isEmpty()) {
select.clear();
select.add(String.valueOf(rowId));
select.addAll(values);
copyCsv(tableName, select, rowId);
}
}
@SneakyThrows
public void copyCsv(String tableName, List<String> values, Long rowId) {
CSVReader reader2 = new CSVReader(new FileReader(tableName + ".csv"));
List<String[]> allElements = reader2.readAll();
allElements.add(Math.toIntExact(rowId), values.toArray(new String[0]));
allElements.remove(Math.toIntExact(++rowId));
CSVWriter writer = new CSVWriter(new FileWriter(tableName + ".csv"));
writer.writeAll(allElements);
writer.flush();
writer.close();
}
@SneakyThrows
@Override
public List<String> select(String tableName, Long rowId) {
PersonResponseDto personResponseDto = selectById(tableName, rowId);
if (personResponseDto == null) {
throw new NotFoundException();
}
LinkedList<String> responseValue = new LinkedList<>();
try (BufferedReader br = new BufferedReader(new FileReader(tableName + ".csv")); CSVParser parser = CSVFormat.DEFAULT.withDelimiter(',').withHeader().parse(br)) {
for (CSVRecord record : parser) {
long recordNumber = record.getRecordNumber();
if (recordNumber == rowId) {
Map<String, String> stringStringMap = record.toMap();
for (Map.Entry<String, String> m : stringStringMap.entrySet()) {
responseValue.add(m.getValue());
}
}
}
}
return responseValue;
}
@SneakyThrows
@Override
public PersonResponseDto selectById(String tableName, Long rowId) {
PersonResponseDto responseDto;
File file = new File(tableName + ".csv");
try (BufferedReader br = new BufferedReader(new FileReader(file)); CSVParser parser = CSVFormat.DEFAULT.withDelimiter(',').withHeader().parse(br)) {
Predicate<CSVRecord> predicate = ((record) -> record.get("id").equals(String.valueOf(rowId)));
responseDto = parser.getRecords().stream().filter(predicate).map(this::preparePersonResponseDto).findFirst().orElseThrow(NotFoundException::new);
}
return responseDto;
}