?? 195-199.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=195-199//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="192-195.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="199-203.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H4 ALIGN="LEFT"><A NAME="Heading44"></A><FONT COLOR="#000077">Class Usage</FONT></H4>
<!-- CODE //-->
<PRE>
InputStream is;
byte b, bytes[];
int length, offset;
char c;
int i;
long l;
float f;
double d;
String string;
</PRE>
<!-- END CODE //-->
<P>Suppose that the following variables are predefined:
</P>
<!-- CODE SNIP //-->
<PRE>
InputStream is;
DataInputStream dis;
int numberRead;
</PRE>
<!-- END CODE SNIP //-->
<P>To create an instance of the <I>DataInputStream</I> class, use this statement:</P>
<!-- CODE SNIP //-->
<PRE>
dis = new DataInputStream(is);
</PRE>
<!-- END CODE SNIP //-->
<P>To read data into a byte array or subarray:
</P>
<!-- CODE SNIP //-->
<PRE>
numberRead = dis.read(bytes);
numberRead = dis.read(bytes, offset, length);
</PRE>
<!-- END CODE SNIP //-->
<P>To read <I>bytes.length</I> into <I>bytes</I> from <I>bytes[0]</I>:</P>
<!-- CODE SNIP //-->
<PRE>
dis.readFully(bytes);
</PRE>
<!-- END CODE SNIP //-->
<P>To read <I>bytes</I> into a subarray:</P>
<!-- CODE SNIP //-->
<PRE>
dis.readFully(bytes, offset, length);
</PRE>
<!-- END CODE SNIP //-->
<P>To skip bytes:
</P>
<!-- CODE SNIP //-->
<PRE>
numberSkipped = dis.skip(numberToSkip);
</PRE>
<!-- END CODE SNIP //-->
<P>To read a boolean (a single byte that is nonzero for true to be returned):
</P>
<!-- CODE SNIP //-->
<PRE>
aboolean = dis.readBoolean();
</PRE>
<!-- END CODE SNIP //-->
<P>To read a byte:
</P>
<!-- CODE SNIP //-->
<PRE>
b = dis.readByte();
</PRE>
<!-- END CODE SNIP //-->
<P>To read the byte into an <I>int</I>:</P>
<!-- CODE SNIP //-->
<PRE>
i = dis.readUnsignedByte();
</PRE>
<!-- END CODE SNIP //-->
<P>To read a 16-bit signed or unsigned <I>short</I>:</P>
<!-- CODE SNIP //-->
<PRE>
s = dis.readShort();
i = dis.readUnsignedShort();
</PRE>
<!-- END CODE SNIP //-->
<P>To read a 16-bit <I>char</I>:</P>
<!-- CODE SNIP //-->
<PRE>
c = dis.readChar();
</PRE>
<!-- END CODE SNIP //-->
<P>To read 32 bits into an <I>int</I>:</P>
<!-- CODE SNIP //-->
<PRE>
i = dis.readInt();
</PRE>
<!-- END CODE SNIP //-->
<P>To read 64 bits into a <I>long</I>:</P>
<!-- CODE SNIP //-->
<PRE>
l = dis.readLong();
</PRE>
<!-- END CODE SNIP //-->
<P>To read 32 bits into a <I>float</I>:</P>
<!-- CODE SNIP //-->
<PRE>
f = dis.readFloat();
</PRE>
<!-- END CODE SNIP //-->
<P>To read 64 bits into a <I>double</I>:</P>
<!-- CODE SNIP //-->
<PRE>
d = dis.readDouble();
</PRE>
<!-- END CODE SNIP //-->
<P>To read a line, stopping at the end of the stream ('\n', '\r', or '\r\n'):
</P>
<!-- CODE SNIP //-->
<PRE>
string = dis.readLine();
</PRE>
<!-- END CODE SNIP //-->
<P>To read a Unicode Transfer Format (UTF) string:
</P>
<!-- CODE SNIP //-->
<PRE>
string = dis.readUTF();
</PRE>
<!-- END CODE SNIP //-->
<P>To read UTF from an <I>InputStream</I>:</P>
<!-- CODE SNIP //-->
<PRE>
string = DataInputStream.readUTF(is);
</PRE>
<!-- END CODE SNIP //-->
<H4 ALIGN="LEFT"><A NAME="Heading45"></A><FONT COLOR="#000077">Cat.fileToStream and Cat.javasToFile</FONT></H4>
<P>The <I>futils</I> package has a facility that works like the <I>cat</I> command of UNIX. The basic idea is that we would like Java to perform the <I>cat *.java >file</I> sequence, listing all the files in the current directory into a single file.</P>
<P>On line 2 we build a list of files with the <B>.java</B> suffix. Lines 6 and 7 pass the file name and print stream of each file to the <I>Cat.fileToStream</I> method.</P>
<!-- CODE //-->
<PRE>
1.static public void javasToFile() {
2.String[] files = Ls.getWildNames("java");
3. FileOutputStream fos =
4. Futil.getFileOutputStream();
5. PrintStream ps = new PrintStream(fos);
6. for (int i=0; i < files.length; i++)
7. fileToStream(files[i], ps);
8. Futil.closeOutputStream(fos);
9. }
10.static public void fileToStream(String fileName, PrintStream output) {
11.System.out.println("cat: "+fileName);
12.FileInputStream fis = Futil.getFileInputStream(fileName);
13. String line;
</PRE>
<!-- END CODE //-->
<P>The mapping is such that there may be many input streams but only a single output stream. This requires that we open and close the <I>FileInputStream</I> many times on several different files.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE: </B>The <I>try</I> and <I>catch</I> surrounding lines 15-17 reflect the <I>IOException</I> that line 16 might throw.<HR></FONT>
</BLOCKQUOTE>
<!-- CODE //-->
<PRE>
14. try {
15. DataInputStream dis = new DataInputStream(fis);
16. while ((line = dis.readLine()) != null)
17. output.println(line);
18. } // try
19. catch (Exception exe)
20. {System.out.println("cat:Error on input file");}
21. Futil.closeInputStream(fis);
22. }
</PRE>
<!-- END CODE //-->
<H3><A NAME="Heading46"></A><FONT COLOR="#000077">The DataOutputStream Class</FONT></H3>
<P>Like the <I>DataInputStream</I> class, the <I>DataOutputStream</I> class resides in the <I>java.io</I> package. <I>DataOutputStream</I> is a byte stream writer, providing high-level methods that supply writing services from various primitive data types into a stream of bytes. This class is useful when you’re attempting to encode binary files (such as the audio files in Chapter 5).</P>
<P><I>DataOutputStream</I> is a subclass of <I>FilterOutputStream</I> and implements the <I>DataOutput</I> interface.</P>
<P>A <I>DataOutputStream</I> instance keeps track of the number of bytes that it has written. This value is kept in protected storage. All output performed with blocking is blocked until the data is finally written. If the output does not say “with blocking,” the output can be buffered and flushed either manually or automatically.</P>
<P>A UTF-8 format is used to write strings in a machine-independent manner. <I>Chars</I> from 1 to 127 are written as a single byte. <I>Chars</I> from 128 to 2,047 and 0 are written by a pair of bytes. <I>Chars</I> from 2,048 to 65,535 are written by three bytes. The actual bit format is given in [Gosling and Yellin].</P>
<H4 ALIGN="LEFT"><A NAME="Heading47"></A><FONT COLOR="#000077">Class Summary</FONT></H4>
<!-- CODE //-->
<PRE>
public class DataOutputStream extends FilterOutputStream implements
DataOutput.
public DataOutputStream(OutputStream out)
public synchronized void write(int b) throws IOException
public synchronized void write(byte b[], int off, int len)
public void flush() throws IOException
public final void writeBoolean(boolean v) throws IOException
public final void writeByte(int v) throws IOException
public final void writeShort(int v) throws IOException
public final void writeChar(int v) throws IOException
public final void writeInt(int v) throws IOException
public final void writeLong(long v) throws IOException
public final void writeFloat(float v) throws IOException
public final void writeDouble(double v) throws IOException
public final void writeBytes(String s) throws IOException
public final void writeChars(String s) throws IOException
public final void writeUTF(String str) throws IOException
public final int size()
}
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="192-195.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="199-203.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 + -