简体   繁体   中英

Selecting file using JFileChooser

Will these lines below help me to browse to a file and store the file content into the myFile variable?

Also, can someone please tell me what the following means?

JFrame frame = null; 

and

(System.getProperty( "user.dir" )

Code:

    JFrame frame = null; 
    JFileChooser fChoose = new JFileChooser(System.getProperty( "user.dir" ) );
    int returnVal = fChoose.showOpenDialog(frame);
    File myFile  = fChoose.getSelectedFile(); 

This

JFrame frame = null;

means you're declaring a JFrame variable and assigning it to null .

This

System.getProperty( "user.dir" )

means you're getting the user working directory.

See also:

http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html

On your main question, you should read some tutorial about JFrame and JFileChooser.

http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html

http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html

If you want to read text from the file, this would be your way to go:

FileInputStream fis = new FileInputStream(myFile);
BufferedReader stream = new BufferedReader(new InputStreamReader(fis, "ISO-8859-1"));
String line;
while ((line = stream.readLine()) != null) {
     //save your lines to an array or list       
}
stream.close();
fis.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM