getting the content of a .csv using open csv - java

How to get and display some lines of a csv using openCSV.
I currently have the following code :
CSVReader reader1 = new CSVReader(new FileReader(mydata_csv.getpath()));
List myDatas = reader1.readAll();
How to display one specific line ?
Maybe can I use a better way to store my datas (the csv contains lines of hundreds variables). any suggestion would be welcome.

The documentation for opencsv http://opencsv.sourceforge.net/#how-to-read seems to say that your code returns a list of String[]
in which case I would write it like so:
CSVReader reader1 = new CSVReader(new FileReader(mydata_csv.getpath()));
List<String[]> myDatas = reader1.readAll();
String[] lineI = myDatas.get(i);
for (String[] line : myDatas) {
for (String value : line) {
//do stuff with value
}
}

You should use the following code:
CSVReader reader = new CSVReader(new FileReader(mydata_csv.getpath()));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
System.out.println(nextLine[0] + nextLine[1] + "etc...");
}

Related

How to Convert List<String[]> to Comma Separated String without " " in Java

I'm trying to remove fist line from the CSV file after reading it. Code is working properly but it's adding "" to my data when it's rewriting.
for an example:
before write to csv file: 100,COMRADE,CAMPBELL
after write to csv file: "100","COMRADE","CAMPBELL"
Here is the code.
========================================================================
public void deleteLineFromCsv() throws IOException {
List<PatientsTabData> list = new ArrayList<>();
BufferedReader reader = new BufferedReader(new FileReader(new File("src/main/resources/patients.csv")));
String line = reader.readLine();
while(line != null) {
String[] split = line.split(",");
list.add(new PatientsTabData().patientId(Integer.parseInt(split[0])).patName(split[1]).patLastName(split[2]));
line = reader.readLine();
}
list.remove(0);
CSVWriter writer = new CSVWriter(new FileWriter(new File("src/main/resources/patients.csv")));
List<String[]> newlist = list.stream().map(item -> new String[]
{String.valueOf(item.getPatientId()), item.getPatName(), item.getPatLastName()})
.collect(Collectors.toList());
writer.writeAll(newlist);
writer.close();
}
Library:
<dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>2.3</version>
</dependency>
You can use another constructor when creating your writer. Change
CSVWriter writer = new CSVWriter(new FileWriter(new File("src/main/resources/patients.csv")));
to
CSVWriter writer = new CSVWriter(new FileWriter(new File("src/main/resources/patients.csv")),',',CSVWriter.NO_QUOTE_CHARACTER);
this is the way to convert List to comma Seprated Single String.
List<String[]> oldList= . . . ;
String requiredString = oldList
.stream()
.map(Arrays::asList)
.flatMap(Collection::stream)
.collect(Collectors.joining(","));
Your CSV writer is writing each element as string in the csv file. This can be by design in library as it will keep any comma in your data in the string and later will not add extra column if your data has comma as well.
"name","class","house, street, city","value"
See the documentation of your library and check if it provides the option to not treat every element as string.
But my opinion is to keep the strings in quotes to avoid data descripency during reads.

Object[] cannot be converted to String[]

