473,231 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

read csv file and make array

13
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
Sep 5 '07 #1
22 20689
madhoriya22
252 100+
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
Sep 5 '07 #2
dmjpro
2,476 2GB
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.
Expand|Select|Wrap|Line Numbers
  1. import au.com.bytecode.opencsv.*;
  2.  
Please help.

Kind regards,
Dmjpro.
Sep 5 '07 #3
dmjpro
2,476 2GB
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.
Expand|Select|Wrap|Line Numbers
  1. import au.com.bytecode.opencsv.*;
  2.  
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.
Sep 5 '07 #4
madhoriya22
252 100+
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.
Expand|Select|Wrap|Line Numbers
  1. import au.com.bytecode.opencsv.*;
  2.  
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
Expand|Select|Wrap|Line Numbers
  1.  
  2. import com.csvreader.CsvReader;
  3.  
and use the methods given this class to read the CSV file.
Sep 5 '07 #5
dmjpro
2,476 2GB
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
Expand|Select|Wrap|Line Numbers
  1.  
  2. import com.csvreader.CsvReader;
  3.  
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.
Sep 5 '07 #6
madhoriya22
252 100+
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.
Sep 5 '07 #7
JosAH
11,448 Expert 8TB
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
Sep 5 '07 #8
r035198x
13,262 8TB
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.
Sep 5 '07 #9
JosAH
11,448 Expert 8TB
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
Sep 5 '07 #10
bela
13
Hi,
First download the javacsv.jar. After that read a bit about csvReader API.
http://opencsv.sourceforge.net/api/a...CSVReader.html

thank you.
But I'm not able to convert in / store as in array form.

regards,
bela
Sep 5 '07 #11
madhoriya22
252 100+
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.
Sep 5 '07 #12
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...
Sep 5 '07 #13
madhoriya22
252 100+
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.
Sep 5 '07 #14
bela
13
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
Sep 6 '07 #15
madhoriya22
252 100+
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..
Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < 14; i++) {
  2.     for(int j = 0; j < 5; j++) {//obviously u can get row and column lenghts by using str1.length.
  3.      System.out.println(str1[i][j]);
  4.     }
  5. }
  6.  
Sep 6 '07 #16
bela
13
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..
Expand|Select|Wrap|Line Numbers
  1. for(int i = 0; i < 14; i++) {
  2.     for(int j = 0; j < 5; j++) {//obviously u can get row and column lenghts by using str1.length.
  3.      System.out.println(str1[i][j]);
  4.     }
  5. }
  6.  
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?
Sep 6 '07 #17
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
Sep 6 '07 #18
madhoriya22
252 100+
Ya got it.. Now its executing.. Thnks
Hi,
Ofcourse Welcome Buddy :)
Sep 6 '07 #19
madhoriya22
252 100+
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 :)
Sep 6 '07 #20
bela
13
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
Sep 6 '07 #21
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...
Sep 6 '07 #22
madhoriya22
252 100+
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 :)
Sep 6 '07 #23

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Chris | last post by:
Can anybody help. I need to read a txt file backwords line by line. Can anybody help me do this. Thanks Chris
2
by: Jim Richards | last post by:
I have been told by a local PC club technician that 98SE cannot read NTFS drives in a network. Is this true? TIA, Jim.
19
by: ranjeet | last post by:
Hay Guys can you all suggest me the points on the below issue Problem : The thing is that I have the data some thing like this. 1486, 2168, 3751, 9074, 12134, 13944, 17983, 19173, 21190,...
7
by: Naren | last post by:
Hello All, Can any one help me in this file read problem. #include <stdio.h> int main() {
6
by: comp.lang.php | last post by:
if (!function_exists('bigfile')) { /** * Works like file() in PHP except that it will work more efficiently with very large files * * @access public * @param mixed $fullFilePath * @return...
9
by: Adi | last post by:
Hello eveyone, I wanna ask a very simple question here (as it was quite disturbing me for a long time.) My problem is to read a file line by line. I've tried following implementations but still...
3
by: KWienhold | last post by:
I'm currently writing an application (using Visual Studio 2003 SP1 and C#) that stores files and additional information in a single compound file using IStorage/IStream. Since files in a compound...
6
by: xdeath | last post by:
Hi guys, i've currently got an assignment, whereby, im supposed to create 2 classes, a Vehicle superclass, and a Taxi subclass. Vehicle class needs to have Reg Number, model, price, and Taxi is...
14
by: chance | last post by:
Hello, I have a file on disk called TEMP.ZIP and I would like to somehow get this into a memory stream so I can eventually do this: row = dataStream.ToArray() However, I am not sure of the...
11
by: a | last post by:
To solve the 100k * 100k data, I finally adopt the file read method and use malloc. The system somehow knows to use virtual memory. Now I first test 1k * 1k data but something uninterpretable...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.