spencer.sm
spencer.sm

Reputation: 20624

Throws FileNotFoundException when creating scanner from file

I'm trying to show a file open dialog and then create a scanner to read the selected file. When I run my code it throws a FileNotFoundException which doesn't make sense to me since it throws the exception before it opens the file selector window.

package files;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class FileManipulations {

    public static void main (String[] args) {
        SwingUtilities.invokeLater(() -> runGUI());
    }

    public static void runGUI () {
        JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(null);
        File file = chooser.getSelectedFile();
        System.out.println(file.exists());
        Scanner fromFile = new Scanner(file);
    }
}

Upvotes: 0

Views: 1043

Answers (2)

hunters30
hunters30

Reputation: 521

I think your code is somewhat as follows :

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

import javax.swing.*;

public class Solutiokn {

    public static void main(String[] args) {
           JFileChooser fileChoose = new JFileChooser();
           fileChoose.showOpenDialog(null);
           File file = fileChoose.getSelectedFile();
           System.out.println(file.exists());
           try {
            Scanner fromFile = new Scanner(file);
            while(fromFile.hasNextLine())
                System.out.println(fromFile.nextLine());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }
}

Ok now this works just fine. Only point when I have issue is when I do not pick a file in the file chosser and just click cancel. When this happens the scanner itself is not created as value in "file" is null. I suggest adding a check manually to see if any file was selected or not like if(file==null) System.out.println("Please select a file"); or throw your own exception. If you don't probably a sudden nullPointerException will pop up. Edit : As madProgrammer pointed out file.getName() would rather instead throw nullPointerException I tried changing it to file.exists() that still throws Exception if file was not selected. So instead maybe checking file==null is better. file.exists() checks if file denoted by this pathname exists but if we never inititalized it i.e just cancelled in file chooser then it does not help.

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347334

Starting with your example code...

import java.io.File;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;

public class FileManipulations {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> runGUI());
    }

    public static void runGUI() {
        JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(null);
        File file = chooser.getSelectedFile();
        System.out.println(file.exists());
        Scanner fromFile = new Scanner(file);
    }
}

You are getting a compiler error because Scanner(File) can throw a FileNotFoundException

You either need to catch the exception or re-throw it, for example...

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.SwingUtilities;

public class FileManipulations {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> runGUI());
    }

    public static void runGUI() {
        JFileChooser chooser = new JFileChooser();
        switch (chooser.showOpenDialog(null)) {
            case JFileChooser.APPROVE_OPTION:
                File file = chooser.getSelectedFile();
                System.out.println(file.exists());
                try (Scanner fromFile = new Scanner(file)) {
                    while (fromFile.hasNextLine()) {
                        String text = fromFile.nextLine();
                        System.out.println(text);
                    }
                } catch (FileNotFoundException exp) {
                    exp.printStackTrace();
                }
                break;
        }
    }
}

You might also like to have a look at How to Use File Choosers and make sure you are checking the return result of showOpenDialog so you know how the dialog was closed

Also, have a look at The try-with-resources Statement for more details about how to manage your resources

Upvotes: 2

Related Questions