📄 ch36.htm
字号:
<B>Listing 36.4 FileApp.java: An Application That Readsa File.<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>import java.io.*;public class FileApp{ public static void main(String args[]) { System.out.println(""); System.out.println("------------------------------"); System.out.println(""); try { FileInputStream inputStream = new FileInputStream("test.java"); String str = ""; int b = 0; while(b != -1) { b = inputStream.read(); str += (char)b; } inputStream.close(); System.out.println(str); } catch (FileNotFoundException e) { System.out.println("File not found!"); } catch (IOException e) { System.out.println("I/O Error!"); } System.out.println("------------------------------"); }}</PRE></BLOCKQUOTE><HR><P><IMG ALIGN=RIGHT SRC="pseudo.gif" HEIGHT=94 WIDTH=94 BORDER=1><BLOCKQUOTE>Tell Java that the application uses the <TT>io</TT> package.<BR>Declare the <TT>FileApp</TT> class<BR> Define the <TT>main()</TT> method.<BR> Display blank and dashed lines.<BR> Create a <TT>FileInputStream</TT> object.<BR> Initialize the input variable and buffer.<BR> Loop until the last byte in the file is read.<BR> Read a byte from the input stream.<BR> Add the byte as a character to the string buffer.<BR> Close the input stream.<BR> Display the data read from the stream.<BR> Catch any exceptions and print error messages.<BR> Display the bottom dashed line.</BLOCKQUOTE><H2><A NAME="TheIawtIPackage"><FONT SIZE=5 COLOR=#Ff0000>The <I>awt</I> Package</FONT></A></H2><P>You're already familiar with the <TT>awt</TT> package, which containsthe classes you need to create and run applets in windowed environments.The <TT>awt</TT> package contain the <TT>Graphics</TT> class thatyou used to create displays for your applets, and all the controlclasses you used throughout the book to handle user interactionswith applets. The <TT>awt</TT> package even has the classes forhandling events and creating windows with menus. You've alreadyexplored much of the <TT>awt</TT> library, but for your referencetable 36.7 lists the package's classes and their descriptions.Feel free to explore any of the classes with which you're notfamiliar.<BR><P><CENTER><B>Table 36.7 Classes of the </B><I>AWT </I><B>Package.</B></CENTER><P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=195><I><B>Class</B></I></TD><TD WIDTH=395><I><B>Description</B></I></TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>BorderLayout</TT></TD><TD WIDTH=395>One of Java's layout managers.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>Button</TT></TD><TD WIDTH=395>Represents button controls.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>Canvas</TT></TD><TD WIDTH=395>Represents a surface on which a program can draw.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>CardLayout</TT></TD><TD WIDTH=395>One of Java's layout managers.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>Checkbox</TT></TD><TD WIDTH=395>Represents a checkbox control.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>CheckboxGroup</TT></TD><TD WIDTH=395>Represents a group of check boxes used as "radio buttons."</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>CheckboxMenuItem</TT></TD><TD WIDTH=395>A menu entry that can be checked.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>Choice</TT></TD><TD WIDTH=395>A type of pop-up menu.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>Color</TT></TD><TD WIDTH=395>Represents color values in Java programs.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>Component</TT></TD><TD WIDTH=395>The superclass from which all Java components are derived.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>Container</TT></TD><TD WIDTH=395>Represents an object that can hold Java components.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>Dialog</TT></TD><TD WIDTH=395>A dialog-box type of window.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>Dimension</TT></TD><TD WIDTH=395>Represents the width and height of an object.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>Event</TT></TD><TD WIDTH=395>Represents various system and user events.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>FileDialog</TT></TD><TD WIDTH=395>A dialog box for selecting files.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>FlowLayout</TT></TD><TD WIDTH=395>One of Java's layout managers.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>Font</TT></TD><TD WIDTH=395>Represents a character style.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>FontMetrics</TT></TD><TD WIDTH=395>The attributes of a font.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>Frame</TT></TD><TD WIDTH=395>A main window that can contain a menu and other window controls.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>Graphics</TT></TD><TD WIDTH=395>Contains methods for drawing various shapes and controllong graphical attributes like color, fonts, clipping rectangles, etc.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>GridBagConstraints</TT></TD><TD WIDTH=395>Used in conjunction with GridBagLayout managers.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>GridBagLayout</TT></TD><TD WIDTH=395>One of Java's layout managers.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>GridLayout</TT></TD><TD WIDTH=395>One of Java's layout managers.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>Image</TT></TD><TD WIDTH=395>Represents graphical images, usually in GIF format.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>Insets</TT></TD><TD WIDTH=395>Used as spacers for components in a container.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>Label</TT></TD><TD WIDTH=395>Represents text labels.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>LayoutManager</TT></TD><TD WIDTH=395>The superclass from which all layout managers are derived.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>List</TT></TD><TD WIDTH=395>Represents a list box control.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>MediaTracker</TT></TD><TD WIDTH=395>A class for organizing multiple images.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>Menu</TT></TD><TD WIDTH=395>Represents menus in a menu bar.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>MenuBar</TT></TD><TD WIDTH=395>Represents menu bars in frame windows.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>MenuComponent</TT></TD><TD WIDTH=395>The superclass from which all menu components are derived.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>MenuContainer</TT></TD><TD WIDTH=395>The superclass from which all menu containers are derived.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>MenuItem</TT></TD><TD WIDTH=395>Represents an item in a pop-up menu.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>Panel</TT></TD><TD WIDTH=395>A simple container class.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>Point</TT></TD><TD WIDTH=395>Represents an X,Y coordinate.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>Polygon</TT></TD><TD WIDTH=395>A list of coordinates for outlining a polygon.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>Rectangle</TT></TD><TD WIDTH=395>An object the represents the X,Y coordinate and width and height of a rectangle.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>Scrollbar</TT></TD><TD WIDTH=395>A scrollbar control.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>TextArea</TT></TD><TD WIDTH=395>A simple text edit box.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>TextComponent</TT></TD><TD WIDTH=395>A component for editing text.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>TextField</TT></TD><TD WIDTH=395>A one-line text component.</TD></TR><TR VALIGN=TOP><TD WIDTH=195><TT>Window</TT></TD><TD WIDTH=395>A general window class.</TD></TR></TABLE></CENTER><H2><A NAME="Summary"><FONT SIZE=5 COLOR=#Ff0000>Summary</FONT></A></H2><P>The Java Developers Kit is comprised of dozens of classes thatdo everything from define the basic language to enable programmersto create applets and applications for windowed environments.These classes are organized into six main packages: <TT>lang</TT>,<TT>util</TT>, <TT>io</TT>, <TT>awt</TT>, <TT>applet</TT>, and<TT>net</TT>. For the novice and intermediate Java programmer,the <TT>lang</TT> and <TT>awt</TT> packages, which define theJava language and supply classes for operating under a windowedenvironment, respectively, are by far the most important.<P>Although the <TT>io</TT> class enables the programmer to createvarious types of input and output streams, due to security considerations,Java applets are restricted on the types of I/O they can perform.For that reason, you'll probably use I/O methods mostly in Javastandalone applications, if you are even interested in buildingapplications rather than applets. Applets, of course, rely onthe few classes that make up the <TT>applet</TT> package for thefunctionality that sets them apart from regular applications.<P>Finally, the <TT>util</TT> and <TT>net</TT> packages contain littleof interest to any except advanced Java programmers. The <TT>util</TT>package contains classes that support the other Java classes byproviding helper classes such as <TT>Properties</TT>, <TT>Stack</TT>,and <TT>Vector</TT>. Finally, the <TT>net</TT> package featuresthe classes that enable programmers to include communication protocolsfor use with Internet connections in their applets and applications.<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#Ff0000>Review Questions</FONT></A></H2><OL><LI>What are the six main packages of the Java classes?<LI>Which package contains classes for operating in a windowedenvironment?<LI>When would you use the <TT>Math</TT> class?<LI>What's the advantage of using the <TT>String</TT> class tohandle text?<LI>How do you join two strings?<LI>Do you need to instantiate an object of the <TT>Math</TT>class in order to call its methods?<LI>What are data-type wrapper classes and how are they used?<LI>How can you get information about the system on which yourapplet or application is currently running?<LI>How can you write file-handling code for a standalone application?</OL><H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#Ff0000>Review Exercises</FONT></A></H2><OL><LI>Write an applet that accepts a value from the user, and thendisplays the value's square root, logarithm, and absolute value.<LI>Write an application called SystemApp2 that displays all systemproperties. Figure 36.5 shows what the program's output lookslike. (You canfind the solution for this exercise in the CHAP36folder of this book's CD-ROM.)</OL><P><A HREF="f36-5.gif"><B> Figure 36.5 : </B><I>The SystemApp2 application should display all of the system properties.</I></A><P><HR><HR WIDTH="100%"></P></CENTER><!-- reference library footer #1--></CENTER><IMG SRC="/images/rule.gif" WIDTH="460" HEIGHT="5" VSPACE="5"ALT="Ruler image"><br><FONT SIZE="-1">Contact <a href="mailto:reference@developer.com">reference@developer.com</a> with questions or comments.<br><a href="/legal/">Copyright 1998</a> <a href="http://www.earthweb.com" target="_top">EarthWeb Inc.</a>, All rights reserved.<BR>PLEASE READ THE <a href="/reference/usage.html">ACCEPTABLE USAGE STATEMENT</a>.<BR>Copyright 1998 Macmillan Computer Publishing. All rights reserved.</FONT></BLOCKQUOTE><!--outer table--><TD VALIGN="TOP"><!--right side ads --><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCAD1.REF" alt="Click here for more info"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCAD1.REF" alt="Click here for more info" height="88" width="88" border="0"></a><P><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCAD2.REF" alt="Click here for more info"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCAD2.REF" alt="Click here for more info" height="88" width="88" border="0"></a><P></td></tr></table></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -