Question

Hey guys one of the aspects of a program of mine opens .txt files via the OpenDialog javax command. My bufferedreader works fine in my developing environment but when I try to export the project to a .jar my program doesn't seem to load in the data. I can still access the openDialog subview, pick the file, etc. but the file won't load and I'm not sure how to debug a jar so I'm unsure on whether the error is a bufferedreader or from the dialog itself. Like I said the program works fine when I compile and run BLueJ but for some reason when I create the .jar it stops after opendialog. Anyone familiar with this type of error?

For explanation, the method adds a deck of cards from a text file already shuffled. Card is declared as an ArrayList

        card.clear();
        JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(null);
        String filename = chooser.getSelectedFile().getName();
        try
        {
            FileReader reader = new FileReader(filename);
            BufferedReader breader = new BufferedReader(reader);
            String line;
            while ((line = breader.readLine()) != null) 
            {
                card.add(line);
                displayField.append("\nAdded a " + line);
            }
            displayField.append("\nDeck Size:  " + card.size());
            breader.close();
        } catch (FileNotFoundException e) 
        {
            System.err.println("FileNotFoundException: " + e.getMessage());
        } catch(IOException ex) 
        {
            System.out.println(
            "Error reading file '" 
            + filename + "'");  
        }
Was it helpful?

Solution

Here is your problem:

String filename = chooser.getSelectedFile().getName();
        try
        {
            FileReader reader = new FileReader(filename);

You need to specify a path. You are getting the filename of the selected file, but disregarding the path. In this case, FileReader will use the current working directory of your process as the file path.

Try this...

File selectedFile = chooser.getSelectedFile();

... then you can ...

FileReader reader = new FileReader(selectedFile);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top