First, describe
After we use JFileChooser or file controls to open files in the Windows system directory, how to save our most recently opened file path so that each time we open the file, we can open the most recently opened file directory, instead of opening the C:\Users\ each time by default. Administrator\documents directory. We need to use the profile property file to record the most recently opened file path, read the most recent file directory in the configuration file before the file control opens it, save the path to the profile file after opening the file, and then write the profile after the action click OK button.
Second, the source code
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 () {profiles = new profile ()//()//() Create Configuration Profile object//Read the parameters profile in the configuration file every time you run the program and assign to Lates
Tpath, if the record is not in the configuration file, set a default path Latestpath = (Profile.read ()? Profile.latestpath: "d:/kkserver/mainserver/"); try{if (!new File (Latestpath). Exists ()) {Latestpath = "d:/kkserver/ Mainserver/"; Sets the default latest path} Filechooser = new JFileChooser (LATESTPATH);
Filter. log type of file 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 ();//Update directory Properties Profile.write (Latestpath) Every time you exit the file selector;
New Onlinecountdialog ();
}}catch (FileNotFoundException e) {e.printstacktrace ();
} public static void Main (string[] args) {new Countuserserver ();
Class profile{//sets the default latest path String Latestpath = "d:/kkserver/mainserver/";
Create the Setlatestpath.properties profile file = new file ("./setlatestpath.properties") under the current engineering directory;
Public profile () {} Boolean Create () {Boolean flag = true;
if (file!=null) { File directory = File.getparentfile (); Get the file's parent if (!directory.exists ()) {//parent directory does not exist flag = directory.mkdirs ();//create parent Directory} else{//Exists directory if (!file.exists ()) {//config file does not exist try {flag = fil
E.createnewfile ()///Create Profile} catch (IOException e) {flag = false;
}}} return flag; /** * Read the directory of the latest open files in the properties file * @return/public Boolean read () {Propertie s properties; Declares a property set FileInputStream InputStream = null; Declare file input stream Boolean b = true; Declares a Boolean return value if (!file.exists ()) {//config file does not exist when B = Create (); Calling the Create () method creates a profile if (b) {//config file is successfully created after B = Write (latestpath);//Call Write () will Lates
Tpath Write profile}else{//Create failure without configuration file pop-up dialog box prompts error Joptionpane.showconfirmdialog (NULL, "Sorry, no configuration file exists.")
"," error ", joptionpane.yes_no_option,joptionpane.error_message);
}else{try {inputstream = new FileInputStream (file);
Properties = new properties ();
Properties.load (InputStream)//Read Property Latestpath = Properties.getproperty ("Latestpath")//Read configuration parameter Latestpath value
Inputstream.close ();
}catch (IOException ex) {ex.printstacktrace ();
b = false;
} return B; /** * Saves the directory of the most recently opened files to the properties file * @param latestpath * @return/public Boolean WR
ITE (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);
Writes the attribute to Outputstream.flush ();
Outputstream.close ();
}catch (IOException IoE) {flag = false;
Ioe.printstacktrace ();
return flag; }
}
Third, summary
1, each time you open the file control or JFileChooser first read the profile file path information, if there is read, if not exist on the set a default path;
2. After selecting a file, the new file path is written to the profile file for the next read.