I have some array strings like the following (note that there is space after each player which shows different lines...),
["user1","track1","player1", "user1","track2","player2", "user1","track3","player3", ...]
I would like to divide this array into two parts (almost equal) based on the number of lines (spaces here). So, for example if there is 40 spaces in this array, I need to store the lines starting from the first to the half(20th line) in a file (let's call it file A) and put the rest in file B. I could divide the arraylist, but I could not write it in the correct way in the CSV file.
Here is my code:
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();
}
Could someone please help me know how could I write the data in a list string into an empty csv file?? My second question is: how could I split them wherever there is space and consider it as a line..
Thanks,