Java by API/java.io/StreamTokenizer — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 14:32, 31 мая 2010

new StreamTokenizer(FileReader fileReader)

  
/*
 * Output:
 *  
 */
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StreamTokenizer;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileReader fr = new FileReader(args[0]);
      BufferedReader br = new BufferedReader(fr);
      StreamTokenizer st = new StreamTokenizer(br);
      st.ordinaryChar(".");
      st.wordChars("\"", "\"");
      while (st.nextToken() != StreamTokenizer.TT_EOF) {
        switch (st.ttype) {
        case StreamTokenizer.TT_WORD:
          System.out.println(st.lineno() + ") " + st.sval);
          break;
        case StreamTokenizer.TT_NUMBER:
          System.out.println(st.lineno() + ") " + st.nval);
          break;
        default:
          System.out.println(st.lineno() + ") " + (char) st.ttype);
        }
      }
      fr.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}





new StreamTokenizer(Reader r)

     
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
public class Main {
  public static void main(String[] av) throws IOException {
    StreamTokenizer tf = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    int i;
    while ((i = tf.nextToken()) != StreamTokenizer.TT_EOF) {
      switch (i) {
      case StreamTokenizer.TT_EOF:
        System.out.println("End of file");
        break;
      case StreamTokenizer.TT_EOL:
        System.out.println("End of line");
        break;
      case StreamTokenizer.TT_NUMBER:
        System.out.println("Number " + tf.nval);
        break;
      case StreamTokenizer.TT_WORD:
        System.out.println("Word, length " + tf.sval.length() + "->" + tf.sval);
        break;
      default:
        System.out.println("What is it? i = " + i);
      }
    }
  }
}





StreamTokenizer: commentChar(int ch)

 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
public class Main {
  public static void main(String args[]) throws IOException {
    BufferedReader inData = new BufferedReader(new InputStreamReader(System.in));
    StreamTokenizer inStream = new StreamTokenizer(inData);
    inStream.rumentChar("#");
    boolean eof = false;
    do {
      int token = inStream.nextToken();
      switch (token) {
      case StreamTokenizer.TT_EOF:
        System.out.println("EOF encountered.");
        eof = true;
        break;
      case StreamTokenizer.TT_EOL:
        System.out.println("EOL encountered.");
        break;
      case StreamTokenizer.TT_WORD:
        System.out.println("Word: " + inStream.sval);
        break;
      case StreamTokenizer.TT_NUMBER:
        System.out.println("Number: " + inStream.nval);
        break;
      default:
        System.out.println((char) token + " encountered.");
        if (token == "!")
          eof = true;
      }
    } while (!eof);
  }
}





StreamTokenizer: eolIsSignificant(boolean b)

  
/*
 * Output:
 *  
 */
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StreamTokenizer;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileReader fr = new FileReader(args[0]);
      BufferedReader br = new BufferedReader(fr);
      StreamTokenizer st = new StreamTokenizer(br);
      // Consider end-of-line as a token
      st.eolIsSignificant(true);
      // Declare variable to count lines
      int lines = 1;
      // Process tokens
      while (st.nextToken() != StreamTokenizer.TT_EOF) {
        switch (st.ttype) {
        case StreamTokenizer.TT_EOL:
          ++lines;
        }
      }
      System.out.println("There are " + lines + " lines");
      fr.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}





StreamTokenizer: lineno()

  
/*
 * Output:
 *  
 */
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StreamTokenizer;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileReader fr = new FileReader(args[0]);
      BufferedReader br = new BufferedReader(fr);
      StreamTokenizer st = new StreamTokenizer(br);
      st.ordinaryChar(".");
      st.wordChars("\"", "\"");
      while (st.nextToken() != StreamTokenizer.TT_EOF) {
        switch (st.ttype) {
        case StreamTokenizer.TT_WORD:
          System.out.println(st.lineno() + ") " + st.sval);
          break;
        case StreamTokenizer.TT_NUMBER:
          System.out.println(st.lineno() + ") " + st.nval);
          break;
        default:
          System.out.println(st.lineno() + ") " + (char) st.ttype);
        }
      }
      fr.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}





StreamTokenizer: nextToken()

  
/*
 * Output:
 *  
 */
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StreamTokenizer;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileReader fr = new FileReader(args[0]);
      BufferedReader br = new BufferedReader(fr);
      StreamTokenizer st = new StreamTokenizer(br);
      st.ordinaryChar(".");
      st.wordChars("\"", "\"");
      while (st.nextToken() != StreamTokenizer.TT_EOF) {
        switch (st.ttype) {
        case StreamTokenizer.TT_WORD:
          System.out.println(st.lineno() + ") " + st.sval);
          break;
        case StreamTokenizer.TT_NUMBER:
          System.out.println(st.lineno() + ") " + st.nval);
          break;
        default:
          System.out.println(st.lineno() + ") " + (char) st.ttype);
        }
      }
      fr.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}





StreamTokenizer: nval

  
/*
 * Output:
 *  
 */
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StreamTokenizer;
public class MainClass {
  public static void main(String args[]) {
    try {
      // Create a file reader
      FileReader fr = new FileReader(args[0]);
      // Create a buffered reader
      BufferedReader br = new BufferedReader(fr);
      // Create a stream tokenizer
      StreamTokenizer st = new StreamTokenizer(br);
      // Process tokens
      while (st.nextToken() != StreamTokenizer.TT_EOF) {
        switch (st.ttype) {
        case StreamTokenizer.TT_WORD:
          System.out.println(st.lineno() + ") " + st.sval);
          break;
        case StreamTokenizer.TT_NUMBER:
          System.out.println(st.lineno() + ") " + st.nval);
          break;
        default:
          System.out.println(st.lineno() + ") " + (char) st.ttype);
        }
      }
      // Close file reader
      fr.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}





StreamTokenizer: ordinaryChar(int ch)

  
/*
 * Output:
 *  
 */
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StreamTokenizer;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileReader fr = new FileReader(args[0]);
      BufferedReader br = new BufferedReader(fr);
      StreamTokenizer st = new StreamTokenizer(br);
      st.ordinaryChar(".");
      st.wordChars("\"", "\"");
      while (st.nextToken() != StreamTokenizer.TT_EOF) {
        switch (st.ttype) {
        case StreamTokenizer.TT_WORD:
          System.out.println(st.lineno() + ") " + st.sval);
          break;
        case StreamTokenizer.TT_NUMBER:
          System.out.println(st.lineno() + ") " + st.nval);
          break;
        default:
          System.out.println(st.lineno() + ") " + (char) st.ttype);
        }
      }
      fr.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}





StreamTokenizer.TT_EOF

  
/*
 * Output:
 *  
 */
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StreamTokenizer;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileReader fr = new FileReader(args[0]);
      BufferedReader br = new BufferedReader(fr);
      StreamTokenizer st = new StreamTokenizer(br);
      st.ordinaryChar(".");
      st.wordChars("\"", "\"");
      while (st.nextToken() != StreamTokenizer.TT_EOF) {
        switch (st.ttype) {
        case StreamTokenizer.TT_WORD:
          System.out.println(st.lineno() + ") " + st.sval);
          break;
        case StreamTokenizer.TT_NUMBER:
          System.out.println(st.lineno() + ") " + st.nval);
          break;
        default:
          System.out.println(st.lineno() + ") " + (char) st.ttype);
        }
      }
      fr.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}





StreamTokenizer.TT_NUMBER

  
/*
 * Output:
 *  
 */
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StreamTokenizer;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileReader fr = new FileReader(args[0]);
      BufferedReader br = new BufferedReader(fr);
      StreamTokenizer st = new StreamTokenizer(br);
      st.ordinaryChar(".");
      st.wordChars("\"", "\"");
      while (st.nextToken() != StreamTokenizer.TT_EOF) {
        switch (st.ttype) {
        case StreamTokenizer.TT_WORD:
          System.out.println(st.lineno() + ") " + st.sval);
          break;
        case StreamTokenizer.TT_NUMBER:
          System.out.println(st.lineno() + ") " + st.nval);
          break;
        default:
          System.out.println(st.lineno() + ") " + (char) st.ttype);
        }
      }
      fr.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}





StreamTokenizer.TT_WORD

  
/*
 * Output:
 *  
 */
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StreamTokenizer;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileReader fr = new FileReader(args[0]);
      BufferedReader br = new BufferedReader(fr);
      StreamTokenizer st = new StreamTokenizer(br);
      st.ordinaryChar(".");
      st.wordChars("\"", "\"");
      while (st.nextToken() != StreamTokenizer.TT_EOF) {
        switch (st.ttype) {
        case StreamTokenizer.TT_WORD:
          System.out.println(st.lineno() + ") " + st.sval);
          break;
        case StreamTokenizer.TT_NUMBER:
          System.out.println(st.lineno() + ") " + st.nval);
          break;
        default:
          System.out.println(st.lineno() + ") " + (char) st.ttype);
        }
      }
      fr.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}





StreamTokenizer: ttype

  
/*
 * Output:
 *  
 */
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StreamTokenizer;
public class MainClass {
  public static void main(String args[]) {
    try {
      // Create a file reader
      FileReader fr = new FileReader(args[0]);
      // Create a buffered reader
      BufferedReader br = new BufferedReader(fr);
      // Create a stream tokenizer
      StreamTokenizer st = new StreamTokenizer(br);
      // Process tokens
      while (st.nextToken() != StreamTokenizer.TT_EOF) {
        switch (st.ttype) {
        case StreamTokenizer.TT_WORD:
          System.out.println(st.lineno() + ") " + st.sval);
          break;
        case StreamTokenizer.TT_NUMBER:
          System.out.println(st.lineno() + ") " + st.nval);
          break;
        default:
          System.out.println(st.lineno() + ") " + (char) st.ttype);
        }
      }
      // Close file reader
      fr.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}





StreamTokenizer: wordChars((int low, int hi))

  
/*
 * Output:
 *  
 */
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StreamTokenizer;
public class MainClass {
  public static void main(String args[]) {
    try {
      FileReader fr = new FileReader(args[0]);
      BufferedReader br = new BufferedReader(fr);
      StreamTokenizer st = new StreamTokenizer(br);
      st.ordinaryChar(".");
      st.wordChars("\"", "\"");
      while (st.nextToken() != StreamTokenizer.TT_EOF) {
        switch (st.ttype) {
        case StreamTokenizer.TT_WORD:
          System.out.println(st.lineno() + ") " + st.sval);
          break;
        case StreamTokenizer.TT_NUMBER:
          System.out.println(st.lineno() + ") " + st.nval);
          break;
        default:
          System.out.println(st.lineno() + ") " + (char) st.ttype);
        }
      }
      fr.close();
    } catch (Exception e) {
      System.out.println("Exception: " + e);
    }
  }
}