Use the Profile File to record the latest File path opened by JFileChooser (or File control,
I. Description
After we use the JFileChooser or File control to open files in the Windows System directory, how can we save the path of the File we recently opened so that each time we open the File, we can open the directory of the last opened File, instead of opening the C: \ Users \ Administrator \ Documents directory by default each time. We need to use the Profile attribute File to record the path of the most recently opened File. Before the File control opens the File, read the latest File directory in the configuration File, after opening the file, save the path to the Profile file. After the operation is complete, click "OK" and write it to the Profile configuration file.
Ii. 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 stat Ic Profile profile; private static JFileChooser fileChooser; public CountUserServer () {profile = new Profile (); // create a Profile object each time you run the program // read the parameter Profile in the configuration file and assign it to latestPath, if this record is not found 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/"; // set the default latest path} fileChooser = new JFileChooser (latestPath); // filter. 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 the directory Properties profile after exiting the file selector each time. write (latestPath); new OnlineCountDialog () ;}} catch (FileNotFoundException e) {e. printStackTrace () ;}} public static void main (String [] args) {new CountUserServer () ;}} class Profile {// set the default latest path String latestPath = "D: /KKServer/MainServer/"; // create setLatestPath in the current project directory. properties configuration File file = new File (". /setLatestPath. prop Erties "); public Profile () {}boolean create () {boolean flag = true; if (file! = Null) {File directory = file. getParentFile (); // obtain the parent directory of the File if (! Directory. exists () {// if the parent directory does not exist, flag = directory. mkdirs (); // create the parent directory} else {// if (! File. exists () {// when the configuration file does not exist, try {flag = file. createNewFile (); // create a configuration file} catch (IOException e) {flag = false ;}}} return flag ;} /*** read the latest opened file directory in the property file * @ return */public boolean read () {Properties properties Properties; // declare the attribute set FileInputStream inputStream = null; // declare the object input stream boolean B = true; // declare the boolean return value if (! File. exists () {// when the configuration file does not exist, B = create (); // call the create () method to create a configuration file if (B) {// B = write (latestPath) after the configuration file is created successfully; // call write () write latestPath to the configuration file} else {// when the creation fails, the error JOptionPane is displayed in the pop-up dialog box. 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 the attribute latestPath = properties. getProperty ("latestPath"); // read the value of the Configuration Parameter latestPath inputStream. close ();} catch (IOException ex) {ex. printStackTrace (); B = false;} return B ;} /*** Save the directory of the latest open file to the property file * @ 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); // write attributes to outputStream. flush (); outputStream. close ();} catch (IOException ioe) {flag = false; ioe. printStackTrace ();} return flag ;}}
Iii. Summary
1. Read the path information in the Profile File every time you open the File control or JFileChooser. If yes, read the path. If no, set a default path;
2. Select a file and write the new file path to the Profile file for the next read.