📄 data.html
字号:
}}</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A NAME="sec"></A><H3>Granting Applets Permission</H3>If you tried to run the applet example, you undoubtedlysaw errors when you clicked the <CODE>Click Me</CODE> button. This is because the Java 2 Platform security does not permit an applet to write to and read from files without explicit permission.<P>An applet has no access to local system resources unless itis specifically granted the access. So for the <CODE>FileUIAppl</CODE> program to read from <CODE>text.txt</CODE> and write to <CODE>text.txt</CODE>, the applet has to be given the appropriate reador write access permission for each file. <P>Access permission is granted with a policy file, and appletvieweris launched with the policy file to be used for the appletbeing viewed.<H4>Creating a Policy File</H4>Policy tool is a Java 2 Platform security tool for creating policy files. <A HREF="http://java.sun.com/docs/books/tutorial/">The Java Tutorial</A>trail on <A HREF="http://java.sun.com/docs/books/tutorial/security1.2/tour1/step2.html">Controlling Applets</A> explains how to use Policy Tool in good detail. Here is the policy file you need to run the applet. You can usePolicy tool to create it or copy the text below into an ASCII file. </FONT><PRE>grant { permission java.util.PropertyPermission "user.home", "read"; permission java.io.FilePermission "${user.home}/text.txt", "read,write";};</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><H4>Running an Applet with a Policy File</H4>Assuming the policy file is named <CODE>polfile</CODE> andis in the same directory with an HTML file named<CODE>fileIO.html</CODE> that contains the HTML to run the <CODE>FileIOAppl</CODE> applet, you would run theapplication in appletviewer like this:</FONT><PRE>appletviewer -J-Djava.security.policy=polfile fileIO.html</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><BLOCKQUOTE><HR><STRONG>Note:</STRONG>If your browser is enabled for the Java 2 Platform or if youhave <A HREF="http://java.sun.com/products/plugin/index.html">JavaPlug-in</A> installed, youcan run the applet from the browser if you put the policy filein your local home directory. <HR></BLOCKQUOTE>Here is the <CODE>fileIO.html</CODE> file for running the <CODE>FileIOAppl</CODE>applet:</FONT><PRE><HTML><BODY><APPLET CODE=FileIOAppl.class WIDTH=200 HEIGHT=100></APPLET></BODY></HTML></PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A NAME="res"></A><H3>Restricting Applications</H3>You can use the default security manager and a policy fileto restrict the application's access as follows. </FONT><PRE>java -Djava.security.manager -Djava.security.policy=apppolfile FileIO </PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif">Because the application runs within the security manager, which disallowsall access, the policy file needs two additional permissions. One sothe security manager can access the event queue and load the user interfacecomponents, and another so the application does not displaythe banner warning that its window was created by anotherprogram (the security manager). </FONT><PRE>grant { permission java.awt.AWTPermission "accessEventQueue"; permission java.awt.AWTPermission "showWindowWithoutWarningBanner"; permission java.util.PropertyPermission "user.home", "read"; permission java.io.FilePermission "${user.home}/text.txt", "read,write";};</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A NAME="serv"></A><H3>File Access by Servlets</H3>Although servlets are invoked from a browser, they are under the security policy in force for the web server under which they run. When file input and output code is added to <CODE>ExampServlet.java</CODE> from Lesson 5, <A HREF="./Code/FileIOServlet.java">FileIOServlet</A> for this lesson executes without restriction under Java WebServer<FONT SIZE="-2"><SUP>TM</SUP></FONT> 1.1.1. <P><IMG SRC="./Art/servIO1.gif" WIDTH="297" HEIGHT="205" ALT=""><TABLE BORDER="2" CELLSPACING="2" CELLPADDING="2"><TR><TD><IMG SRC="./Art/servIO2.gif" WIDTH="313" HEIGHT="115" ALT=""></TD></TR></TABLE></FONT><PRE>import java.io.*;import javax.servlet.*;import javax.servlet.http.*;public class FileIOServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<title>Example<title>" + "<body bgcolor=FFFFFF>"); out.println("<h2>Button Clicked</h2>"); String DATA = request.getParameter("DATA"); if(DATA != null){ out.println("<STRONG>Text from form:</STRONG>"); out.println(DATA); } else { out.println("No text entered."); } try{//Code to write to file String outputFileName= System.getProperty("user.home", File.separatorChar + "home" + File.separatorChar + "monicap") + File.separatorChar + "text.txt"; File outputFile = new File(outputFileName); FileWriter fout = new FileWriter(outputFile); fout.write(DATA); fout.close();//Code to read from file String inputFileName = System.getProperty("user.home", File.separatorChar + "home" + File.separatorChar + "monicap") + File.separatorChar + "text.txt"; File inputFile = new File(inputFileName); FileReader fin = new FileReader(inputFile); char c[] = new char[(char)inputFile.length()]; int i; i = fin.read(c); String s = new String(c); out.println("<P> <STRONG>Text from file:</STRONG>"); out.println(s); fin.close(); }catch(java.io.IOException e){ System.out.println("Cannot access text.txt"); } out.println("<P>Return to <A HREF="../simpleHTML.html">Form</A>"); out.close(); }}</PRE><FONT FACE="Verdana, Arial, Helvetica, sans-serif"><A NAME="append"></A><H3>Appending</H3>So far the examples have shown you how to read in andwrite out streams of data in their entirety. But often,you want to append data to an existing file or read inonly certain amounts. Using the <A HREF="http://java.sun.com/products/jdk/1.2/docs/api/java/io/RandomAccessFile.html">RandomAccessFile</A> class, alter the <A HREF="./Code/FileIO.java">FileIO.java</A>class to append to the file.<P>Give it a try before taking a peek at the <A HREF="./Code/AppendIO.java">Solution</A>.<A NAME="more"></A><H3>More Information</H3>For more infomation on file input and output, see the <A HREF="http://java.sun.com/docs/books/tutorial/essential/io/index.html">Reading and Writing</A> trail in <A HREF="http://java.sun.com/docs/books/tutorial">The Java Tutorial</A>.<P>You can learn more 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">Solving Common Layout Problems</A>and<A HREF="http://java.sun.com/docs/books/tutorial/ui/swingOverview/layout.html">Layout Management</A>.<P ALIGN="RIGHT"><FONT SIZE="-1">[<A HREF="#top">TOP</A>]</FONT></FONT></TD></TR></TABLE><!-- ================ --><!-- End Main Content --><!-- ================ --></FONT></TD></TR></TABLE><!-- Copyright Insert --><BR CLEAR="ALL"><FORM ACTION="/cgi-bin/search.cgi" METHOD="POST"><TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="5"> <TR> <TD VALIGN="BOTTOM"></TD></TR><A HREF="/servlet/PrintPageServlet"><IMG SRC="/images/printbutton.gif" WIDTH="155" HEIGHT="25" ALT="Print Button" BORDER="0"></A> <CENTER> <FONT SIZE="-1" COLOR="#999999" FACE="Verdana, Arial, Helvetica, sans-serif"> [ This page was updated: <!-- new date --> 31-Mar-2000 ]</font></CENTER> </TD> </TR> <TR> <TD BGCOLOR="#CCCCCC"> <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR> <TD> <CENTER> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> <A HREF="http://java.sun.com/products/">Products & APIs</A> | <A HREF="/developer/index.html">Developer Connection</A> | <A HREF="/developer/infodocs/index.shtml">Docs & Training</A> | <A HREF="/developer/support/index.html">Online Support</A><BR> <A HREF="/developer/community/index.html">Community Discussion</A> | <A HREF="http://java.sun.com/industry/">Industry News</A> | <A HREF="http://java.sun.com/solutions">Solutions Marketplace</A> | <A HREF="http://java.sun.com/casestudies">Case Studies</A> </FONT> </CENTER> </TD> </TR> <TR> <TD BGCOLOR="#CCCCCC"> <IMG SRC="/images/pixel.gif" HEIGHT="1" WIDTH="1" ALT=""></TD> </TR> <TR> <TD ALIGN="CENTER"> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> <A HREF="http://java.sun.com/docs/glossary.html">Glossary</A> - <A HREF="http://java.sun.com/applets/">Applets</A> - <A HREF="http://java.sun.com/docs/books/tutorial/">Tutorial</A> - <A HREF="http://java.sun.com/jobs/">Employment</A> - <A HREF="http://java.sun.com/nav/business/">Business & Licensing</A> - <A HREF="http://java.sun.com/javastore/">Java Store</A> - <A HREF="http://java.sun.com/casestudies/">Java in the Real World</A> </FONT> </TD> </TR> <TR> <TD> <CENTER> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> <a href="/siteinfo/faq.html">FAQ</a> | <a href="/feedback/index.html">Feedback</a> | <a href="http://www.dynamicdiagrams.net/mapa/cgi-bin/help.tcl?db=javasoft&dest=http://java.sun.com/">Map</a> | <A HREF="http://java.sun.com/a-z/index.html">A-Z Index</A> </FONT> </CENTER> </TD> </TR> <TR> <TD> <TABLE WIDTH="100%" CELLPADDING="0" BORDER="0" CELLSPACING="0"> <TR> <TD WIDTH="50%"> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> For more information on Java technology<BR> and other software from Sun Microsystems, call:<BR> </FONT> <FONT SIZE="-1" FACE="Verdana, Arial, Helvetica, sans-serif"> (800) 786-7638<BR></FONT> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> Outside the U.S. and Canada, dial your country's <A HREF="http://www.att.com/business_traveler/attdirecttollfree/">AT&T Direct Access Number</A> first.<BR> </FONT> </TD> <TD ALIGN="RIGHT" WIDTH="50%"> <A HREF="http://www.sun.com"><IMG SRC="/images/lgsun.gif" width="64" height="30" border="0" ALT="Sun Microsystems, Inc."></A><BR> <FONT SIZE="-2" FACE="Verdana, Arial, Helvetica, sans-serif"> Copyright © 1995-2000 <A HREF="http://www.sun.com">Sun Microsystems, Inc.</A><BR> All Rights Reserved. <A HREF="http://www.sun.com/share/text/termsofuse.html">Terms of Use</A>. <A HREF="http://www.sun.com/privacy/">Privacy Policy</A>. </FONT> </TD> </TR> </TABLE> </TD> </TR> </TABLE></FORM><!-- End Copyright Insert --></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -