在java中编写时,只在csv文件中添加了一行

时间:2014-05-13 08:10:49

标签: java csv

我编写了用于在java.But中编写csv文件的代码,我只能编写一行。从第二行开始,行不会被追加。

请检查以下代码以获取csv文件

"1.0.0.0","1.0.0.255","16777216","16777471","AU","Australia"
"1.0.1.0","1.0.3.255","16777472","16778239","CN","China"
"1.0.4.0","1.0.7.255","16778240","16779263","AU","Australia"
"1.0.8.0","1.0.15.255","16779264","16781311","CN","China"
"1.0.16.0","1.0.31.255","16781312","16785407","JP","Japan"
"1.0.32.0","1.0.63.255","16785408","16793599","CN","China"
"1.0.64.0","1.0.127.255","16793600","16809983","JP","Japan"
"1.0.128.0","1.0.255.255","16809984","16842751","TH","Thailand"

用于在java中编写csv代码

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import au.com.bytecode.opencsv.CSVWriter;


public class CSVFileReader {

  public static void main(String[] args) {

    CSVFileReader obj = new CSVFileReader();
    obj.run();

  }

  public void run() {

String csvFile = "C:/Users/piyush bhatia/Desktop/whois.csv";

    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";
    String sFileName="C:/Users/piyush bhatia/Desktop/whois2.csv";

    try {

        br = new BufferedReader(new FileReader(csvFile));
        while ((line = br.readLine()) != null) {

                // use comma as separator
            String[] country = line.split(cvsSplitBy);
                if(line.contains("AU"))
                {
                    FileWriter writer = new FileWriter(sFileName);
                    //writer.append(country[4]);
                    writer.write(country[2]);

                    //writer.append('\n');
                    writer.flush();
                    writer.close();


            System.out.println("Country [code= " + country[2] 
                                 + " , name=" + country[5] + "]");
                }
                else
                {

            System.out.println("");
                }


        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


 writer.flush(); // flush and close only once.
            writer.close();
    }

    System.out.println("Done");
  }

}

当im打印找到AU的值时,代码工作正常。但是当我创建csv文件时,它只附加一行。没有附加找到AU的第二行。创建新的csv文件时,只附加第一个AU行。找到AU的第三行,它没有被打印出来。请指导我......

3 个答案:

答案 0 :(得分:1)

创建传递布尔参数FileWriter的{​​{1}}对象以进行追加:

true

或将此对象的构造(以及冲洗和关闭)移动到循环外部。目前,每次写出一行时,您都会覆盖文件的内容。

其他一些一般性意见:

  • 使用CSV解析库读取您的文件。那里有许多好的例子,例如http://commons.apache.org/proper/commons-csv

  • 为文件编写器使用正确的错误处理。如果您有例外,目前作家不会被关闭。如果您有Java 7 +,请使用try-with-resources语句。

答案 1 :(得分:0)

将您的代码更改为..

public void run(){

    String csvFile = "C:/Users/piyush bhatia/Desktop/whois.csv";

    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";
    String sFileName = "C:/Users/piyush bhatia/Desktop/whois2.csv";
    FileWriter writer = null;
    try {
        writer = new FileWriter(sFileName);
        br = new BufferedReader(new FileReader(csvFile));
        while ((line = br.readLine()) != null) {

            // use comma as separator
            String[] country = line.split(cvsSplitBy);
            if (line.contains("AU")) {

                // writer.append(country[4]);
                writer.write(country[2]);

                System.out.println("Country [code= " + country[2]
                        + " , name=" + country[5] + "]");
            } else {

                System.out.println("");
            }

        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
                writer.flush(); // flush and close only once.
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    System.out.println("Done");
}

答案 2 :(得分:0)

可以为原始文件中的每一行打开编写器,它只是被覆盖了吗?