Use of File selection box JFileChooser

Source: Internet
Author: User

Reprint other people's articles, for the Java File selection box JFileChooser make a simple note.


Usually we use JFileChooser to assist in the completion of file reading and writing.

The steps to use JFileChooser are as follows: 1. Declare a JFileChooser object 2. Add a file filter for it, of course this is not necessary. 3. Call its ShowDialog method and return an integer value, based on which you can determine the user's choice, and if the user does not select Cancel, then you can then obtain basic information about the selected file, such as the path, file name, and so on, based on JFileChooser.

JFileChooser filechooser = new JFileChooser ();  
Two ways to add a filter:  
filenameextensionfilter ff = new Filenameextensionfilter (null, "TXT");  
Filechooser.setfilefilter (FF);  
Law II:  
filechooser.addchoosablefilefilter (New Javax.swing.filechooser.FileFilter () {  
                      
@Override  
public String getdescription () {return  
    "*.txt";  
}  
                      
@Override Public  
Boolean accept (File arg0) {return  
                                arg0.getname (). EndsWith (". txt");  
            }  
);  
This completes the basic setup, below can use this to complete our request
int option = Filechooser.showopendialog (null);  
if (option = jfilechooser.approve_option) {  
//Get basic information  
System.out.println ("Path:" +filechooser.getselectedfile (). GetPath ());  
SYSTEM.OUT.PRINTLN ("Absolute path:" +filechooser.getselectedfile (). GetAbsolutePath ());  
System.out.println ("File name:" +filechooser.getselectedfile (). GetName ());  
String path = Filechooser.getselectedfile (). GetAbsolutePath ();  
  
Read into  
filereader freader = new FileReader (path);  
BufferedReader breader = new BufferedReader (freader);  
arraylist<string> data =  new arraylist<string> ();  
String row = null;  
while ((row = Breader.readline ())!=null) {  
    row = Row.replaceall ("\\s*", "");//Remove Space  
    data.add (row);  
}  
  

Transferred from http://miman2008.iteye.com/blog/816035. Add, note Import header file:
Import Javax.swing.JFileChooser;
Import Javax.swing.filechooser.FileNameExtensionFilter;

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.