Saturday, 17 December 2016

How to read CSV File in Java

Here we can learn how to read CSV file comma seprated in java below is the code

--> Use apache API to read CSV or use BufferReader
--> here i have used Buffer Reader


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
 *
 * @author anand
 *
 */
public class ReadCSVFile {

    public static void main(String[] args) {

     
        BufferedReader br = null;
        String line = "";
        String cvsSplitBy = ","; //Comma seprated

        try {

            br = new BufferedReader(new FileReader("Your_CSV_FILE_NAME"));
            while ((line = br.readLine()) != null) {
                System.out.println("Content \t"+line);

            }

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

    }}

No comments:

Post a Comment

Map class field to map

 import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; public class AutogeneratedClassMapper {     public static M...