我有一个数据集csv文件,有大约46000个实例和17个属性..然后我提取列值并修改其值增加1现在我想要csv文件修改值。我可以得到...
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.FileWriter;
//import com.csvreader.CsvWriter;
//import com.opencsv.CSVReader;
//import com.opencsv.CSVWriter;
//import au.com.bytecode.opencsv.CSVWriter;
public class Main {
public static void main(String[] args) {
// TODO code application logic here
String filename ="bank-full.csv";
File file= new File(filename);
try {
Scanner inputStream = new Scanner(file);
inputStream.next();
while(inputStream.hasNext())
{
double abc;
String data= inputStream.next();
String[] values = data.split(";");
double balance= Double.parseDouble(values[11]);
balance=balance+1;
System.out.println(balance);
}
inputStream.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
答案 0 :(得分:0)
您已在String[] values
中拥有值的字符串数组。那么为什么不将修改后的balance
分配给values[11]
然后加入值,用分号粘贴?
您可以尝试在System.out.println(balance);
之后添加此内容:
System.out.println(balance);
// replace original value with the modified balance
values[11] = String.valueOf(balance);
// iterate through the values and build a string out of them
StringBuilder sb = new StringBuilder();
for (int i = 0; i < values.length; i++) {
sb.append(values[i]);
if (i < values.length - 1) {
// if it is not the last value, put a semicolon in between
sb.append(";");
}
}
// get the new string
String newData = sb.toString();
System.out.println(newData);
新值现在存储在字符串newData
中。只需将newData
写入新的.csv文件即可。