[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 163
  • Last Modified:

streamtokeniser ? how to

hi ,
I have some records in file like this

$$$$$ INFORMATION RECORD $$$$$
name1=value1
name2=value2


$$$$$ RECORD $$$$$
name=value
name=value


$$$$$ RECORD $$$$$
name=value
name=value

I want to strip off the header and put the 'name=value' for all records except the Information record to another file.
Can ypu please help! I can't understand streamtokenizer class at all,it complicated !
please tell me fast as I have to give it right now and i am still finding .

Thank you
0
hey_hey
Asked:
hey_hey
  • 3
1 Solution
 
rainmalCommented:
post code...
0
 
rainmalCommented:
post code...
0
 
rainmalCommented:
Heres some working code maybe you'd like to try out.

I hope I'm not working on you school assignments here!


import java.io.*;

public class Parser
{
      static
      {
            try
            {
                  PrintStream f = new PrintStream(new FileOutputStream("c:/log.log"),false);
                  System.setOut(f);
                  System.setErr(f);
            }
            catch(Exception e)
            {}
      }
      
      static StreamTokenizer st=null;
      static String newLine="\n";
      static boolean isFirst=true,isInfo=false;
      static String RECD="$$$$$ ";
      static String INFORMATION="INFORMATION";
      static StringBuffer strBuff=new StringBuffer();
      static String token=null;
      
      public static void main (String[] args)throws Exception
      {
        try
        {
                  //DataInputStream din=new DataInputStream(new FileInputStream(args[0]));
                  DataInputStream din=new DataInputStream(new FileInputStream("c:/sample.txt"));
                  byte [] data=new byte[din.available()];
                  din.readFully(data);
                  din.close();
                  
                  String dataStr=new String(data);                  
                  StringReader br=new StringReader(dataStr);
                  st = new StreamTokenizer(br);
                  
                  st.resetSyntax();
                  st.eolIsSignificant(false);
                  st.wordChars(32,126);
                  st.slashSlashComments(false);
                  st.slashStarComments(false);

                  String record=null;
                  
                  while((record=getTokens())!=null)
                  {
                        System.out.println("Record='\n"+record+"'");
                  }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
      }
      
      public static final String getTokens()throws Exception
      {
            strBuff.setLength(0);
        int tok=0;            
            
        while((tok = st.nextToken()) != StreamTokenizer.TT_EOF)
        {
            if(tok == StreamTokenizer.TT_WORD)
                  {
                        token=st.sval;

                        if(!isInfo)
                        {
                              if((token.indexOf(RECD)!=-1)&&(token.indexOf(INFORMATION)!=-1))
                              {
                                    isInfo=true;
                                    continue;
                              }

                              if(!token.startsWith(RECD))
                              {
                                    strBuff.append(token+newLine);
                              }
                              else if(!isFirst)
                              {
                                    break;
                              }
                              else if(isFirst)
                              {
                                    isFirst=false;
                              }
                        }
                        else if((token.indexOf(RECD)!=-1)&&(token.indexOf(INFORMATION)==-1))
                        {      
                              isInfo=false;
                              isFirst=false;                              
                        }
                        
                        token=null;
                  }
        }

            if(strBuff.length()==0)
            {      
                  return null;                  
            }
            else
                  return strBuff.toString();
      }
}
0
 
hey_heyAuthor Commented:
thansk you ,it worked perfectly and it's not homework!
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now