|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
多谢指点,其实我对.net和ruby也不是很了解,对与java也只是刚起步的阶段,只是在学习中有了点想法就写出来了,现在俺本科还没毕业,所以对大型项目基本上也没有什么经验。stream|字符串从sun网站看到的StreamTokenizing
InTechTips:June23,1998,anexampleofstringtokenizationwaspresented,usingtheclassjava.util.StringTokenizer.
Theresalsoanotherwaytodotokenization,usingjava.io.StreamTokenizer.StreamTokenizeroperatesoninputstreamsratherthanstrings,andeachbyteintheinputstreamisregardedasacharacterintherangeu0000throughu00FF.
StreamTokenizerislowerlevelthanStringTokenizer,butoffersmorecontroloverthetokenizationprocess.Theclassusesaninternaltabletocontrolhowtokensareparsed,andthissyntaxtablecanbemodifiedtochangetheparsingrules.HeresanexampleofhowStreamTokenizerworks:
importjava.io.*;
importjava.util.*;
publicclassstreamtoken{
publicstaticvoidmain(Stringargs[])
{
if(args.length==0){
System.err.println("missinginputfilename");
System.exit(1);
}
Hashtablewordlist=newHashtable();
try{
FileReaderfr=newFileReader(args[0]);
BufferedReaderbr=newBufferedReader(fr);
StreamTokenizerst=newStreamTokenizer(br);
//StreamTokenizerst=
//newStreamTokenizer(newStringReader(
//"thisisatest"));
st.resetSyntax();
st.wordChars(A,Z);
st.wordChars(a,z);
inttype;
Objectdummy=newObject();
while((type=st.nextToken())!=
StreamTokenizer.TT_EOF){
if(type==StreamTokenizer.TT_WORD)
wordlist.put(st.sval,dummy);
}
br.close();
}
catch(IOExceptione){
System.err.println(e);
}
Enumerationenum=wordlist.keys();
while(enum.hasMoreElements())
System.out.println(enum.nextElement());
}
}
Inthisexample,aStreamTokenizeriscreatedontopofaFileReader/BufferedReaderpairthatrepresentsatextfile.NotethataStreamTokenizercanalsobemadetoreadfromaStringbyusingStringReaderasillustratedinthecommented-outcodeshownabove(StringBufferInputStreamalsoworks,althoughthisclasshasbeendeprecated).
ThemethodresetSyntaxisusedtocleartheinternalsyntaxtable,sothatStreamTokenizerforgetsanyrulesthatitknowsaboutparsingtokens.ThenwordCharsisusedtodeclarethatonlyupperandlowercaselettersshouldbeconsideredtoformwords.Thatis,theonlytokensthatStreamTokenizerrecognizesaresequencesofupperandlowercaseletters.
nextTokeniscalledrepeatedlytoretrievewords,andeachresultingwordisfoundinthepublicinstancevariable"st.sval".ThewordsareinsertedintoaHashtable,andattheendofprocessingthecontentsofthetablearedisplayed,usinganEnumerationasillustratedinTechTips:June23,1998.Sotheactionofthisprogramistofindalltheuniquewordsinatextfileanddisplaythem.
StreamTokenizeralsohasspecialfacilitiesforparsingnumbers,quotedstrings,andcomments.ItsausefulalternativetoStringTokenizer,andisespeciallyapplicableifyouaretokenizinginputstreams,orwishtoexercisefinercontroloverthetokenizationprocess
用java开发web只要两本书:一本是关于java基础的,一本是关于jsp、servlet的就可以了。开发周期长,我就来讲句题外话,现在有很多思想都是通过java来展现。 |
|