The goal of my code is to replace a certain text value within my .CSV file with the user input of a text field.
My .CSV file has values delimited by commas: hey,hi. If I'm just wanting to replace 'hey' then I would gather the input from the text field and replace 'hey' with 'bye'. Output: bye,hi.
In my code, I believe I am reading in my file and writing the contents of the file to a list, delimited by commas.
I will then iterate through the list and replace an instance of the user input within the list, with another user input and write it back to file.
However, I cannot write it back to file as I'm getting the Object[] cannot be converted to String[] error. Thus I'm stuck as to how to replace the instance of user input within the text file.
Here's my code:
try{
//Convert user input into strings
String strSerial = editSerialField.getText();
String strLocation = editLocationField.getText();
//Read existing file
CSVReader reader = new CSVReader(new FileReader("test test.txt"), ',');
List myEntries = reader.readAll();
//Iterate through my array
for (int i = 0; i < myEntries.size(); i++)
{
//If an entry matches the user input
if (myEntries.get(i).equals(strSerial))
{
//Set the match to the user input from strLocation
myEntries.set(i, strLocation);
break;
}
}
//Write to existing file
CSVWriter writer = new CSVWriter(new FileWriter("test test.txt"), ',');
//Error is here**********************
//Write the new string with the replaced word OVER the same file
writer.writeNext(myEntries.toArray(new String[myEntries.size()]));
writer.close();
}catch (IOException e)
{
System.out.println(e);
}
}
How do I modify my code so that it writes my changes to the .CSV file?
For a start writeNext will write on line at a time, so you need to loop.
Secondly consider using not a raw List but using generics.
Thirdly, it may be cleaner to write as you go
and lastly, each line will contain an Array of Strings
consider this code (not tested)
CSVReader reader = new CSVReader(new FileReader("test test.txt"), ',');
List<String []> myEntries = reader.readAll();
reader.close ();
CSVWriter writer = new CSVWriter(new FileWriter("test test.txt"), ',');
//Iterate through my array
for (String [] line : myEntries)
{
ArrayList<String> newLine = new ArrayList <String>();
for (String word : line) {
{
String newVal = word.replace(strSerial, strLocation);
newLine.add (newVal);
}
writer.writeNext(newLine.toArray(new String[newLine.size()]));
}
Your problem is/ starts at this line:
List myEntries = reader.readAll();
I assume that you did not noticed that the return type of the method readAll() is
List<String[]>
If for example your test file looks like :
hey, hi
hallo, hello
sth, sthelse
After calling readAll() your variable myEntries will be a list of string arrays; each array representing each row in your file and each string from that row as element of the array
myEntries : [hey, hi]
[hallo, hello]
[sth, sthelse]
Keeping this in mind the next issue is
if (myEntries.get(i).equals(strSerial))
where you try to compare a String[] with a String which will not be true.
Try it as follows :
try{
//Convert user input into strings
String strSerial = editSerialField.getText();
String strLocation = editLocationField.getText();
CSVReader reader = new CSVReader(new FileReader("test test.txt"), ',');
// valid but not a good practice how you declare your variable before
// List myEntries = reader.readAll(); // List of what??
List<String[]> myEntries = reader.readAll();
for (String[] row : myEntries){ // go through each array from your list representing a row in your file
for (int i = 0; i < row.length; i++){ //go through each element of that array
if (row[i].equalsIgnoreCase(strSerial)){
row[i] = strLocation;
}
}
}
//add the parameter CSVWriter.NO_QUOTE_CHARACTER to prevent opencsv from writing quotes to file
CSVWriter writer = new CSVWriter(new FileWriter("test test.txt"), ',',CSVWriter.NO_QUOTE_CHARACTER);
for (String[] row : myEntries){
writer.writeNext(row);
}
writer.close();
}catch (IOException e){
System.out.println(e);
}
Not really important but i would prefer test.csv instead of test.txt as file name as long as you store comma-separated values in there.

Parsing txt file which has delimiters as part of column value using opencsv reader

I have a tab delimited text file which I want to parse using openscsv and upload to a database. I used CSVReader() to parse the file. The problem is, some column values have tabs within. For instance, a column ends with a tab, and then it has another tab which is used for separating it from the next column.
I'm having trouble in parsing this file. How do I avoid delimiters which are as part of the value?
This is the file I'm trying to parse. Each line has 2 columns and there are 5 rows in total. The first row is the header. However, when I parse it using the following code, I get only 3 rows:
CSVReader reader = new CSVReader(new FileReader("input.txt"), '\t');
String[] nextLine;
int cnt = 0;
while ((nextLine = reader.readNext()) != null) {
if (nextLine != null) {
cnt++;
System.out.println("Length of row "+cnt+" = "+nextLine.length);
System.out.println(Arrays.toString(nextLine));
}
}
******** Update ********
Doing a normal readline such as below prints 5 lines:
BufferedReader br = new BufferedReader(new FileReader("input.txt"));
int lines = 0;
while(br.readLine() != null){
lines++;
}
System.out.println(lines);
Put quotes on your data - here is a modified unit test from CSVReaderTest that shows quotes will work:
#Test
public void testSkippingLinesWithDifferentEscape() throws IOException
{
StringBuilder sb = new StringBuilder(CSVParser.INITIAL_READ_SIZE);
sb.append("Skip this line?t with tab").append("\n"); // should skip this
sb.append("And this line too").append("\n"); // and this
sb.append("a\t'b\tb\tb'\t'c'").append("\n"); // single quoted elements
CSVReader c = new CSVReader(new StringReader(sb.toString()), '\t', '\'', '?', 2);
String[] nextLine = c.readNext();
assertEquals(3, nextLine.length);
assertEquals("a", nextLine[0]);
assertEquals("b\tb\tb", nextLine[1]);
assertEquals("c", nextLine[2]);
}
If that does not work please post some of the lines from your input.txt. When I click on the link it takes me to some website trying to sell me a dropbox clone.

Searching Using opencsv

I wanted to know, if we can search a particular row in a csv (as we do in UI using find) containing a particular word. Does opencsv provide this functionality ?
If not, what is the best way to search in csv file.
No it doesn't but you could simply iterate through the fields
CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] nextLine;
String searchWord = ".*\\d+.*"; // field contains integer?
while ((nextLine = reader.readNext()) != null) {
for (String field: nextLine) {
if (field.matches(searchWord)) {
// matched word...
}
}
}

