Java

Moderators: zibadian
Number of threads: 7836
Number of posts: 18235

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
about csv Posted by Tan theng wei on 14 Jan 2007 at 9:03 AM
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();
}
}
}
Report
Re: about csv Posted by zibadian on 14 Jan 2007 at 10:25 AM
: 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.
Report
Re: about csv Posted by Tan theng wei on 15 Jan 2007 at 4:51 AM
Thanks alot,that website is cool!~
i have found the way to do it.Thanks.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.