使用Java读写CSV文件

gpuk360

我有一个CSV日志文件,它包含许多这样的行:

2016-06-21 12:00:00,000 : helloworld: header1=2;header2=6;header=0

我想将它们写入新的CSV文件。

public void readLogFile() throws Exception
{
    String currentLine = "";
    String nextLine = "";

    BufferedReader reader = new BufferedReader(new FileReader(file(false)));
    while ((currentLine = reader.readLine()) != null)
    {
        if (currentLine.contains("2016") == true)
        {
            nextLine = reader.readLine();
            if (nextLine.contains("helloworld") == true)
            {                   
                currentLine = currentLine.substring(0, 23);
                nextLine = nextLine.substring(22, nextLine.length());

                String nextBlock = replaceAll(nextLine);
                System.out.println(currentLine + " : helloworld: " + nextBlock);

                String[] data = nextBlock.split(";");
                for (int i = 0, max = data.length; i < max; i++)
                {
                    String[] d = data[i].split("=");
                    map.put(d[0], d[1]);
                }
            }
        }
    }
    reader.close();
}

这是我写内容的方法:

public void writeContentToCsv() throws Exception
{
    FileWriter writer = new FileWriter(".../file_new.csv");
    for (Map.Entry<String, String> entry : map.entrySet())
    {
        writer.append(entry.getKey()).append(";").append(entry.getValue()).append(System.getProperty("line.separator"));
    }
    writer.close();
}

这是我想要的输出:

header1; header2; header3
2;6;0
1;5;1
5;8;8
...

当前,CSV文件如下所示(仅显示一个数据集):

header1;4
header2;0
header3;0

谁能帮我修复代码?

Jaiprakash

创建一个用于存储标头值的类,并将其存储在列表中。遍历列表以保存结果。

当前使用的映射只能存储2个值(正在存储标头值(命名为其对应的值))

map.put(d [0],d [1]); 此处d [0]将为header1,而d [1]将为4(但我们只希望从此处开始4)

    class Headervalues {
    String[] header = new String[3];
}

public void readLogFile() throws Exception
{
    List<HeaderValues> list = new ArrayList<>();
    String currentLine = "";
    BufferedReader reader = new BufferedReader(new FileReader(file(false)));
    while ((currentLine = reader.readLine()) != null)
    {
        if (currentLine.contains("2016") && currentLine.contains("helloworld"))
        {

                String nextBlock = replaceAll(currentLine.substring(22, currentLine.length());

                String[] data = nextBlock.split(";");
                HeaderValues headerValues = new HeaderValues();
                //Assuming data.length will always be 3.
                for (int i = 0, max = data.length; i < max; i++)
                {
                    String[] d = data[i].split("=");
                    //Assuming split will always have size 2
                   headerValues.header[i] = d[1];
                }
                list.add(headerValues)
            }
        }
    }
    reader.close();
}
public void writeContentToCsv() throws Exception
{
    FileWriter writer = new FileWriter(".../file_new.csv");
    for (HeaderValues value : headerValues)
    {
        writer.append(value.header[0]).append(";").append(value.header[1]).append(";").append(value.header[2]);
    }
    writer.close();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用Java 7 nio读写文件

来自分类Dev

使用Java 7 nio读写文件

来自分类Dev

使用Java创建不可删除文件并对其进行读写

来自分类Dev

Java-读写.txt文件

来自分类Dev

如何用Java读写文件

来自分类Dev

Java同步外部文件读写

来自分类Dev

使用python读写.docx文件

来自分类Dev

使用MATLAB读写文件头

来自分类Dev

使用C#读写文件

来自分类Dev

适用于读写文件的Java类?

来自分类Dev

Java向文件读写字节

来自分类Dev

在Java中以十六进制读写文件?

来自分类Dev

解压文件以获得具有读写权限的csv文件

来自分类Dev

使用Windows-1252读写文件

来自分类Dev

打开文件以使用truncate进行读写

来自分类Dev

使用python测量文件的原位读写速度

来自分类Dev

使用node.js读写文件(JSON)

来自分类Dev

如何使用vimscript在文件中读写列表

来自分类Dev

同时使用文件进行读写操作失败

来自分类Dev

使用MFC CFile进行文件读写

来自分类Dev

使用python的多线程读写文件

来自分类Dev

使用PropertyHandler Shell Extension读写文件属性

来自分类Dev

在C中使用libpng读写PNG文件

来自分类Dev

使用python测量文件的原位读写速度

来自分类Dev

使用node.js读写文件(JSON)

来自分类Dev

使用 Python 读写非 .txt 文件

来自分类Dev

如何使用 javascript 在属性文件中读写

来自分类Dev

使用 Files.lines 读写文件流

来自分类Dev

使用Java创建CSV文件