当前位置: 移动技术网 > IT编程>开发语言>Java > Java使用Preference类保存上一次记录的方法

Java使用Preference类保存上一次记录的方法

2019年07月22日  | 移动技术网IT编程  | 我要评论

本文实例讲述了java使用preference类保存上一次记录的方法。分享给大家供大家参考。具体分析如下:

在使用java中jfilechooser选择文件的时候,我们总希望在下次打开的时候能保存上次浏览的记录,即打开文件对话框的时候,总能追溯到上一次的路径。

有一个很愚蠢的方法,那就是在每次打开的时候把选择的文件的路径保存到本地文件中,再打开jfilechooser对话框的时候,先查看是否有内容,如果文件中有内容则按照存储的路径打开对话框。

如果我说java里面可以不使用jni的手段操作windows的注册表你信不信?很多软件 的菜单里都有“setting”或“preferences”这样的选项用来设定或修改软件的配置,这些配置信息可以保存到一个像上面所述的配置文件当 中,如果是windows平台下,也可能会保存到系统注册表中。从jdk 1.4开始,java在java.util下 加入了一个专门处理用户和系统配置信息的java.util.prefs包, 其中一个类preferences是 一种比较“高级”的玩意。

从本质上讲,preferences本身是一个与平台无关的东西,但不同的os对它的 spi(service provider interface)的实现却是与平台相关的,因此,在不同的系统中你可能看到首选项保存为本地文件、 ldap目录项、数据库条目等,像在windows平台下,它就保存到了系统注册表中。不仅如此,你还可以把首选项导出为xml文件或从xml文件导 入。

① systemnodeforpackage() //根据指定的class对象得到一个preferences对象,这个对象的注册表路径是从 “hkey_local_machine\”开始的

② systemroot() //得到以注册表路径hkey_local_machine\software\javasoft \prefs 为根结点的preferences对象

③ usernodeforpackage() //根据指定的class对象得到一个preferences对象,这个对象的注册表路径 是从“hkey_current_user\”开始的

④ userroot() //得到以注册表路径hkey_current_user\software\javasoft \prefs 为根结点的preferences对象

下面代码简单演示了preference类的用法,代码来自网上

import java.util.prefs.preferences;
public class preferrencetest { 
private preferences prefs;
public void setpreference() { 
// this will define a node in which the preferences can be stored 
prefs = preferences.userroot().node(this.getclass().getname()); 
string id1 = "test1"; 
string id2 = "test2"; 
string id3 = "test3";
// first we will get the values 
// define a boolean value 
system.out.println(prefs.getboolean(id1, true)); 
// define a string with default "hello world 
system.out.println(prefs.get(id2, "hello world")); 
// define a integer with default 50 
system.out.println(prefs.getint(id3, 50));
// now set the values 
prefs.putboolean(id1, false); 
prefs.put(id2, "hello europa"); 
prefs.putint(id3, 45);
// delete the preference settings for the first value 
prefs.remove(id1); 
system.out.println(prefs.get(id2, "")); 
}
public static void main(string[] args) { 
preferrencetest test = new preferrencetest(); 
test.setpreference(); 
} 
}

这里演示一下如何实现选择文件保存上一次路径

preferences pref = preferences.userroot().node(this.getclass().getname()); 
string lastpath = pref.get("lastpath", ""); 
   jfilechooser chooser = null; 
   if(!lastpath.equals("")){ 
   chooser = new jfilechooser(lastpath); 
   } 
   else 
   chooser=new jfilechooser();
//myfilefilter 是自己写的一个文件过滤类,只接受xls格式文件
    myfilefilter filter = new myfilefilter("xls","只接受xls格式文件,即excel 2003版文件");
   chooser.setfilefilter(filter);
 int state; //文件选择器返回状态
 state=chooser.showopendialog(null);//显示打开文件对话框
 file file = chooser.getselectedfile(); //得到选择的文件
 pref.put("lastpath",file.getpath());
import java.io.file;
import javax.swing.filechooser.filefilter;
//文件过滤器
public class myfilefilter extends filefilter
{
 public string ends; //文件后缀
 public string description; //文件描述文字
  public myfilefilter (string ends, string description)
  { //构造函数
    this.ends = ends; //设置文件后缀
    this.description=description; //设置文件描述文字
  }
  public boolean accept (file file)
  { //重载filefilter中的accept方法
    if (file.isdirectory ()) //如果是目录,则返回true
      return true;
    string filename = file.getname (); //得到文件名称
    if (filename.touppercase ().endswith (ends.touppercase ()))
    //把文件后缀与可接受后缀转成大写后比较
      return true;
    else
      return false;
  }
 public string getends() {
 return ends;
 }
 public void setends(string ends) {
 this.ends = ends;
 }
 public string getdescription() {
 return description;
 }
 public void setdescription(string description) {
 this.description = description;
 }
}

希望本文所述对大家的java程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网