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;