> Hi All;
Please refrain from multi-posting.
(X-posted to c.l.j.h,p,g w/f-ups to c.l.j.h.)
Andrew T.
> Hi All;
> I am trying to get csv values into a jTable can anyone hlp me
> by sending a sample code or even a code of reading a text file on a
> java interface......thanks.....william
import java.io.*;
//import java.util.*;
import javax.swing.*;
public class HWGuideline {
public static void main(String[] args) {
/*
First you need a filename. Having that, you use FileReader (
http://java.sun.com/j2se/1.5.0/docs/api/java/io/FileReader.html ) to read it.
Now, as CSV file is usualy organized as one-row-per-record, wrapping FileReader
with BufferedReader (
http://java.sun.com/j2se/1.5.0/docs/api/java/io/BufferedReader.html ,
http://java.sun.com/j2se/1.5.0/docs/api/java/io/BufferedReader.html#readLine() )
would come in handy. Being able to read one line/record at a time, you now need
a means of splitting a record into columns/fields. What you need is either a
StringTokenizer (
http://java.sun.com/j2se/1.5.0/docs/api/java/util/StringTokenizer.html ) or a
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#split(java.lang.String)
with a proper regex (^\\t$ perhaps?). Now you pack so obtained data into a two
dimensional array of String[rows][cols] (plus an additional one-dimensional one
with column names) and, feeding that to JTable constructor (
http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JTable.html#JTable(java.lang
.Object[][],%20java.lang.Object[])
), you have an "operational" JTable.
*/
}
}
You should be able to write the actual code yourself. Otherwise, start here:
http://java.sun.com/j2se/1.5.0/docs/index.html
http://java.sun.com/docs/books/tutorial/index.html