Skip to content

Commit

Permalink
Added a resource conversion to read internal files when deployed as a…
Browse files Browse the repository at this point in the history
… jar file
  • Loading branch information
Nikollai committed May 26, 2026
1 parent fbd0d66 commit a61978a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/main/java/millions/controller/GameController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public void startGame(
throw new IllegalArgumentException("Pre run weeks cannot be negative");
}
try {
CSVFileHandler fileHandler = new CSVFileHandler();
List<Stock> stocks = fileHandler.getStocksFromFile(stockFilePath);
CSVFileHandler csvFileHandler = new CSVFileHandler();
List<Stock> stocks = csvFileHandler.getStocksFromFile(stockFilePath);

exchange = new Exchange("Exchange", stocks);
for (int i = 0; i < preRunWeeks; i++) {
Expand All @@ -39,10 +39,10 @@ public void startGame(

player = new Player(name, startingMoney);

} catch (FileNotFoundException e) {
throw new UncheckedFileNotFoundException(e.getMessage());
} catch (InvalidFormatException e) {
throw new InvalidFormatException(e.getMessage());
} catch (FileNotFoundException e) {
throw new UncheckedFileNotFoundException(e.getMessage());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package millions.controller.fileIO.CSV;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

public class InternalResourceUtil {
public File getFileFromJar(InputStream resource) throws IOException {
if (resource == null) {
throw new IOException("Resource not found is null");
}
File tempFile = File.createTempFile("javafx_app_", ".tmp");
tempFile.deleteOnExit();
Files.copy(resource, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
return tempFile;
}
}
14 changes: 9 additions & 5 deletions src/main/java/millions/view/StartView.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package millions.view;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URISyntaxException;
import java.net.URL;
Expand All @@ -17,6 +19,7 @@
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import millions.controller.fileIO.CSV.InternalResourceUtil;

/** The initial game setup screen where the player enters their info. */
public class StartView extends VBox {
Expand Down Expand Up @@ -107,12 +110,13 @@ public StartView(Stage stage) {

private File loadDefaultStocksFile() {
try {
URL resource = Objects.requireNonNull(getClass().getResource("/data/default-stocks.csv"));
return Paths.get(resource.toURI()).toFile();
} catch (URISyntaxException e) {
logger.log(Level.SEVERE, "Error accessing default stocks file", e);
throw new IllegalStateException("Could not load default stocks file", e);
InputStream resource = Objects.requireNonNull(getClass().getResourceAsStream("/data/default-stocks.csv"));
InternalResourceUtil converter = new InternalResourceUtil();
return converter.getFileFromJar(resource);
} catch (IOException e) {
logger.log(Level.SEVERE, "error loading default file: ", e);
}
return null;
}

/** Enables/Disables start button */
Expand Down

0 comments on commit a61978a

Please sign in to comment.