当前位置 : 主页 > 编程语言 > java >

《Java 核心技术 卷1》 笔记 第10章 部署应用程序和applet(5) 配置文件创建与保存

来源:互联网 收集:自由互联 发布时间:2022-07-13
10.2.1 沙箱 为了保证安全,必须进行数字签名 受限制的执行环境称为沙箱 总结那么多,主要是不能随意读本机的信息,只能访问有用的Java程序 10.2.2 签名代码 security all-permissions/ /secu


 

10.2.1 沙箱

为了保证安全,必须进行数字签名

受限制的执行环境称为沙箱

总结那么多,主要是不能随意读本机的信息,只能访问有用的Java程序

10.2.2 签名代码

<security>

<all-permissions/>

</security>

获取签名的引用程序用途:(其实意思就是不能保证安全性,只能保证未被篡改)

PS:个人建议,后续不用深入,既然知道不安全了,这个东西它比GUI的价值还低,到此处已经成功的不想看完这部分了。。。

10.4 应用程序存储的配置

10.4.1 属性映射

保存信息

Properties settings = new Properties();
settings.put("width","200");
settings.put("title","Hello, World");
FileOutputStream out = new FileOutputStream("program.properties");
settings.store(out, "Program Properties");

加载信息

FileInputStream in = new FileInputStream("program.properties");
settings.load(in);

作者示例:

这个例子做了下面这些工作:

public class Main {
public static void main(String[] args) throws CloneNotSupportedException, InterruptedException {
Main solution = new Main();

EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
PropertiesFrame frame = new PropertiesFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}

class PropertiesFrame extends JFrame{
private File propertiesFile;
private Properties settings;
public static final int W = 300;
public static final int H = 200;
public PropertiesFrame(){
String userDir = System.getProperty("user.home");
File proertiesDir = new File(userDir, ".corejava");
if(!proertiesDir.exists()){
proertiesDir.mkdir();
}

propertiesFile = new File(proertiesDir, "program.properties");

Properties defaultSettings = new Properties();
defaultSettings.put("left", "0");
defaultSettings.put("top","0");
defaultSettings.put("width",""+W);
defaultSettings.put("height",""+H);
defaultSettings.put("title","");

settings = new Properties(defaultSettings);
if(propertiesFile.exists()){
try{
FileInputStream in = new FileInputStream(propertiesFile);
settings.load(in);
}catch (IOException ex){
ex.printStackTrace();
}
}

int left = Integer.parseInt(settings.getProperty("left"));
int top = Integer.parseInt(settings.getProperty("top"));
int width = Integer.parseInt(settings.getProperty("width"));
int height = Integer.parseInt(settings.getProperty("height"));
setBounds(left,top,width,height);

String title = settings.getProperty("title");
if(title.equals(""))
title = JOptionPane.showInputDialog("Please supply a frame title:");
if(title == null) title = "";
setTitle(title);

addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
settings.put("left",""+getX());
settings.put("top",""+getY());
settings.put("width",""+getWidth());
settings.put("height",""+getHeight());
settings.put("title",""+getTitle());
try{
FileOutputStream out = new FileOutputStream(propertiesFile);
settings.store(out, "Program Properties");
}catch (IOException ex){
ex.printStackTrace();
}
System.exit(0);
}
});
}
} 【文章原创作者:ddos攻击防御 http://www.558idc.com/aqt.html欢迎留下您的宝贵建议】
网友评论