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

📄 readlocalfileapplet.java

📁 java的书上例子
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.applet.*;

/** 一个小应用程序,用来进行安全性测试。在Applet中读取一个本地文件,运行时需使用
  * 安全策略给出权限
  * @作者:尉哲明
  * @日期:2001年5月 */

/** 定义读取本地文件的Applet类 */
public class ReadLocalFileApplet extends Applet
{
	TextArea text=new TextArea();
	Panel panel=new Panel();
	Button button=new Button("Read Local File");
	String fileName="";
	
	/** init()方法,用来从Web页中得到本地文件名,并设置组件 */

	public void init()
	{
		fileName=getParameter("fileName");
		//得到HTML文件中给定的本地文件名
		System.out.println(fileName);
		setLayout(new BorderLayout());
		button.addActionListener(new ButtonHandler());
		panel.add(button);
		add("North",panel);
		add("Center",text);
	}

	/** 内部类用来处理按钮事件,主要将读取的本地文件内容显示在TextArea区域中 */
	
	class ButtonHandler implements ActionListener 
	{
	
		/** 动作处理方法 */
		public void actionPerformed(ActionEvent e)
		{//选择"Read Local File"按钮后将读取的本地文件内容显示在TextArea区域中
			String s=e.getActionCommand();
			if ("Read Local File".equals(s))
			{
				try
				{
					FileInputStream inStream=new FileInputStream(fileName);
					int inBytes=inStream.available();
					byte inBuf[]=new byte[inBytes];
					int byteRead=inStream.read(inBuf,0,inBytes);
					text.setText(new String(inBuf));
				}catch(Exception ex)
				{
					text.setText(ex.toString());
				}
			}
		}
	}
}

⌨️ 快捷键说明

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