0

I have a swing code. In which I will select a file and I want to pass that file to a method as a string. how to do it? Please give me a simple solution.This code should be in java

Ok I got the answer just follow this answer

Reading a txt file in a Java GUI gui/13667222#13667222

Community
  • 1
  • 1
Murali
  • 774
  • 6
  • 12

2 Answers2

7

please read about File Choosers. They're easy to use and reuse.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
  • Yeah I know file choosers but how to pass that selected file to a method as string? will toString() helps? – Murali Oct 20 '12 at 06:48
  • 2
    *"will toString() helps?"* What happened when ***you tried it?*** – Andrew Thompson Oct 20 '12 at 06:49
  • 2
    you get File object from JFileChooser.getSelectedFile(). Please read the documentation. We can get the file-name as a String from the File object. READ the documentation – Aniket Inge Oct 20 '12 at 06:50
1

It depends, do you want the name of the file or the whole path...

File#getName() will return just the name of the file, without the path where as File#getPath() will return the path and name

Ten seconds reading the API docs would have given you the answer ;)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366