package lab6;
import javax.swing.*;
import java.io.*;
import java.util.*;
import javax.swing.filechooser.*;
public class FileReading {
public static void main(String[] args) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Lab 6 (Select a file to read) - CSC 120 Josh Peel");
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"TXT - text files", "txt");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION){ //user picked something
FileReader myReader = new FileReader(chooser.getSelectedFile().getAbsolutePath());
}
else{
System.exit(0);
}
gives me the error:
Exception in thread "main" java.lang.Error:
Unresolved compilation problem:
Unhandled exception type FileNotFoundException
at lab6.FileReading.main(FileReading.java:15)
so I'm not quite sure how to bind the file chosen with jfilechooser to my file reader... I do not want to hardcode the file paths but am having a brainfart on a more universal apporach
still getting the same error with
if (returnVal == JFileChooser.APPROVE_OPTION){ //user picked something
String myChoice = chooser.getSelectedFile().getAbsolutePath();
FileReader myReader = new FileReader(myChoice);
as well as
if (returnVal == JFileChooser.APPROVE_OPTION){ //user picked something
File myChoice = chooser.getSelectedFile();
FileReader myReader = new FileReader(myChoice);