Hello,
I am very new to JAVA.
I would like to know, how to read *.csv file from java and how to save that data into an array.
regards,
bela
22 20524
Hello,
I am very new to JAVA.
I would like to know, how to read *.csv file from java and how to save that data into an array.
regards,
bela
Hi,
First download the javacsv.jar. After that read a bit about csvReader API. http://opencsv.sourceforge.net/api/a...CSVReader.html
Hi,
First download the javacsv.jar. After that read a bit about csvReader API. http://opencsv.sourceforge.net/api/a...CSVReader.html
Your answer is really Impressive.
As soon as I saw this I went through Test it.
But failed.
Actually what happened, I downloaded that Jar and made my NetBeans identifies that Jar.
But this line is not working as mentioned in your Link. -
import au.com.bytecode.opencsv.*;
-
Please help.
Kind regards,
Dmjpro.
Your answer is really Impressive.
As soon as I saw this I went through Test it.
But failed.
Actually what happened, I downloaded that Jar and made my NetBeans identifies that Jar.
But this line is not working as mentioned in your Link. -
import au.com.bytecode.opencsv.*;
-
Please help.
Kind regards,
Dmjpro.
Sorry I got it.
Actually what it mentioned in your link that the CSVReader class is under
au.com.bytecode.opencsv package.
But what I saw in NetBeans is, com.csvreader.
Anyway, I think it is Jar specific.
Kind regards,
Dmjpro.
Your answer is really Impressive.
As soon as I saw this I went through Test it.
But failed.
Actually what happened, I downloaded that Jar and made my NetBeans identifies that Jar.
But this line is not working as mentioned in your Link. -
import au.com.bytecode.opencsv.*;
-
Please help.
Kind regards,
Dmjpro.
Hi,
I think u have not put the jar at right place. Put that jar in Libraries folder of ur project. That line should work. U can try with this line also -
-
import com.csvreader.CsvReader;
-
and use the methods given this class to read the CSV file.
Hi,
I think u have not put the jar at right place. Put that jar in Libraries folder of ur project. That line should work. U can try with this line also -
-
import com.csvreader.CsvReader;
-
and use the methods given this class to read the CSV file.
I think you didn't see my Last Post.
I posted my solution there.
Actually, I used the package what is mentioned in your link.
Check that link.
Kind regards,
Dmjpro.
I think you didn't see my Last Post.
I posted my solution there.
Actually, I used the package what is mentioned in your link.
Check that link.
Kind regards,
Dmjpro.
Hi,
Check with this link again .... http://javacsv.sourceforge.net/com/c...e-summary.html
This is the different and right one.
I think you didn't see my Last Post.
I posted my solution there.
Actually, I used the package what is mentioned in your link.
Check that link.
Kind regards,
Dmjpro.
Just for the fun of it I downloaded and installed the .jar and api docs. After reading
the API docs and the example code (AddressExample.java: the demo reads
the addresses.csv file) I decided to add the .jar to my classpath and it just
worked (as it should). Please read before you simply click away.
kind regards,
Jos
Just for the fun of it I downloaded and installed the .jar and api docs. After reading
the API docs and the example code (AddressExample.java: the demo reads
the addresses.csv file) I decided to add the .jar to my classpath and it just
worked (as it should). Please read before you simply click away.
kind regards,
Jos
Done it too.
The reading part didn't kill me either.
Done it too.
The reading part didn't kill me either.
Of course not; why should it? The magic word is reading again.
kind regards,
Jos
thank you.
But I'm not able to convert in / store as in array form.
regards,
bela
Hi,
Show us a bit what u have tried ? Then only we vil be able to help you.
Hi, I tried an Example but am not able to read the file.
My Code is,
mport com.csvreader.CsvReader;
import java.io.*;
class csveg
{
public static void main(String a[])
{
try
{
CsvReader cs=new CsvReader("hi.csv");
System.out.println("hiii "+ cs);
System.out.println("hiii "+ cs.getColumnCount());//Prints 0
String arra[]=cs.getValues();
System.out.println("hiii "+arra.length);//Prints 0
for(int j=0;j<arra.length;j++)
{
System.out.println("hiii "+j);
System.out.println(arra[j]);
}
}catch(Exception e){System.out.println("Exception is ");}
}
}
pls help...
Hi, I tried an Example but am not able to read the file.
My Code is,
mport com.csvreader.CsvReader;
import java.io.*;
class csveg
{
public static void main(String a[])
{
try
{
CsvReader cs=new CsvReader("hi.csv");
System.out.println("hiii "+ cs);
System.out.println("hiii "+ cs.getColumnCount());//Prints 0
String arra[]=cs.getValues();
System.out.println("hiii "+arra.length);//Prints 0
for(int j=0;j<arra.length;j++)
{
System.out.println("hiii "+j);
System.out.println(arra[j]);
}
}catch(Exception e){System.out.println("Exception is ");}
}
}
pls help...
Hi,
If u hav not read the API for method getColumnCount() .. then check it again and try understand what it is saying ....
It is saying that "Gets the count of columns found in this record".
Here U hav get the file in the csvReader but havn't started reading it ... So start reading the file then only this method vil give u the column count for the particular record u r reading.
I hope u r getting my point.
Hi,
Show us a bit what u have tried ? Then only we vil be able to help you.
yes, my code is :
import java.io.*;
import au.com.bytecode.opencsv.CSVReader;
public class csv2array {
private static final String data = "e:\\weather.csv";
public static void main(String[] args) throws IOException {
CSVReader reader = new CSVReader(new FileReader(data),',','\'',1);
String [] nextLine;
int cnt=0;
String[][] str1= new String[14][5];
while ((nextLine = reader.readNext()) != null) {
System.out.println("Row no is : " + cnt);
System.out.println(nextLine[0]+',' + nextLine[1]+','+ nextLine[2]+',' +nextLine[3]+','+nextLine[4]);
for(int n=0;n<14;n++){
for(int rowelement=0;rowelement<5;rowelement++){
str1[n][rowelement]=nextLine[rowelement];
}
}
cnt++;
}
}
}
weather.csv is available on WEKA.
I'm taking data in matrix str1.
How to see the containts of str1?
when I tried, System.out.println(str1); it gives address of str1.
regards,
bela
yes, my code is :
import java.io.*;
import au.com.bytecode.opencsv.CSVReader;
public class csv2array {
private static final String data = "e:\\weather.csv";
public static void main(String[] args) throws IOException {
CSVReader reader = new CSVReader(new FileReader(data),',','\'',1);
String [] nextLine;
int cnt=0;
String[][] str1= new String[14][5];
while ((nextLine = reader.readNext()) != null) {
System.out.println("Row no is : " + cnt);
System.out.println(nextLine[0]+',' + nextLine[1]+','+ nextLine[2]+',' +nextLine[3]+','+nextLine[4]);
for(int n=0;n<14;n++){
for(int rowelement=0;rowelement<5;rowelement++){
str1[n][rowelement]=nextLine[rowelement];
}
}
cnt++;
}
}
}
weather.csv is available on WEKA.
I'm taking data in matrix str1.
How to see the containts of str1?
when I tried, System.out.println(str1); it gives address of str1.
regards,
bela
Hi,
If u hav done all this hard work urself then printing array values should not be a problem :) ur str1 is a two dimensional array. For reading values in that u hav to put two for loops like.. -
for(int i = 0; i < 14; i++) {
-
for(int j = 0; j < 5; j++) {//obviously u can get row and column lenghts by using str1.length.
-
System.out.println(str1[i][j]);
-
}
-
}
-
Hi,
If u hav done all this hard work urself then printing array values should not be a problem :) ur str1 is a two dimensional array. For reading values in that u hav to put two for loops like.. -
for(int i = 0; i < 14; i++) {
-
for(int j = 0; j < 5; j++) {//obviously u can get row and column lenghts by using str1.length.
-
System.out.println(str1[i][j]);
-
}
-
}
-
yes, this I've tried, and also know this method. But here it prints element by element. my final aim is to make new matrix by extracting few rows and column of this str1 matrix, where I need entire row/ entire column.
using for loops, I can do it,
but I was searching for different method if there is as this will be lengthy procedure.
I have to create those many arrays/matrices.
Do you know any other method?
Hi,
If u hav not read the API for method getColumnCount() .. then check it again and try understand what it is saying ....
It is saying that "Gets the count of columns found in this record".
Here U hav get the file in the csvReader but havn't started reading it ... So start reading the file then only this method vil give u the column count for the particular record u r reading.
I hope u r getting my point.
Ya got it.. Now its executing.. Thnks
Ya got it.. Now its executing.. Thnks
Hi,
Ofcourse Welcome Buddy :)
yes, this I've tried, and also know this method. But here it prints element by element. my final aim is to make new matrix by extracting few rows and column of this str1 matrix, where I need entire row/ entire column.
using for loops, I can do it,
but I was searching for different method if there is as this will be lengthy procedure.
I have to create those many arrays/matrices.
Do you know any other method?
Hi,
Check my post #7. Go to that link. That CsvReader API has more methods than the one which u are using. By Using methods of this u can skip the records which u don't want to read.
Tip: Dont be rude while asking, sharing or answering. Always be thankful :)
Hi,
Check my post #7. Go to that link. That CsvReader API has more methods than the one which u are using. By Using methods of this u can skip the records which u don't want to read.
Tip: Dont be rude while asking, sharing or answering. Always be thankful :)
hello,
thank you.
I'll go through it.
regards,
bela
Hi,
Ofcourse Welcome Buddy :)
one more query :-)
I used parse mtd of CsvReader and created a new CsvReader obj.
If i want to write this to a file, what should i do??
pls help...
one more query :-)
I used parse mtd of CsvReader and created a new CsvReader obj.
If i want to write this to a file, what should i do??
pls help...
Hi,
Again U have to read a bit for that :) Use CsvWriter class for that. Here is the link for that class API... http://javacsv.sourceforge.net/com/c...CsvWriter.html
start using methods of it and have fun :)
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
8 posts
views
Thread by Chris |
last post: by
|
2 posts
views
Thread by Jim Richards |
last post: by
|
19 posts
views
Thread by ranjeet |
last post: by
|
7 posts
views
Thread by Naren |
last post: by
|
6 posts
views
Thread by comp.lang.php |
last post: by
|
9 posts
views
Thread by Adi |
last post: by
|
3 posts
views
Thread by KWienhold |
last post: by
| |
14 posts
views
Thread by chance |
last post: by
|
11 posts
views
Thread by a |
last post: by
| | | | | | | | | | |