java将列表字符串写入csv文件

时间:2016-03-02 13:57:32

标签: java arrays csv writer

我有一些像下面这样的数组字符串(注意每个播放器后面有空格显示不同的行......),

["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文件? 我的第二个问题是:如果有空间我怎么能将它们分开并将其视为一条直线..

谢谢,

1 个答案:

答案 0 :(得分:9)

您是否考虑过使用OpenCSV来编写您的值:http://opencsv.sourceforge.net/

有一个如何编写CSV文件的例子,这很容易。

至于你的问题,似乎你不能写值,因为Arraylist toString方法不会返回逗号分隔的字符串。你可以用这个:

public static void main(String[] args) throws IOException {

    FileWriter writer = new FileWriter("/Users/artur/tmp/csv/sto1.csv");

    List<String> test = new ArrayList<>();
    test.add("Word1");
    test.add("Word2");
    test.add("Word3");
    test.add("Word4");

    String collect = test.stream().collect(Collectors.joining(","));
    System.out.println(collect);

    writer.write(collect);
    writer.close();

}

查看收集器impl - 它将使用您提供的分隔符收集所有String对象并将它们连接起来。然后输出就像:

Word1,Word2,Word3,Word4

我希望能解决你的问题。如果有些事情不清楚,请告诉我。

阿图尔

编辑2,针对您的问题。

如果您输入的是您读取的字符串,则可以先按空格拆分它,使其以分隔的行显示。如果它是一个实际的阵列,它就不会起作用。这是我的例子:

public static void main(String[] args) throws IOException {

        String input = "[\"user1\",\"track1\",\"player1\", \"user1\",\"track2\",\"player2\", \"user1\",\"track3\",\"player3\"]";
        input = input.substring(1, input.length() - 1); // get rid of brackets
        String[] split = input.split(" ");

        FileWriter writer = new FileWriter("/Users/artur/tmp/csv/sto1.csv");

        for(String s : split) {
            String[] split2 = s.split(",");
            writer.write(Arrays.asList(split2).stream().collect(Collectors.joining(",")));
            writer.write("\n"); // newline
        }

        writer.close();

    }

生成的文件如下所示:

"user1","track1","player1"
"user1","track2","player2"
"user1","track3","player3"