⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 data.html

📁 java 基础的 一点东西,,可以看看
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><HTML><HEAD>  <TITLE>Java(TM) Language Basics, Part 1, Lesson 6: File Access and Permissions</TITLE>  <META NAME="AUTHOR" CONTENT="Monica Pawlan">  <META NAME="KEYWORDS" CONTENT="programming, basics, Java 2">  <META NAME="OWNER" CONTENT="Editorial/JDC">  <META NAME="revision" CONTENT="@(#)data.src	1.34 03/09/00  JDC">    <STYLE TYPE="text/css">    <!--     CODE {font-family: Courier, Monospace;          font-size: 12pt}    PRE {font-family: Courier, Monospace;         font-size: 11pt}      -->  </STYLE></HEAD><!-- Start Body Insert--><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" ALINK="#FF0000" VLINK="#660066"><!-- End Body Insert--><!-- Start PageTop Insert --><TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" WIDTH="100%">  <TR ALIGN="CENTER" VALIGN="TOP">    <TD WIDTH="157" ALIGN="LEFT">    <IMG SRC="/images/pixel.gif" HEIGHT="40" WIDTH="40" ALT="">    <A HREF="http://java.sun.com/index.html"><IMG SRC="/images/javalogo52x88.gif" WIDTH="52" HEIGHT="88" ALT="Java Technology Home Page" BORDER="0"></A>    <BR>    <IMG SRC="/images/pixel.gif" WIDTH="157" HEIGHT="1" ALT=""></TD>    <TD>    <FORM NAME="seek1" METHOD="GET" ACTION="http://search.java.sun.com/query.html">    <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">      <TR>        <TD ALIGN="RIGHT">        <IMG SRC="/images/stripelt.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>                <TD WIDTH="100%">                <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0" WIDTH="100%">          <TR>            <TD BGCOLOR="#CC9966" WIDTH="100%">            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>          </TR>                    <TR>            <TD>            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>          </TR>                    <TR>            <TD BGCOLOR="#CC9966">            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>          </TR>                    <TR>            <TD>            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>          </TR>                    <TR>            <TD BGCOLOR="#CC9966">            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>          </TR>                    <TR>            <TD>            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>          </TR>                    <TR>            <TD BGCOLOR="#CC9966">            <IMG SRC="/images/pixel.gif" HEIGHT="2" WIDTH="1" ALT=""></TD>          </TR>        </TABLE>        </TD>                <TD ALIGN="LEFT">        <IMG SRC="/images/stripert.gif" WIDTH="6" HEIGHT="14" ALT=""></TD>        <TD>                <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">          <TR>            <TD VALIGN="CENTER">            <A HREF="http://java.sun.com/a-z/index.html"><IMG SRC="/images/azindex.gif" BORDER="0" WIDTH="72" HEIGHT="11" ALT="A-Z Index"></A></TD>                          <TD VALIGN="CENTER">            <FONT FACE="Helvetica" SIZE="1">            <INPUT TYPE="text" SIZE="15" MAXLENGTH="128" NAME=qt></FONT></TD>                        <TD VALIGN="CENTER">            <INPUT TYPE="image" SRC="/images/search.button.gif" WIDTH="55" HEIGHT="14" ALT="Search" value="search" BORDER="0"></TD>          </TR>        </TABLE>        </TD>      </TR>    </TABLE>    </FORM>    <P>    <TABLE BORDER="0" WIDTH="100%" CELLPADDING="0" CELLSPACING="0">      <TR VALIGN="TOP">        <TD WIDTH="100%"><IMG SRC="/images/chiclet.row.gif" WIDTH="55"           HEIGHT="18" ALT=""></TD>                  <TD ROWSPAN="4" ALIGN="RIGHT" WIDTH="152">        <A HREF="/developer/index.html"><IMG SRC="/images/developer.connection.header.gif" BORDER="0" HEIGHT="42" WIDTH="319" ALT="Java Developer Connection(SM)"></A></TD>      </TR>            <TR VALIGN="TOP">        <TD BGCOLOR="#FFFFFF" HEIGHT="1" WIDTH="100%">        <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>      </TR>            <TR VALIGN="TOP">        <TD BGCOLOR="#CC9966" HEIGHT="1" WIDTH="100%">        <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD>      </TR>            <TR VALIGN="TOP">        <TD><A HREF="/developer/onlineTraining/"><IMG SRC="/images/online-training.gif" WIDTH="165" HEIGHT="22" ALT="Online Training" BORDER=0></A></TD>      </TR>    </TABLE>    </TD>  </TR></TABLE><!-- End PageTop Insert --><!-- Start NavBar Insert --><TABLE BORDER="0" CELLSPACING="0" CELLPADDING="3" BGCOLOR="#FFFFFF" WIDTH="157" ALIGN="LEFT"><!-- tab categories --><TR><TD><A HREF="http://java.sun.com/products/"><IMG SRC="/images/side.tab.products.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Downloads, APIs, Documentation"></A></TD></TR><TR><TD><A HREF="/developer/index.html"><IMG SRC="/images/side.tab.developer.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Java Developer Connection"></A></TD></TR><TR><TD><A HREF="/developer/infodocs/index.shtml"><IMG SRC="/images/side.tab.docs.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Tutorials, Tech Articles, Training"></A></TD></TR><TR><TD><A HREF="/developer/support/index.html"><IMG SRC="/images/side.tab.support.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Online Support"></A></TD></TR><TR><TD><A HREF="/developer/community/index.html"><IMG SRC="/images/side.tab.community.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Community Discussion"></A></TD></TR><TR><TD><A HREF="http://java.sun.com/industry/"><IMG SRC="/images/side.tab.news.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="News &amp; Events from Everywhere"></A></TD></TR><TR><TD><A HREF="http://java.sun.com/solutions"><IMG SRC="/images/side.tab.solutions.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="Products from Everywhere"></A></TD></TR><TR><TD><A HREF="http://java.sun.com/casestudies"><IMG SRC="/images/side.tab.case.gif" HEIGHT="15" WIDTH="130" BORDER="0" ALT="How Java Technology is Used Worldwide"></A></TD></TR><TR><TD>    <DIV ALIGN="RIGHT">    <A HREF="/servlet/PrintPageServlet"><IMG SRC="/images/printbutton.gif" WIDTH="155" HEIGHT="25" ALT="Print Button" BORDER="0"></A>    </DIV></TD></TR><TR><TD>&nbsp;</TD></TR><!-- End NavBar Insert --><!-- START SUB-NAV --><TR>  <TD><!-- INSERT SUB-NAV INFO -->  </TD></TR><!-- END SUB-NAV --></TABLE><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0"><TR><TD><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><!-- Template Version 2.0 --><!-- ================== --><!-- Start Main Content --><!-- ================== --><TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0"><TR><TD VALIGN="TOP"><FONT FACE="Verdana, Arial, Helvetica, sans-serif">  <A NAME="top"></A>  <DIV ALIGN="RIGHT">  <FONT SIZE="-1"><A HREF="/developer/onlineTraining/index.html">Training Index</A></FONT>  <H2>Java<FONT SIZE="-2"><SUP>TM</SUP></FONT> Programming Language Basics, Part 1<BR>  <FONT SIZE="3">Lesson 6: File Access and Permissions</FONT></H2>    <FONT SIZE="-1">[<A HREF="servlet.html">&lt;&lt;BACK</A>] [<A HREF="index.html#contents">CONTENTS</A>] [<A HREF="dba.html">NEXT&gt;&gt;</A>]</FONT></DIV>  <P>So far, you have learned how to retrieve and handle a short text string entered from the keyboard into a simple graphical user interface(GUI). But programs also retrieve, handle, and store data in files and databases. <P>This lesson expands the examples from previous lessons to perform basicfile access using the application programming interfaces (APIs)in the <CODE>java.io</CODE> package. It also showsyou how to grant applets permission to access specific files,and how to restrict an application so it has access to specific files only.<UL><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#app">File Access by Applications</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#prop">System Properties</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#sep">File.separatorChar</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#excep">Exception Handling</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#applet">File Access by Applets</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#sec">Granting Applets Permission</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#res">Restricting Applications</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#serv">File Access by Servlets</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#append">Appending</A></FONT><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A HREF="#more">More Informattion</A></FONT></UL><HR><A NAME="app"></A><H3>File Access by Applications</H3>The Java&#174; 2 Platform software provides a rich range of classes for reading characteror byte data into a program, and writing character or byte data out to an external file, storage device, or program. The source or destinationmight be on the local computer system where the program is running oranywhere on the network. <P>This section shows you how to read data fromand write data to a file on the local computer system. See <A HREF="http://java.sun.com/docs/books/tutorial">The Java<FONT SIZE="-2"><SUP>TM</SUP></FONT> Tutorial</A>trail on <A HREF="http://java.sun.com/docs/books/tutorial/essential/io/index.html">Readingand Writing</A> for information on transferring data between programs,between a program and memory, and performing operations such as bufferingor character encoding on data as it is read or written. <UL><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><STRONG>Reading:</STRONG> A program opens an input <EM>stream</EM> on thefile and reads the data in serially (in theorder it was written to the file). </FONT><P><LI><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><STRONG>Writing:</STRONG> Aprogram opens an output stream on the file and writes the data out serially.</FONT></UL>This first example converts the <A HREF="./Code/SwingUI.java">SwingUI.java</A> example from Lesson 4to accept user input through a text field. The window on the left appearswhen you start the <A HREF="./Code/FileIO.java">FileIO</A> application, andthe window on the right appears when you click the button. Whenyou click the button, whatever is entered into the text field is saved to a file. After that, another file is opened and read and its text isdisplayed in the window on the right. Click again and you are back to the original window with a blank text field ready for more input.<P><TABLE><TR><TD WIDTH=200 ALIGN=CENTER><FONT FACE="Verdana, Arial, Helvetica, sans-serif">  <IMG SRC="./Art/fileIO1.gif" WIDTH="212" HEIGHT="" ALT="100"><P>When Application Starts</FONT></TD><TD ALIGN=CENTER><FONT FACE="Verdana, Arial, Helvetica, sans-serif">  <IMG SRC="./Art/fileIO2.gif" WIDTH="212" HEIGHT="101" ALT=""><P>When Button Clicked</FONT></TD></TR></TABLE><P>The conversion from the <A HREF="./Code/SwingUI.java">SwingUI.java</A>program for Lesson 4 to the <A HREF="./Code/FileIO.java">FileIO.java</A>program for this lesson primarily involvesthe <CODE>constructor</CODE> and the <CODE>actionPerformed</CODE>method as described here.<H4>Constructor and Instance Variable Changes</H4>A <CODE>JTextfield</CODE> instance variable is added tothe class so the <CODE>constructor</CODE> can instantiate the objectand the <CODE>actionPerformed</CODE> method can access the textthe end user types into it.<P>The <CODE>constructor</CODE> instantiates the <CODE>JTextField</CODE>with a value of 20. This value tells the Java platform the numberof columns to use to calculate the preferred width of the field.Lower values result in a narrower display, and likewise, higher values result in a wider display. <P>The <CODE>text</CODE> label is added to the <CODE>North</CODE> section of the <CODE>BorderLayout</CODE>so the <CODE>JTextField</CODE> can be added to the<CODE>Center</CODE> section. <P><BLOCKQUOTE><HR><STRONG>Note:</STRONG>You can learnmore about component sizing in <A HREF="http://java.sun.com/docs/books/tutorial">The Java Tutorial</A>sections on <A HREF="http://java.sun.com/docs/books/tutorial/ui/swingLayout/problems.html">SolvingCommon Layout Problems</A>and<A HREF="http://java.sun.com/docs/books/tutorial/ui/swingOverview/layout.html">LayoutManagement</A>.   <HR></BLOCKQUOTE></FONT><PRE>//Instance variable for text fieldJTextField textField;FileIO(){   text = new JLabel(&quot;Text to save to file:&quot;);  clicked = new 	JLabel(&quot;Text retrieved from file:&quot;);  button = new JButton(&quot;Click Me&quot;);  button.addActionListener(this);  clickButton = new JButton(&quot;Click Again&quot;);  clickButton.addActionListener(this);//Text field instantiation  textField = new JTextField(20);  panel = new JPanel();  panel.setLayout(new BorderLayout());  panel.setBackground(Color.white);  getContentPane().add(panel);//Adjustments to layout to add text field  panel.add(&quot;North&quot;, text);  panel.add(&quot;Center&quot;, textField);  panel.add(&quot;South&quot;, button);} </PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><H4>Method Changes</H4>The <CODE>actionPerformed</CODE> method uses the<CODE>FileInputStream</CODE> and <CODE>FileOutputStream</CODE>classes to read data from and write data to a file.These classes handle data in byte streams, as opposed to character streams, which are shown in the applet

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -