我有一些类似下面这样的数组字符串(注意每个选手后面都有空格,表示不同的行...):
["user1","track1","player1", "user1","track2","player2", "user1","track3","player3", ...]
我希望将这个数组根据行数(空格)分成两部分(几乎相等)。例如,如果这个数组有40个空格,我需要将从第一行到一半(第20行)的行存储在一个文件中(我们称之为文件A),并将剩余的行放入文件B。我可以将ArrayList分成两部分,但是我无法以正确的方式将其写入CSV文件。
以下是我的代码:
public static void main(String[] args) throws java.lang.Exception {
BufferedReader userlines = new BufferedReader(new FileReader("/..../usertrackplayer.csv"));
FileWriter output = new FileWriter(new File("/.../output.csv"));
String uLine = null;
ArrayList<String> list = new ArrayList<String>();
List<String> arrayList = new ArrayList<String>();
String currentUserId = null;
while ((uLine = userlines.readLine()) != null) {
String[] userData = uLine.split(",");
String userId = userData[0];
if (userId.equals(currentUserId)) {
// Do what ever we need while buffering same userId
}
else{
list.add(uLine);
int division = (int) Math.ceil(list.size()/2);
List<String> sublist = list.subList(0, division); //TO DEVIDE THE DATA OF EACH USERID INTO TWO ALMOST EQUAL PARTS, IT PUT THE FIRST HALF IN SUBLIST
//HERE I NEED TO WRITE THE OUTPUT OF SUBLIST INTO A CSV FILE, BUT HOW????
output.write(sublits); //--> THIS DOES NOT WORK SINCE SUBLIST IS NOT A STRING
currentUserId = userId;
list.clear();
}
list.add(uLine);
}
userlines.close();
output.close();
}
能否有人帮我了解如何将列表字符串中的数据写入空的csv文件?我的第二个问题是:我如何在每个空格处拆分它并将其视为一行。
谢谢。
user1","track1","player1","user1","track2","player2","user1"...
,而我需要它变成"user1","track1","player1" "user1","track2","player2" "user1
。 - mOnainput.split
或input.length()-1
时,我会遇到错误,可能是因为我使用了bufferedreader
读取我的输入csv文件。关于我的输入文件,我从mysql表中导出它,该表与输出文件具有完全相同的列(userId、trackId、playerId)...在输入文件中,行以空格分隔。 - mOna