質問

I wrote the code for writing the csv file in java.But I am able to write only one row.From second row onwards,rows are not getting appended.

please check the code below for csv file

"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"

For writing csv code in java

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");
  }

}

Code is working fine when I am printing values where AU is found.But when I am creating csv file,it is appending only one row.It is not appending the second row where AU is found.While creating a new csv file,appending only first AU row.Third row where AU is found,its not getting printed.Please guide me on this...

役に立ちましたか?

解決 2

Change your code to ..

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");
}

他のヒント

Either create your FileWriter object passing in a boolean parameter true for appending:

FileWriter writer = new FileWriter(sFileName, true);

or move the construction of this object (and the flushing and closing) outside your loop. Currently you are overwriting the contents of the file each time your write out one line to it.

Some other general comments:

  • Use a CSV parsing library to read your files. There are many good examples out there, e.g. http://commons.apache.org/proper/commons-csv.

  • Use correct error handling for your file writer. Currently the writer won't be closed if you have an exception. Use try-with-resources statements if you have Java 7+.

Can it be that you open writer for each line in original file, and its just overwtritten?

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top