Results 1 to 2 of 2

Thread: FileIO

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2002
    Posts
    88

    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!

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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){;}
     }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width