Java使用Preference类保存上一次记录的方法
本文实例讲述了Java使用Preference类保存上一次记录的方法。分享给大家供大家参考。具体分析如下:
在使用java中JFileChooser选择文件的时候,我们总希望在下次打开的时候能保存上次浏览的记录,即打开文件对话框的时候,总能追溯到上一次的路径。
有一个很愚蠢的方法,那就是在每次打开的时候把选择的文件的路径保存到本地文件中,再打开JFileChooser对话框的时候,先查看是否有内容,如果文件中有内容则按照存储的路径打开对话框。
如果我说Java里面可以不使用JNI的手段操作Windows的注册表你信不信?很多软件的菜单里都有“Setting”或“Preferences”这样的选项用来设定或修改软件的配置,这些配置信息可以保存到一个像上面所述的配置文件当中,如果是Windows平台下,也可能会保存到系统注册表中。从JDK1.4开始,Java在java.util下加入了一个专门处理用户和系统配置信息的java.util.prefs包,其中一个类Preferences是一种比较“高级”的玩意。
从本质上讲,Preferences本身是一个与平台无关的东西,但不同的OS对它的SPI(ServiceProviderInterface)的实现却是与平台相关的,因此,在不同的系统中你可能看到首选项保存为本地文件、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类的用法,代码来自网上
importjava.util.prefs.Preferences;
publicclassPreferrenceTest{
privatePreferencesprefs;
publicvoidsetPreference(){
//Thiswilldefineanodeinwhichthepreferencescanbestored
prefs=Preferences.userRoot().node(this.getClass().getName());
StringID1="Test1";
StringID2="Test2";
StringID3="Test3";
//Firstwewillgetthevalues
//Defineabooleanvalue
System.out.println(prefs.getBoolean(ID1,true));
//Defineastringwithdefault"HelloWorld
System.out.println(prefs.get(ID2,"HelloWorld"));
//Defineaintegerwithdefault50
System.out.println(prefs.getInt(ID3,50));
//Nowsetthevalues
prefs.putBoolean(ID1,false);
prefs.put(ID2,"HelloEuropa");
prefs.putInt(ID3,45);
//Deletethepreferencesettingsforthefirstvalue
prefs.remove(ID1);
System.out.println(prefs.get(ID2,""));
}
publicstaticvoidmain(String[]args){
PreferrenceTesttest=newPreferrenceTest();
test.setPreference();
}
}
这里演示一下如何实现选择文件保存上一次路径
Preferencespref=Preferences.userRoot().node(this.getClass().getName());
StringlastPath=pref.get("lastPath","");
JFileChooserchooser=null;
if(!lastPath.equals("")){
chooser=newJFileChooser(lastPath);
}
else
chooser=newJFileChooser();
//MyFileFilter是自己写的一个文件过滤类,只接受xls格式文件
MyFileFilterfilter=newMyFileFilter("xls","只接受xls格式文件,即Excel2003版文件");
chooser.setFileFilter(filter);
intstate;//文件选择器返回状态
state=chooser.showOpenDialog(null);//显示打开文件对话框
Filefile=chooser.getSelectedFile();//得到选择的文件
pref.put("lastPath",file.getPath());
importjava.io.File;
importjavax.swing.filechooser.FileFilter;
//文件过滤器
publicclassMyFileFilterextendsFileFilter
{
publicStringends;//文件后缀
publicStringdescription;//文件描述文字
publicMyFileFilter(Stringends,Stringdescription)
{//构造函数
this.ends=ends;//设置文件后缀
this.description=description;//设置文件描述文字
}
publicbooleanaccept(Filefile)
{//重载FileFilter中的accept方法
if(file.isDirectory())//如果是目录,则返回true
returntrue;
StringfileName=file.getName();//得到文件名称
if(fileName.toUpperCase().endsWith(ends.toUpperCase()))
//把文件后缀与可接受后缀转成大写后比较
returntrue;
else
returnfalse;
}
publicStringgetEnds(){
returnends;
}
publicvoidsetEnds(Stringends){
this.ends=ends;
}
publicStringgetDescription(){
returndescription;
}
publicvoidsetDescription(Stringdescription){
this.description=description;
}
}
希望本文所述对大家的java程序设计有所帮助。