a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 38|回复: 0

[JAVA] 2011年计算机的等考二级JAVA学习精华整理(25)

[复制链接]
发表于 2012-7-31 22:04:26 | 显示全部楼层 |阅读模式
 1.1.3.2.8 分解   分解 是指将字节或字符序列分割为像单词这样的逻辑块的过程。Java 提供StreamTokenizer 类, 像下面这样操作:- m% p5 t9 f* z% G
  import java.io.*;* Q% H& Z8 m' |* C- ^6 L# s' n
  public class token1 {( D* x2 l: F7 _+ |8 u
  public static void main(String args[]) {2 T2 m, [, r4 ], X5 N7 S
  if (args.length != 1) {4 y% b# k  o" |0 v& M0 j0 ~
  System.err.println("missing filename");
& A, w  x8 t0 c+ O( a9 W# _  System.exit(1);; H1 H9 l: K3 `0 K# w; m
  }# @: M0 o0 c7 q3 F8 O* b% A
  try {
; I0 Y. Z7 C- q% V  FileReader fr = new FileReader(args[0]);
% t3 V# g# m- T3 H  BufferedReader br = new BufferedReader(fr);5 T- }* _1 Z, w( Z' C, b
  StreamTokenizer st = new StreamTokenizer(br);) F) h+ G6 W6 n; e$ A
  st.resetSyntax();
" A- b; Y" `. Q; f  st.wordChars('a', 'z');
1 L2 f8 z$ Z9 |0 s  int tok;
& s8 ~. s* t" `! ~! z: ^( X8 K  while ((tok = st.nextToken()) != StreamTokenizer.TT_EOF) {
" ^: x' j; Q7 \  if (tok == StreamTokenizer.TT_WORD)9 O  X9 C& g) ?# P: T* P! ?
  ;// st.sval has token
, ~! A7 y# V4 G  z4 d6 ]# m8 @3 g4 D  }
* ~6 x$ R7 S( I9 d9 t9 X  br.close();
2 Q5 }+ u/ t; ^7 e, y  } catch (IOException e) {
* R" J4 `# ~+ Z7 i' ]  System.err.println(e);
, e9 a4 I% \: c: k. e  }; D  O% l" A1 [" r
  }7 v# K0 u2 K# Y- N6 K: [7 V& e
  }
7 ?) t- Y6 l! _7 H3 S1 k! G4 p  这个例子分解小写单词 (字母a-z)。如果你自己实现同等地功能,它可能像这样:
/ X9 R3 R" n6 {/ \  import java.io.*;9 _/ `* O  z& j) ^9 P7 J
  public class token2 {
6 s/ P8 r9 w2 |0 c2 W  public static void main(String args[]) {
0 P& ~5 |% W4 u$ Q9 S1 {  if (args.length != 1) {, o7 z2 b% K) W8 k: W9 V, s
  System.err.println("missing filename");2 Z: Z' S5 l2 y' m' P/ a
  System.exit(1);
' N! _7 a0 P  _  `( r, A# q" J& M  }7 e  T" b: W4 X
  try {* Q9 x, a) K3 Z
  FileReader fr = new FileReader(args[0]);
7 {* ]  l# W7 C+ Z4 X" p  BufferedReader br = new BufferedReader(fr);
2 _$ Y/ r" a  R1 x4 {  int maxlen = 256;
5 I" {* N9 G" z$ `$ X  int currlen = 0;0 Q" k+ p! u' k; n: ~% b; N: {0 D
  char wordbuf[] = new char[maxlen];
6 C' Y( w9 {$ }- N7 j) H  int c;* j' p; w1 q5 b3 r. J4 u$ |& O. _. y
  do {" p$ B5 p. m' Z3 \2 s
  c = br.read();' q' J! v# g) T; g% k. ]: d  L
  if (c >= 'a' && c  0) {4 b" D0 |2 N9 f5 ~. t& A3 ~
  String s = new String(wordbuf, 0, currlen); // do something
/ c1 m  X5 ^) j% }/ c# j+ X  // with s7 R- a2 C1 u! v2 ~
  currlen = 0;
- s3 u( A* |# e( d; J  }  r) Q/ T( B$ ]4 l8 n
  } while (c != -1);
/ i3 e, ~- F5 H* s& G1 h  br.close();6 Y$ U/ V1 Y) H; X
  } catch (IOException e) {- \9 a$ D1 r/ G5 c
  System.err.println(e);  r7 [# S& l( K% g9 D9 A2 ?
  }
4 ^9 Z3 M9 x+ `9 R" ~  }
8 Q0 w8 n2 h! E9 p1 [  }
/ H; ?* [- O$ U6 w  第二个程序比前一个运行快大约 20%,代价是写一些微妙的底层代码。2 h& x; b) C7 k* Z
  StreamTokenizer 是一种混合类,它从字符流(例如 BufferedReader)读取, 但是同时以字节的形式操作,将所有的字符当作双字节(大于 0xff) ,即使它们是字母字符。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Woexam.Com ( 湘ICP备18023104号 )

GMT+8, 2024-5-17 16:39 , Processed in 0.333418 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表