Reading a File Through JFileChooser

Hi Friends

i want to Read a file through JFileChooser and Display the Content on the Console Screen.

My code is this

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

publicclass FileExampleextends JDialogimplements ActionListener

{

JTextField txtLocation;

JButton btnBrowse;

BufferedReader inputStream =null;

PrintWriter outputStream =null;

public FileExample()

{

setSize(500,300);

txtLocation=new JTextField(30);

btnBrowse =new JButton("Browse");

btnBrowse.addActionListener(this);

JPanel pnl =new JPanel();

pnl.add(txtLocation);

pnl.add(btnBrowse);

getContentPane().add(pnl);

}

publicstaticvoid main(String arg[])

{

FileExample Exm=new FileExample();

Exm.setVisible(true);

}

publicvoid actionPerformed(ActionEvent ae)

{

Object obj=ae.getSource();

if(obj==btnBrowse)

{

final JFileChooser fcstudent =new JFileChooser();

int rtrnval = fcstudent.showOpenDialog(this);

if(rtrnval==JFileChooser.APPROVE_OPTION)

{

try

{

File file=fcstudent.getSelectedFile();

String path = file.getPath();

System.out.println("This is Path:"+path);

txtLocation.setText(""+path);

String FilePath=txtLocation.getText();

String result;

inputStream =new BufferedReader(new FileReader("FilePath"));

while((result=inputStream.readLine())!=null)

{

String str[];

str=result.split(",");

for(int i=0;i<5;i++)

{

System.out.println(str[i]);

}

}

}

catch(Exception ex)

{

ex.printStackTrace();

}

}

}

}

}

In this Program i am displaying the JFileChooser through A Browse Button and get the Selected File. with that File i get the Path of that File and then Pass the Path to the inputStream =new BufferedReader(new FileReader("FilePath"));

.

Now i don't know, what is the Problem exactly. It gives The System can't Find the Specified File.

What is the Problem exactly, May be the Path Problem or what.

If it is the path Problem please tell how to get the Exact Path of a file and pass that Selected File's Path to the BufferReader and Read the File.

Please help me out on the Issue

I will be greatful to you all on this Issue

Thank you for your Service

Cheers

Jofin

[4457 byte] By [jofin123a] at [2007-11-27 2:43:55]
# 1

> inputStream = new BufferedReader(new FileReader("FilePath"))

Hard coding the file name kind of defaults the purpose of using a file choose to the the file name. The File object contains the name of the file the user selected.

camickra at 2007-7-12 3:10:00 > top of Java-index,Desktop,Core GUI APIs...
# 2

Hi

please help me to get the Selected File and Using the that file in thisinputStream = new BufferedReader(new FileReader("path"));

.

I hava a File called namelist.txt. i want to select that file through JFileChooser and Read the Content of that File.

When i keep this File namelist.txt. in the Current Directory where i work. and specify the Name in this inputStream = new BufferedReader(new FileReader("namelist.txt"));

. It works for me. But when i select the File Through JFileChooser it Says the Specified File is not Found

Please run My code what i posted and help me to do this.

Thank you for your service

Cheers

Jofin

jofin123a at 2007-7-12 3:10:00 > top of Java-index,Desktop,Core GUI APIs...
# 3
inputStream = new BufferedReader(new FileReader(FilePath));It's OK
GanHaitiana at 2007-7-12 3:10:00 > top of Java-index,Desktop,Core GUI APIs...