JFileChooser returns wrong file name?

final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(this);

if (returnVal == JFileChooser.APPROVE_OPTION) {
    String fileName = fc.getSelectedFile().getName();
    String path = (new File(fileName)).getAbsolutePath();

The absolute path I get is the concatenation of the project directory and fileName!


JFileChooser.getSelectedFile() return the File object.

Why are you getting the file name and instantiating a new File object again?

Can you try:


That's what getAbsolutePath() does - gets the full path, including drive letter (if you're on Windows), etc. What are you trying to get, just the file name?

After you initialize your File object, you can get just the file name from that, OR you can use JFileChooser.getSelectedFile()

If you're getting /path/to/filefilename but you're expecting /path/to/file/filename then you can add an extra slash to the path as appropriate.

Sure. Because you created new file new File(fileName) using returned filename, that means relative path. Use fc.getSelectedFile().getPath() or fc.getSelectedFile().getAbsolutePath() instead.

Need Your Help

How to intercept stdout from Python shell exec using subprocess.call?

python shell python-2.7 exec subprocess

I want to create a function that will return output of a shell, I have something like this:

Login successful but unable to open the next activity

android login android-activity

I have login successfully without the intent. But when I add the Customer Activity intent it gives me an force closed error when trying to login. I have a listview of 5 diff. activities within the

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.