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

