Why is the File im opening not found because the File is there

coolian :

I read in a file selected by a JFileChooser, which means that the file exists and I know it's there, but I still get a FileNotFoundException.

I hard-coded the path to this file and that works fine.


JFileChooser chooser = new JFileChooser();
int rueckgabeWert = chooser.showOpenDialog(null);

if (rueckgabeWert == JFileChooser.APPROVE_OPTION)
{
  filetoopen = chooser.getSelectedFile().getName();
  Path path = Paths.get(filetoopen);
  List<String> allLines = null;
  try
  {
    allLines = Files.readAllLines(path, StandardCharsets.UTF_8);
  }
  catch (IOException e1)
  {
    e1.printStackTrace();
  }

  for (int i = 0; i < allLines.size(); i++)
  {
    System.out.println(allLines.get(i));
  }

}

How can I get the file to open the file correctly?

herMa694 :

¿What is filetoopen, is it an file? By the line chooser.getSelectedFile().getName() you are only telling the JFileChooser to just get the name of the file, you should try with getAbsolutePath() instead of getName(). And also change chooser.showOpenDialog(null); by chooser.showOpenDialog(chooser);. I hope it helps you.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=116918&siteId=1