我正在处理具有非常大的数据集的csv文件。在读取文件的时候,我提取了第四名(BALANCE)';'通过while循环迭代从每行中分离数值。经过一些数学计算(此处除法)后,制作一个双重的arraylist。
现在我想以相反的顺序(从头到尾)存储Double的这个arraylist。它的原始位置(这里是文件中的第四位).example
input
1,2,3,4
2,3,4,5
3,4,5,6
output
1,2,3,6
2,3,4,5
3,4,5,4
我试图扭转它但没有成功。我不知道这是否适合我的问题。我怎么能这样做?
然后在使用字符串构建器之后,我使用writer方法在新文件中写回数据。
package csvtest7;
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 java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
public class Csvtest7 {
public static void main(String[] args)throws IOException {
String filename = "sample dataset.csv";
List<Double> list = new ArrayList<Double>();
File file = new File(filename);
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter("lyupdated.csv"));
} catch (IOException e) {
}
try {
Scanner inputStream = new Scanner(file);
inputStream.next();
int count = 0;
int number = 11;
while (inputStream.hasNext()) {
String data = inputStream.next();
String[] values = data.split(",");
double balance = Double.parseDouble(values[3]);
balance = balance / number;
count = count+1;
values[3] = String.valueOf(balance);
list.add(balance);
Collections.reverse(list); // I tryied this method but don't work.
// iterate through the values and build a string out of them
StringBuilder sb = new StringBuilder();
// String newData = sb.toString();
for (int i = 0; i < values.length; i++) {
sb.append(values[i]);
if (i < values.length - 1) {
sb.append(";");
}
}
// get the new string
System.out.println(sb.toString());
writer.write(sb.toString()+"\n");
}
writer.close();
inputStream.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(Csvtest7.class.getName()).log(Level.SEVERE, null, ex);
}
}
}