Jayant Usrete
Jayant Usrete

Reputation: 69

How to read, validate and move a .csv file?

I am new to Java and I got a situation to which I'm clueless about. I need to take all the csv files from a folder read them one by one, validate them for eg. There's data like name, age, email etc. So the name should have only letters, age should be numeric and email should be in valid email format. The file which has invalid data in any of the row, there shouldn't be any further processing of that particular csv file and it should be moved to another folder which will have erroneous csv files and the program will move onto next csv in the folder until all of them gets checked, validated and moved. I don't know how to begin with this. Please help me out guys.

Upvotes: 1

Views: 16706

Answers (2)

Gagan Ahuja
Gagan Ahuja

Reputation: 36

Firstly, save your file to .csv format1. This works on excel sheets. Then call this function in main() by this code, you will read the .csv file row-wise, each cell at a time .

Try this out:

public List<HashMap<String, Object>> convertCSVRecordToList() {

    String csvFile = "your_file_name.csv";
    BufferedReader br = null;
    String line = "";
    String cvsSplitBy = ",";
    HashMap<String, Object> Map = new HashMap<String, Object>();

    List<HashMap<String, Object>> MapList = new ArrayList<HashMap<String, Object>>();

    try {

        br = new BufferedReader(new FileReader(csvFile));

        while ((line = br.readLine()) != null) {

            System.out.println(line);

            String[] data = line.split(cvsSplitBy);         

            Map.put("filed_name", data[3]);
            Map.put("field_name", data[0]);
            Map.put("field_name", data[2]);
            Map.put("fiels_name", data[1]);

            MapList.add(Map);
            Map = new HashMap<String, Object>();                
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
     catch (IOException e) {
        e.printStackTrace();
    }

    return MapList;
}

Upvotes: 2

Herbert Yu
Herbert Yu

Reputation: 111

OK, let's separate this question into following four smaller topics:

  1. Java program to read a folder, result is a list of files
  2. Java program to read a file, result is a list of lines
  3. Java program to parse a line, get a list of columns
  4. For name, age, email, validate the data

Step 1: Java program to read a folder, result is a list of files

Assuming you have below in your top of java program

import java.io.*;
import java.util.*;

Below code should get a list of file in a folder

File f = new File(folder);
File[] fileList = f.listFiles();

Step 2: Java program to read a file, result is a list of lines

String line;
BufferedReader br = new BufferedReader(new FileReader(path));
while ((line = br.readLine()) != null) {
  String l = line.trim(); // Remove end of line. You can print line here.
}
br.close();

Step 3: Java program to parse a line, get a list of columns

String[] columns = l.split(",");  // separate line by comma
for( int i=0; i<columns.length; i++ )
{
    System.out.println(columns[i].trim());// remove space after comma
}

Step 4: Validate e.g. age Age has to be integer so parse it as integer

int age = Integer.parseInt(columns[3].trim());//assuming age at column #3

See another answer come out. That answer doesn't have folder to file loop. Hope this helps.

Upvotes: 4

Related Questions