0

I am writing an program to select few columns from one csv and write it into another csv. i am able to select the columns i want to write but i am not able to write it into another csv.

public class ReadingCSV {

public static void main(String[] args) {

    String csvFile = "/Users/Desktop/NEW Automation/combined.csv";
    String out = "/Users/Desktop/NEW Automation/a.csv";
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";

    try {

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

            // use comma as separator
            String[] mydata = line.split(cvsSplitBy);
            //System.out.println("[Client = " + mydata[1] + " , Acct_id=" + mydata[2] + "]");
            PrintWriter output = new PrintWriter("out", "UTF-8");

            output.println(mydata[0] + cvsSplitBy + mydata[1] + cvsSplitBy + mydata[2]);
        }

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

}

}

0

1 Answer 1

1

You have to initialize Printwriter outside your while block, and you need to close it in finally.

public static void main(String[] args) {

String csvFile = "/Users/Desktop/NEW Automation/combined.csv";
String out = "/Users/Desktop/NEW Automation/a.csv";
BufferedReader br = null;
String line = "";
String cvsSplitBy = ",";
PrintWriter output = new PrintWriter("out", "UTF-8");
try {


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

        // use comma as separator
        String[] mydata = line.split(cvsSplitBy);
        //System.out.println("[Client = " + mydata[1] + " , Acct_id=" + mydata[2] + "]");

        output.println(mydata[0] + cvsSplitBy + mydata[1] + cvsSplitBy + mydata[2]);
    }

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

I will also suggest to use opensource project http://opencsv.sourceforge.net/ to parse csv instead of writing your own code.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response. But its throwing error in finally block for the output.close(); as output is an unresolved variable. I commented it out and ran the program, its not populating anything in my output csv.
@Aravinda I just updated the code and moved output outside try block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.