Показать сообщение отдельно
Старый 24.05.2005, 12:57     # 1
Sam Dark
Member
 
Аватар для Sam Dark
 
Регистрация: 03.07.2003
Адрес: Voronezh, Russia
Пол: Male
Сообщения: 294

Sam Dark МолодецSam Dark МолодецSam Dark Молодец
Question Java. Считать html блоками.

На Java писать только начал.

Задача: считать html блоками (<блок>блок</блок>).

Начал делать так:

Код:
public class block
{
  public static String text;
  public static String type;
  
  public block(String txt, String typ)
  {
    text=txt;
    type=typ;
  }
}
Код:
import java.io.*;

public class blockreader{
  private static boolean flag;
  private static int pos;
  private static char buf;
  public static block block;
  private static InputStream fstream;
  
  public blockreader(String file)  {
    try{
      fstream = new FileInputStream(file);
      pos = 0;
      flag=true;
    }
    catch (FileNotFoundException e){
        System.out.println("File " + file + " not found.");
        flag=false;
    }
  }
  
  public static boolean readblock(){
  if (flag){
    try{
        if (fstream.available()>0){
          buf=(char) fstream.read();
          if ((buf!=13) && (buf!=10)){
            if (buf=='<'){
              block = new block("","tag");
              buf=(char) fstream.read();
              if (fstream.available()>0){
                if (buf=='/'){
                  block.type="ctag";
                }
                else{
                  block.text=block.text+buf;
                }
              }
              while (fstream.available()>0){
                buf=(char) fstream.read();
                if (buf=='>'){break;}
                else{
                  block.text=block.text+buf;
                }
              }
              return true;
            }
            else{
              block = new block(""+buf,"text");
              while (fstream.available()>0){
                buf=(char) fstream.read();
                if (buf=='<'){
                  block.type="tag";
                  break;
                }
                else{
                  block.text=block.text+buf;
                }
              }
              return true;
            }
          }
          //!
          return true;
        }
        else{
          fstream.close();
          return false;
          //eof
        }
      }
      catch (Exception e){
        e.printStackTrace();
        return false;
      }
    }
    else
    {
      return false;
    }
  }
}
Код:
public class main
{     
  public static void main(String args[])
  {
    RusTxt rus = new RusTxt();
    if( args.length == 0 )
    {
      System.out.println("No filename.");
      return;
    }
    else
    {
      blockreader bl = new blockreader(args[0]);
      //read all blocks
      while (bl.readblock())
      {
        System.out.println( bl.block.type + "-" + bl.block.text + "\n" );
      }
    }
  }
}
Понимаю, что всё плохо. Подскажите как лучше сделать.
__________________
Это жжжж неспроста...

Последний раз редактировалось Sam Dark; 24.05.2005 в 12:59.
Sam Dark вне форума