0

I built a JavaFX project that is supposed to read .txt files into TextArea and when I run the project inside IntelliJ, everything works properly, But when I do Artifact, the .jar can't find the requested file. Which way can you get the .jar file to read the .txt file after exporting?

My project layout.

My JavaFX Controller looks like this:

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXComboBox;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Controller {

    ObservableList<String> days = FXCollections.observableArrayList
            ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
            "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
            "21", "22", "23", "24", "25", "26", "27", "28", "29", "30"),
            months = FXCollections.observableArrayList
            ("Tishrei", "Cheshvan", "Kislev", "Tevet", "Shevat", "Adar",
            "Nisan", "Iyar", "Sivan", "Tammuz", "Av", "Elul");

    public JFXComboBox day, month;
    public JFXButton ok;
    public ScrollPane scrollPane;
    public TextArea textArea;

    @FXML
    private void initialize(){
        day.setItems(days);
        month.setItems(months);
    }

    public void okClick() {
        String dayValue = (String) day.getValue();
        String monthValue = (String) month.getValue();
        File dayFile = new File("src/textFiles/days/"+dayValue+".txt");
        File monthFile = new File("src/textFiles/months/"+monthValue+".txt");
        String dayTitle = ("Day " + dayValue + ":\n");
        String monthTitle = ("Month " + monthValue + ":\n");
        try {
            String scanDayFile  = new Scanner(dayFile).useDelimiter("\\Z").next();
            String scanMonthFile  = new Scanner(monthFile).useDelimiter("\\Z").next();
            textArea.setText(dayTitle + scanDayFile + "\n\n" + monthTitle + scanMonthFile);
        }

        catch (FileNotFoundException e){
            System.out.println("file not found");
        }
    }
}

Thanks in advance for any help you can give.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
chaim770
  • 49
  • 1
  • 1
  • 8
  • 1
    Resources are _not_ files. – Slaw Mar 04 '20 at 00:19
  • The answer found in a post that @Sedrick suggested works well, but I now have a problem with non-ASCII characters, which are presented in .jar as gibberish. How can this be corrected? – chaim770 Mar 04 '20 at 17:09
  • The first steps is to use a program like notepad++ to see if you can determine the encoding of the file. After that, do a search on how to read a file with that encoding set. If you can not figure it out, ask a new question. – SedJ601 Mar 04 '20 at 18:14
  • Open an `InputStream` to the resource then wrap it in an `InputStreamReader` using the necessary `Charset`. – Slaw Mar 04 '20 at 20:57

0 Answers0