📄 203-207.html
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1558515682"><META name=vstitle content="Java Digital Signal Processing"><META name=vsauthor content="Douglas A. Lyon"><META name=vsimprint content="M&T Books"><META name=vspublisher content="IDG Books Worldwide, Inc."><META name=vspubdate content="11/01/97"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Java Digital Signal Processing:Futil Recipes for Feudal Times</TITLE>
<!-- HEADER --><STYLE type="text/css"> <!-- A:hover { color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
<!--ISBN=1558515682//-->
<!--TITLE=Java Digital Signal Processing//-->
<!--AUTHOR=Douglas A. Lyon//-->
<!--PUBLISHER=IDG Books Worldwide, Inc.//-->
<!--IMPRINT=M & T Books//-->
<!--CHAPTER=4//-->
<!--PAGES=203-207//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="199-203.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="207-211.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H3><A NAME="Heading50"></A><FONT COLOR="#000077">StreamTokenizer</FONT></H3>
<P>The <I>StreamTokenizer</I> class resides in the <I>java.io</I> package. It is used to convert an <I>InputStream</I> instance into a stream of tokens. Refer to [Gosling and Yellin] for a more comprehensive description of <I>StreamTokenizer</I>. Bytes are read from the <I>InputStream</I> instance and are treated as unsigned <I>shorts</I> that range from 0 to 255.</P>
<P>A <I>StreamTokenizer</I> instance is an instance of a parser. The parser settings can be altered to recognize C-style comments, C++-style comments, and line terminators and to convert tokens to lowercase.</P>
<I>StreamTokenizer</I> is a higher-level <I>Stream</I> than <I>DataInputStream</I>.
<H4 ALIGN="LEFT"><A NAME="Heading51"></A><FONT COLOR="#000077">Class Summary</FONT></H4>
<!-- CODE //-->
<PRE>
public class StreamTokenizer {
public int ttype
public static final int TT_EOF
public static final int TT_EOL
public static final int TT_NUMBER
public static final int TT_WORD
public String sval
public double nval
public StreamTokenizer (InputStream I)
public void resetSyntax()
public void wordChars(int low, int hi)
public void whitespaceChars(int low, int hi)
public void ordinaryChars(int low, int hi)
public void ordinaryChar(int ch)
public void commentChar(int ch)
public void quoteChar(int ch)
public void parseNumbers()
public void eolIsSignificant(boolean flag)
public void slashStarComments(boolean flag)
public void slashSlashComments(boolean flag)
public void lowerCaseMode(boolean fl)
public int nextToken() throws IOException
public void pushBack()
public int lineno()
public String toString()
}
</PRE>
<!-- END CODE //-->
<H4 ALIGN="LEFT"><A NAME="Heading52"></A><FONT COLOR="#000077">Class Usage</FONT></H4>
<P>Suppose the following variables are predefined:
</P>
<!-- CODE SNIP //-->
<PRE>
String sval;
double nval;
InputStream is;
int low, hi, ch, token, lineNumber;
String s;
StreamTokenizer st;
</PRE>
<!-- END CODE SNIP //-->
<P>To make an instance of a <I>StreamTokenizer</I>, use this code:</P>
<!-- CODE SNIP //-->
<PRE>
st = new StreamTokenizer(is);
</PRE>
<!-- END CODE SNIP //-->
<P><I>StreamTokenizer</I> has several constants held as public static final <I>ints</I>. They are used by case statements to determine the token type. They are the end-of-file and end-of-line tokens:</P>
<!-- CODE SNIP //-->
<PRE>
StreamTokenizer.TT_EOF
StreamTokenizer.TT_EOL
</PRE>
<!-- END CODE SNIP //-->
<P>To determine whether the token is a number (value in <I>nval</I>) check the Return of St. next Token() to see if it is equal to:</P>
<!-- CODE SNIP //-->
<PRE>
StreamTokenizer.TT_NUMBER
</PRE>
<!-- END CODE SNIP //-->
<P>To determine whether the token is a word (value in <I>sval</I>) check St. next Token() for a return of:</P>
<!-- CODE SNIP //-->
<PRE>
StreamTokenizer.TT_WORD
</PRE>
<!-- END CODE SNIP //-->
<P>To make all characters ordinary:
</P>
<!-- CODE SNIP //-->
<PRE>
st.resetSyntax();
</PRE>
<!-- END CODE SNIP //-->
<P>To cumulatively specify the Unicode characters to be used for words:
</P>
<!-- CODE SNIP //-->
<PRE>
st.wordChars(low, hi);
</PRE>
<!-- END CODE SNIP //-->
<P>To spec the whitespace <I>chars</I>:</P>
<!-- CODE SNIP //-->
<PRE>
st.whitespaceChars(low,hi);
</PRE>
<!-- END CODE SNIP //-->
<P>Ordinary <I>chars</I> are returned by <I>nextToken</I>. To spec the ordinary <I>chars</I>:</P>
<!-- CODE SNIP //-->
<PRE>
st.ordinaryChars(low, hi);
</PRE>
<!-- END CODE SNIP //-->
<P>To add an <I>ordinaryChar</I>:</P>
<!-- CODE SNIP //-->
<PRE>
st.ordinaryChar(ch);
</PRE>
<!-- END CODE SNIP //-->
<P>To add a single-line comment <I>char</I> (all <I>chars</I> to the end of line are comment <I>chars</I>, and the tokenizer skips them):</P>
<!-- CODE SNIP //-->
<PRE>
st.commentChar(ch);
</PRE>
<!-- END CODE SNIP //-->
<P>To spec the <I>quoteChar</I> to delimit a string:</P>
<!-- CODE SNIP //-->
<PRE>
st.quoteChar(ch);
</PRE>
<!-- END CODE SNIP //-->
<P>To spec that numbers should be parsed:
</P>
<!-- CODE SNIP //-->
<PRE>
st.parseNumbers();
</PRE>
<!-- END CODE SNIP //-->
<P>To have <I>TT_EOL</I> returned by <I>nexttoken</I>:</P>
<!-- CODE SNIP //-->
<PRE>
flag = true;
st.eolIsSignificant(flag);
</PRE>
<!-- END CODE SNIP //-->
<P>To select '/*' comments:
</P>
<!-- CODE SNIP //-->
<PRE>
st.slashStarComments(flag);
</PRE>
<!-- END CODE SNIP //-->
<P>To select '//' comments:
</P>
<!-- CODE SNIP //-->
<PRE>
st.slashSlashComments(flag);
</PRE>
<!-- END CODE SNIP //-->
<P>To select automatic lower case conversion:
</P>
<!-- CODE SNIP //-->
<PRE>
lowerCaseMode(true);
</PRE>
<!-- END CODE SNIP //-->
<P>To parse a token, returning <I>ttype</I>:</P>
<!-- CODE SNIP //-->
<PRE>
ttype = st.nextToken();
</PRE>
<!-- END CODE SNIP //-->
<P>To push a token back into the stream:
</P>
<!-- CODE SNIP //-->
<PRE>
st.pushBack();
</PRE>
<!-- END CODE SNIP //-->
<P>To get the current line number:
</P>
<!-- CODE SNIP //-->
<PRE>
i = st.lineno();
</PRE>
<!-- END CODE SNIP //-->
<P>To convert the <I>StreamToken</I> to a string:</P>
<!-- CODE SNIP //-->
<PRE>
s = st.toString();
</PRE>
<!-- END CODE SNIP //-->
<P>For an example of use, please see the next section.
</P>
<H4 ALIGN="LEFT"><A NAME="Heading53"></A><FONT COLOR="#000077">Futil.readDataFile</FONT></H4>
<P>To save the state of our DiffCAD program, we write several key parameters into a file. These parameters are stored with keywords and values so that a user with a text editor (perhaps a nonprogrammer ) can alter the data. A sample data file follows:
</P>
<!-- CODE SNIP //-->
<PRE>
Data format is order dependentlyon.Laser Rotation= 34.4198
6.47311 -36.7812 lyon.Camera rho= 18.4931 pc= 125.3
-102.35 A= 4.8 F= 3.6 lyon.Wedge p1 = -403.685
591.244 lyon.Grating P1= 6651 P2= 2261 L= 81
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>readDataFile</I> method is in the <I>futils</I> package. It is used to take a file name and read data into an array of <I>doubles</I>. To perform this task we use a <I>StreamTokenizer</I> instance called <I>tokens</I>.</P>
<!-- CODE SNIP //-->
<PRE>
1. public static void readDataFile(String file,double data[]) {
2. System.out.println("processing:\t" + file);
3. FileInputStream inputFile =
4. getFileInputStream(file);
5. StreamTokenizer tokens = new StreamTokenizer(inputFile);
6. int next = 0;
7. int num = 0;
</PRE>
<!-- END CODE SNIP //-->
<P>Note that the <I>tokens.nextToken()</I> must be nested in a try-catch block.</P>
<!-- CODE SNIP //-->
<PRE>
8. try {
9. while ((next = tokens.nextToken()) != tokens.TT_EOF) {
10. switch (next) {
11. case tokens.TT_WORD:
12. break;
</PRE>
<!-- END CODE SNIP //-->
<P>Lines 11 and 12 indicate that words are ignored (they are for humans!). Only numbers matter, and because people are warned not to change the order of the parameters, future versions of the data file can add numbers only to the end of the file. They cannot insert numbers.
</P>
<!-- CODE //-->
<PRE>
13. case tokens.TT_NUMBER:
14. data[num] = (double) tokens.nval;
15. System.out.println(num+": "+ data[num]);
16. num = num + 1;
17. break;
18. case tokens.TT_EOL:
19. break;
20. }
21. }
22. }
23. catch (Exception exe)
24. {System.out.println("listFilteredHrefFile:er!");}
25. closeInputStream(inputFile);
26. }
</PRE>
<!-- END CODE //-->
<P>Creation of the file is a simple matter. In the following code, we open a file as a <I>PrintStream</I> instance, write the data, and then close the file:</P>
<!-- CODE SNIP //-->
<PRE>
public static void save() {
FileOutputStream os = Futil.getWriteFileOutputStream();
PrintStream output = new PrintStream(os);
output.println("Data format is order-dependent");
Geometry.print(output);
Futil.closeOutputStream(os);
}
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>Geometry.print</I> is a public static method that uses the <I>Print</I> class in the <I>futils</I> package, which is described in the following section.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="199-203.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="207-211.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<hr width="90%" size="1" noshade><div align="center"><font face="Verdana,sans-serif" size="1">Copyright © <a href="/reference/idgbooks00001.html">IDG Books Worldwide, Inc.</a></font></div>
<!-- all of the reference materials (books) have the footer and subfoot reveresed --><!-- reference_subfoot = footer --><!-- reference_footer = subfoot --></BODY></HTML><!-- END FOOTER -->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -