" because i am trying to select file using JFileChoosear "
The JFileChooser
will return a File
object if APPROVE_OPTION
is the return value. You can use chooser.getSelectedFile()
then pass it to the Scanner
JFileChooser chooser = new JFileChooser();
File file = null;
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
}
try {
Scanner scanner = new Scanner(file);
// read file
} catch (Exception ex) {
ex.printStackTrace();
}
Run this
import java.io.File;
import java.util.Scanner;
import javax.swing.JFileChooser;
public class TestFileChooser {
public static void main(String[] args) {
JFileChooser chooser = new JFileChooser();
File file = null;
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = chooser.getSelectedFile();
}
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}