about csv - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

about csv

Tan theng weiTan theng wei Posts: 39Member
hi ,anyone can teach me how to read a row of data from a text file?
i need to read the record in row and each row and each row have different type record,so i need to read the row and check which category it belongs to and only i add to the class.eg book ,tv,radio are record are added to different class.

i need some example code about read from the text file.and the record are store in csv format.
is this a way to do it ?

import java.io.*;

class ReadFromDesktop {
public static void main(String [] args)throws IOException
{
String fs = java.io.File.separator;
String ls = java.lang.System.getProperty("line.separator");

File filePath = new File("C:"+fs+"Documents and Settings"+fs+"shiro"+fs+"Desktop"+fs+"readFromHere.txt");

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

String data;
data = br.readLine();
while(data!=null)
{
System.out.println(data);
data=br.readLine();
}
}
}

Comments

  • zibadianzibadian Posts: 6,349Member
    : hi ,anyone can teach me how to read a row of data from a text file?
    : i need to read the record in row and each row and each row have different type record,so i need to read the row and check which category it belongs to and only i add to the class.eg book ,tv,radio are record are added to different class.
    :
    : i need some example code about read from the text file.and the record are store in csv format.
    : is this a way to do it ?
    :
    : import java.io.*;
    :
    : class ReadFromDesktop {
    : public static void main(String [] args)throws IOException
    : {
    : String fs = java.io.File.separator;
    : String ls = java.lang.System.getProperty("line.separator");
    :
    : File filePath = new File("C:"+fs+"Documents and Settings"+fs+"shiro"+fs+"Desktop"+fs+"readFromHere.txt");
    :
    : BufferedReader br = new BufferedReader(new FileReader(filePath));
    :
    : String data;
    : data = br.readLine();
    : while(data!=null)
    : {
    : System.out.println(data);
    : data=br.readLine();
    : }
    : }
    : }
    :
    I see you already read the rows from the file. Splitting the string into an array of strings can be done using the String.split() method, see http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#split(java.lang.String) for more info and example.
    From there you can convert the individual string parts into the record fields.
  • Tan theng weiTan theng wei Posts: 39Member
    Thanks alot,that website is cool!~
    i have found the way to do it.Thanks.
Sign In or Register to comment.