Use the profile file to record the most recently opened file path for JFileChooser (or file control)

Source: Internet
Author: User

First, describe

After we use JFileChooser or the file control to open the files in the Windows system directory, how to save our recently opened file path, so that each time you open the file can open the last open file directory, rather than the default open C:\Users\ 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 after the file is opened, and write the profile when the Click OK button is finished.

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 ();//each time you run the program, create a Configuration profile object//Read the parameter profile in the configuration file and assign the value to LATESTP                  Ath, set a default path if there is no record in the configuration file Latestpath = (Profile.read ()? Profile.latestpath: "d:/kkserver/mainserver/"); try{if (!new File (Latestpath). Exists ()) {Latestpath = "D:/kkserver/mainser   Ver/";               Set the default up-to-date path} Filechooser = new JFileChooser (Latestpath); Filtering. LoType G file Filenameextensionfilter filter = new Filenameextensionfilter ("Text Files", "Log"); Filechooser.setfilefilter (  Filter); int returnvalue = Filechooser.showopendialog (null); if (returnvalue = = filechooser.approve_option) {choosefile = FileCho             Oser.getselectedfile ();  Latestpath = Choosefile.getparent ();//After each exit of the file selector, update the directory Properties Profile.write (Latestpath); New Onlinecountdialog ();}                    }catch (FileNotFoundException e) {e.printstacktrace ();              }} public static void Main (string[] args) {new Countuserserver ();       }} class profile{//Set the default up-to-date path String Latestpath = "d:/kkserver/mainserver/";         Create the Setlatestpath.properties profile in the current project directory file File = new file ("./setlatestpath.properties");           Public profile () {} Boolean Create () {Boolean flag = true;  if (file!=null) {File directory = File.getparentfile (); Get the parent directory of the file if (!directory.exists ()) {//parent directory does not existWhen flag = Directory.mkdirs ();               Create parent directory}else{//existence directory if (!file.exists ()) {//config file does not exist when try {              Flag = File.createnewfile ();//create config file} catch (IOException e) {flag = false;        }}}} return flag; /** * Reads the directory of the most recent open file in the properties file * @return */public Boolean read () {Properties prop Erties; Declares the attribute set FileInputStream InputStream = null;         Declares the file input stream Boolean b = true;         Declares the Boolean return value if (!file.exists ()) {///config file does not exist when B = Create (); Calling the Create () method creates a configuration file if (b) {////config file is created successfully b = Write (latestpath);//Call Write () to LATESTP Ath Write config file}else{//Create failed when configuration file is not present when Popup dialog prompt 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 the value of the configuration parameter Latestpath            Inputstream.close ();                 }catch (IOException ex) {ex.printstacktrace ();            b = false;        }} return B; /** * Save the directory of the most recent open file in the properties file * @param latestpath * @return */public Boolean write (St         Ring 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 properties to OutputStream. Flush ();         Outputstream.close ();          }catch (IOException IoE) {flag = false;         Ioe.printstacktrace ();    } return flag; }}

Iii. Summary

1, each time you open the file control or JFileChooser read the path information in the profile, if there is read, if it does not exist, set a default path;

2. After selecting a file, the new file path is written to the profile file for the next read.


Use the profile file to record the most recently opened file path for JFileChooser (or file control)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.