Sometime we need to parse CSV(Comma Separated Values) file for data reading or writing .We use CSV file due to platform independence .There are lots of API available to parse a CSV file but here we use simple JAVA code, later we will learn how to use JAVA API for reading and writing a CSV file.
STEP 1 :
Basically we need this lines to read a file.
try (InputStream fileStream = new BufferedInputStream(new FileInputStream(filePath));
STEP 2 :
To read a file with different character set except English then we need to support for UTF-8 characters.So we use Reader class for that.
Reader ir = new InputStreamReader(fileStream, "UTF-8");
STEP 3 :
Now we create a BufferedReader object to read a file in a speedy way.
BufferedReader buffReader = new BufferedReader(ir));
STEP 4 :
We use readLine() method on BufferedReader object and loop will iterate until this object will be null.
while ((line = buffReader.readLine()) != null) {
csvData = line.split(separator);
list.add(csvData);
}
Example Code :
package csvfilehandling;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CSVReadingWithOutAPI {
public static void main(String[] args) {
try {
//System.out.println((getCSVInList("Our CSV File Path", ",").get(0))[1]);
for (String[] csvDataRow : getCSVInList("Our CSV File Path", ",")) {
for (String csvDataOnColumn : csvDataRow) {
System.out.println(csvDataOnColumn);
}
}
//System.out.println(getCSVInMap("Our CSV File Path", ",").get(0).get(1));
for (Map.Entry<Integer, List<String>> csvDataSet : getCSVInMap("Our CSV File Path", ",").entrySet()) {
for (String csvData: csvDataSet.getValue()) {
System.out.println(csvData);
}
}
//System.out.println(getCSVFromColumn("Our CSV File Path", ",", "DATACOLUMN1"));
for (String columndata : getCSVFromColumn("Our CSV File Path", ",", "DATACOLUMN1")) {
System.out.println(columndata);
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
public static List<String[]> getCSVInList(String filePath, String separator) throws IOException {
List<String[]> list = new ArrayList<>();
/**
* We use try-with-resources Statement for close the resource and
* release it .
*/
try (InputStream fileStream = new BufferedInputStream(new FileInputStream(filePath));
Reader ir = new InputStreamReader(fileStream, "UTF-8");
BufferedReader buffReader = new BufferedReader(ir)) {
String line;
String[] csvData;
while ((line = buffReader.readLine()) != null) {
csvData = line.split(separator);
list.add(csvData);
}
}
return list;
}
public static Map<Integer, List<String>> getCSVInMap(String filePath, String separator) throws IOException {
Map<Integer, List<String>> map = new HashMap<>();
/**
* We use try-with-resources Statement for close the resource and
* release it .
*/
try (InputStream fileStream = new BufferedInputStream(new FileInputStream(filePath));
Reader ir = new InputStreamReader(fileStream, "UTF-8");
BufferedReader buffReader = new BufferedReader(ir)) {
String line;
String[] csvData;
int lineNumber = 0;
while ((line = buffReader.readLine()) != null) {
csvData = line.split(separator);
map.put(lineNumber, Arrays.asList(csvData));
lineNumber++;
}
}
return map;
}
public static List<String> getCSVFromColumn(String filePath, String separator, String columnName) throws IOException {
List<String> columnList = new ArrayList<>();
/**
* We use try-with-resources Statement for close the resource and
* release it .
*/
try (InputStream fileStream = new BufferedInputStream(new FileInputStream(filePath));
Reader ir = new InputStreamReader(fileStream, "UTF-8");
BufferedReader buffReader = new BufferedReader(ir)) {
String line;
String[] csvDataInCurrentLine;
int lineNumber = 0;
int columnNumber = 0;
while ((line = buffReader.readLine()) != null) {
csvDataInCurrentLine = line.split(separator);
/**
* This is a one time checking for First Line Reading .
*/
if (lineNumber == 0) {
/**
* Use binarySearch to find the column name from String
* Array String[] csvDataInCurrentLine; .
*/
if (Arrays.binarySearch(csvDataInCurrentLine, columnName) < 0) {
break;
}
columnNumber = Arrays.asList(csvDataInCurrentLine).indexOf(columnName);
} else {
columnList.add(csvDataInCurrentLine[columnNumber]);
}
lineNumber++;
}
}
return columnList;
}
}
STEP 1 :
Basically we need this lines to read a file.
try (InputStream fileStream = new BufferedInputStream(new FileInputStream(filePath));
STEP 2 :
To read a file with different character set except English then we need to support for UTF-8 characters.So we use Reader class for that.
Reader ir = new InputStreamReader(fileStream, "UTF-8");
STEP 3 :
Now we create a BufferedReader object to read a file in a speedy way.
BufferedReader buffReader = new BufferedReader(ir));
STEP 4 :
We use readLine() method on BufferedReader object and loop will iterate until this object will be null.
while ((line = buffReader.readLine()) != null) {
csvData = line.split(separator);
list.add(csvData);
}
Example Code :
package csvfilehandling;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CSVReadingWithOutAPI {
public static void main(String[] args) {
try {
//System.out.println((getCSVInList("Our CSV File Path", ",").get(0))[1]);
for (String[] csvDataRow : getCSVInList("Our CSV File Path", ",")) {
for (String csvDataOnColumn : csvDataRow) {
System.out.println(csvDataOnColumn);
}
}
//System.out.println(getCSVInMap("Our CSV File Path", ",").get(0).get(1));
for (Map.Entry<Integer, List<String>> csvDataSet : getCSVInMap("Our CSV File Path", ",").entrySet()) {
for (String csvData: csvDataSet.getValue()) {
System.out.println(csvData);
}
}
//System.out.println(getCSVFromColumn("Our CSV File Path", ",", "DATACOLUMN1"));
for (String columndata : getCSVFromColumn("Our CSV File Path", ",", "DATACOLUMN1")) {
System.out.println(columndata);
}
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
public static List<String[]> getCSVInList(String filePath, String separator) throws IOException {
List<String[]> list = new ArrayList<>();
/**
* We use try-with-resources Statement for close the resource and
* release it .
*/
try (InputStream fileStream = new BufferedInputStream(new FileInputStream(filePath));
Reader ir = new InputStreamReader(fileStream, "UTF-8");
BufferedReader buffReader = new BufferedReader(ir)) {
String line;
String[] csvData;
while ((line = buffReader.readLine()) != null) {
csvData = line.split(separator);
list.add(csvData);
}
}
return list;
}
public static Map<Integer, List<String>> getCSVInMap(String filePath, String separator) throws IOException {
Map<Integer, List<String>> map = new HashMap<>();
/**
* We use try-with-resources Statement for close the resource and
* release it .
*/
try (InputStream fileStream = new BufferedInputStream(new FileInputStream(filePath));
Reader ir = new InputStreamReader(fileStream, "UTF-8");
BufferedReader buffReader = new BufferedReader(ir)) {
String line;
String[] csvData;
int lineNumber = 0;
while ((line = buffReader.readLine()) != null) {
csvData = line.split(separator);
map.put(lineNumber, Arrays.asList(csvData));
lineNumber++;
}
}
return map;
}
public static List<String> getCSVFromColumn(String filePath, String separator, String columnName) throws IOException {
List<String> columnList = new ArrayList<>();
/**
* We use try-with-resources Statement for close the resource and
* release it .
*/
try (InputStream fileStream = new BufferedInputStream(new FileInputStream(filePath));
Reader ir = new InputStreamReader(fileStream, "UTF-8");
BufferedReader buffReader = new BufferedReader(ir)) {
String line;
String[] csvDataInCurrentLine;
int lineNumber = 0;
int columnNumber = 0;
while ((line = buffReader.readLine()) != null) {
csvDataInCurrentLine = line.split(separator);
/**
* This is a one time checking for First Line Reading .
*/
if (lineNumber == 0) {
/**
* Use binarySearch to find the column name from String
* Array String[] csvDataInCurrentLine; .
*/
if (Arrays.binarySearch(csvDataInCurrentLine, columnName) < 0) {
break;
}
columnNumber = Arrays.asList(csvDataInCurrentLine).indexOf(columnName);
} else {
columnList.add(csvDataInCurrentLine[columnNumber]);
}
lineNumber++;
}
}
return columnList;
}
}
No comments:
Post a Comment