栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

 

10.2.1 沙箱

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

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

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

10.2.2 签名代码

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

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);
            }
        });
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/768404.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号