-
FileIO
Hi
let say i have a sample.txt file which looks like this,
Item NoSold UnitPrice
jean 3 $49.90
shirt 5 $19.90
each item is separated by a tab
how to use FileInputStream to read into a GUI app without the 1st line which is the description of items and how to detect a tab?
please show me the codes, i have a test on this topic thanks!
-
Quick overview of the code. The first line is skipped by the first call to readLine(). Each line read is appended to a StringBuffer with a tab placed after each line. The tab is needed because when the Strings are appended to the StringBuffer no spaces will be added. Then the StringBuffer is cut into tokens which are blank spaces or tabs.
Code:
import java.io.*;
import java.util.*;
public class Example121{
public static void main(String[] args){
try{
File f = new File("C:" + File.separator + "Java" + File.separator + "Test.txt");
BufferedReader buff = new BufferedReader(new FileReader(f));
String firstline = buff.readLine();
StringBuffer sb = new StringBuffer();
while(true){
String s = buff.readLine();
if(s == null) break;
sb.append(s);
sb.append('\t');
}
StringTokenizer st = new StringTokenizer(sb.toString());
while(st.hasMoreTokens()){
System.out.println(st.nextToken());
}
}catch(IOException e){;}
}
}