一、描述
我们使用JFileChooser或者File控件打开Windows系统目录下的文件之后,如何保存我们最近打开的文件路径,使得每次打开文件就能打开最近一次打开的文件目录,而不是每次默认打开C:\Users\Administrator\Documents目录。我们需要使用Profile属性文件来记录最近打开的文件路径,在File控件打开文件前先读取配置文件中的最近文件目录,在打开文件后将路径保存到Profile文件中,在操作完毕点击确定按钮后写入Profile配置文件。
二、源代码
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
public class CountUserServer{
public static File chooseFile;
private static String latestPath ;
private static Profile profile;
private static JFileChooser fileChooser;
public CountUserServer (){
profile = new Profile();//每次运行程序时创建配置文件Profile对象
//读取配置文件里的参数Profile并赋值给latestPath,如果配置文件中没有该记录则设置一个默认路径
latestPath = (profile.read()?profile.latestPath:"D:/KKServer/MainServer/");
try{
if(!new File(latestPath).exists()){
latestPath = "D:/KKServer/MainServer/"; //设置默认的最新路径
}
fileChooser = new JFileChooser(latestPath);
//过滤.log类型的文件
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files", "log");
fileChooser.setFileFilter(filter);
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == fileChooser.APPROVE_OPTION) {
chooseFile = fileChooser.getSelectedFile();
latestPath = chooseFile.getParent();//每次退出文件选择器后更新目录Properties
profile.write(latestPath);
new OnlineCountDialog();
}
}catch(FileNotFoundException e){
e.printStackTrace();
}
}
public static void main(String[] args){
new CountUserServer();
}
}
class Profile{
//设置默认的最新路径
String latestPath = "D:/KKServer/MainServer/";
//在当前工程目录下创建setLatestPath.properties配置文件
File file = new File("./setLatestPath.properties");
public Profile(){}
boolean create(){
boolean flag = true;
if(file!=null){
File directory = file.getParentFile(); //获得文件的父目录
if(!directory.exists()){ //父目录不存在时
flag = directory.mkdirs(); //创建父目录
}else{ //存在目录
if(!file.exists()){//配置文件不存在时
try {
flag = file.createNewFile();//创建配置文件
} catch (IOException e) {
flag = false;
}
}
}
}
return flag;
}
/**
* 读取属性文件中最新打开文件的目录
* @return
*/
public boolean read(){
Properties properties; //声明属性集
FileInputStream inputStream = null; //声明文件输入流
boolean b = true; //声明boolean返回值
if(!file.exists()){ //配置文件不存在时
b = create(); //调用create()方法创建一个配置文件
if(b) { //配置文件创建成功后
b = write(latestPath);//调用write()将latestPath写入配置文件
}else{
//创建失败即不存在配置文件时弹出对话框提示错误
JOptionPane.showConfirmDialog(null, "对不起,不存在配置文件!", "错误",JOptionPane.YES_NO_OPTION,JOptionPane.ERROR_MESSAGE);
}
}else{
try {
inputStream = new FileInputStream(file);
properties = new Properties();
properties.load(inputStream);//读取属性
latestPath = properties.getProperty("latestPath");//读取配置参数latestPath的值
inputStream.close();
}catch (IOException ex) {
ex.printStackTrace();
b = false;
}
}
return b;
}
/**
* 将最新打开文件的目录保存到属性文件中
* @param latestPath
* @return
*/
public boolean write(String latestPath){
this.latestPath = latestPath;
Properties properties = null;
FileOutputStream outputStream = null;
boolean flag = true;
try {
outputStream = new FileOutputStream(file);
properties = new Properties();
properties.setProperty("latestPath",latestPath);
properties.store(outputStream,null); //将属性写入
outputStream.flush();
outputStream.close();
}catch (IOException ioe) {
flag = false;
ioe.printStackTrace();
}
return flag;
}
}
三、总结
1、每次打开File控件或者JFileChooser时先读取Profile文件中的路径信息,如果存在就读取,如果不存在就设置一个默认的路径;
2、选择某个文件后就将新的文件路径写入Profile文件,以便下次读取。