Noob_Programmer
Noob_Programmer

Reputation: 111

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(); 

Upvotes: 0

Views: 1151

Answers (2)

cdMinix
cdMinix

Reputation: 675

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();

Upvotes: 1

peter.petrov
peter.petrov

Reputation: 39477

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

Upvotes: 1

Related Questions