import java.io.FileReader; import java.io.StreamTokenizer; import java.io.StringReader; // Do you see this comment? public class TokenizerExample2 { public static void main(String[] args) { try { FileReader reader = new FileReader("sample.html"); StreamTokenizer in = new StreamTokenizer(reader); int tokenType, depth = 0; in.wordChars(0, 127); in.ordinaryChar('<'); in.ordinaryChar('/'); in.ordinaryChar('>'); in.ordinaryChar('\r'); in.ordinaryChar('\n'); while ((tokenType = in.nextToken()) != StreamTokenizer.TT_EOF) { if (tokenType == StreamTokenizer.TT_WORD) { outputString(depth, in.sval); } else if (tokenType == '<') { tokenType = in.nextToken(); if (tokenType == '/') { depth--; in.nextToken(); outputString(depth, ""); } else { String internalStr = in.sval; while((tokenType = in.nextToken()) != '>') { if (tokenType == StreamTokenizer.TT_WORD) { internalStr += in.sval; } else { internalStr += (char)tokenType; } } outputString(depth, "<" + internalStr + ">"); if (!internalStr.startsWith("BR")) { depth++; } } } else if (tokenType == '/') { System.out.print("/"); } } reader.close(); } catch (Exception e) { e.printStackTrace(); } } public static void outputString(int depth, String string) { for (int i = 0; i < depth; i++) { System.out.print("\t"); } System.out.println(string); } }