How to read a String (file) to array in java

Suppose there is a file named as SUN.txt
File contains : a,b,dd,ss,
I want to make dynamic array depending upon the number of attributes in file.
If ther is a char after comma then array will be of 0-4 i.e of length 5.
In the above mentioned case there is no Char which returns 0-3 Array of length 4. I want to read the NULL after comma too.
How do i do that?
Sundhas
You should think about
Reading the file into a String
Splitting the file by separator ','
Using a list for adding the characters and convert the list to an array, when the list is filled
As Markus said, you want to do something like this..
//Create a buffred reader so that you can read in the file
BufferedReader reader = new BufferedReader(new FileReader(new File(
"\\SUN.txt")));
//The StringBuffer will be used to create a string if your file has multiple lines
StringBuffer sb = new StringBuffer();
String line;
while((line = reader.readLine())!= null)
{
sb.append(line);
}
//We now split the line on the "," to get a string array of the values
String [] store = sb.toString().split(",");
I do not quite understand why you would want the NULL after the comma? I am assuming that you mean after the last comma you would like that to be null in your array? I do not quite see the point in that but that is not what the question is.
If that is the case you wont read in a NULL, if after the comma there was a space, you could read that in.
If you would like a NULL you would have to add it in yourself at the end so you could do something like
//Create a buffred reader so that you can read in the file
BufferedReader reader = new BufferedReader(new FileReader(new File(
"\\SUN.txt")));
//Use an arraylist to store the values including nulls
ArrayList<String> store = new ArrayList<String>();
String line;
while((line = reader.readLine())!= null)
{
String [] splitLine = line.split(",");
for(String x : splitLine)
{
store.add(line);
}
//This tests to see if the last character of the line is , and will add a null into the array list
if(line.endsWith(","))
store.add(null);
}
String [] storeWithNull = store.toArray();
Well if you want want to simply open the file and store the content in a array of string then
1) open the file into a string
2) split the string using a regex "," http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#split(java.lang.String)
but I'm curious why you can't use a String file directly ?
For your datatructure, use a list of arrays. Each list entry is a line of your textfile, each entry is an array that holds the comma separated values:
List<String[]> data = new ArrayList<String[]>();
String line = readNextLine(); // custom method, to be implemented
while (line != null) {
data.add(line.split(","));
line = readNextLine();
}
(assuming, your file contains 1..n lines of comma separated values)
You may want to have it like this:
"a,b,c,d," -> {"a", "b", "c", "d", null}
Here's a suggestion how to solve that problem:
List<String[]> data = new ArrayList<String[]>();
String line = readNextLine(); // custom method, to be implemented
while (line != null) {
String[] values = new String[5];
String[] pieces = line.split(",");
for (int i = 0; i<pieces.length; i++)
values[i] = pieces[i];
data.add(values);
line = readNextLine();
}
its seems like a CSV file something like this will work assuming it has 5 lines and 5 values
String [][] value = new String [5][5];
File file = new File("SUN.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line = null;
int row = 0;
int col = 0;
while((line = br.readLine()) != null ){
StringTokenizer s = new StringTokenizer(line,",");
while (s.hasMoreTokens()){
value[row][col] = s.nextToken();
col++;
}
col = 0;
row++;
}
i havent tested this code
Read the file, using BufferedReader, one line at the time.
Use split(",", -1) to convert to an array of String[] including also empty strings beyond the last comma as part of your array.
Load the String[] parts into a List.

Categories