Как прочитать файл из ресурсов?

Версия Minecraft
1.16.5
API
Forge
70
1
0
Делаю таким образом но не работает, похоже не так понял как нужно делать
Java:
if (Minecraft.getInstance().getLanguageManager().getSelected().getCode() == "ru_ru") {
            try {
                modeSplashes = (ArrayList<String>) Files.readAllLines(Paths.get("src/main/resources/Splashes/SplashesRu.txt"));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        else {
            try {
                modeSplashes = (ArrayList<String>) Files.readAllLines(Paths.get("src/main/resources/Splashes/SplashesEn.txt"));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
Вот такое расположение в проекте
1652804528623.png
При запуске игра выключается после загрузки
 
70
1
0
Вместо Files.readAllLines(Path) попробуй IOUtils.readLines(this.getClass().getResourceAsStream("src/main/resources/Splashes/SplashesRu.txt")), если твой метод статичный, то вместо this.getClass() используй НазваниеЕгоКласса.class.
'readLines(java.io.InputStream)' is deprecated
 
70
1
0

will0376

Токсичная личность
2,072
55
584
А вообще неправильно файл грузишь. Для ресурсов надо просить класслоадер подать файл
Java:
  // the stream holding the file content
  InputStream is = getClass().getClassLoader().getResourceAsStream("file.txt");

  // for static access, uses the class name directly
  InputStream is = JavaClassName.class.getClassLoader().getResourceAsStream("file.txt");
сус https://mkyong.com/java/java-read-a-file-from-resources-folder/
 
70
1
0
Java:
if (Minecraft.getInstance().getLanguageManager().getSelected().getCode() == "ru_ru") {
            try {
                modeSplashes = (ArrayList<String>) IOUtils.readLines(WWSplashes.class.getClass().getResourceAsStream("/src/main/resources/Splashes/SplashesRu.txt"), StandardCharsets.UTF_8);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        else {
            try {
                modeSplashes = (ArrayList<String>) IOUtils.readLines(WWSplashes.class.getClass().getResourceAsStream("/src/main/resources/Splashes/SplashesEn.txt"), StandardCharsets.UTF_8);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }



Вот так тоже пробовал, по нулям
Java:
if (Minecraft.getInstance().getLanguageManager().getSelected().getCode() == "ru_ru") {
            try {
                modeSplashes = (ArrayList<String>) IOUtils.readLines(WWSplashes.class.getClassLoader().getResourceAsStream("/src/main/resources/Splashes/SplashesRu.txt"), StandardCharsets.UTF_8);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        else {
            try {
                modeSplashes = (ArrayList<String>) IOUtils.readLines(WWSplashes.class.getClassLoader().getResourceAsStream("/src/main/resources/Splashes/SplashesEn.txt"), StandardCharsets.UTF_8);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
 
70
1
0
А, тьфу, сотри src/main/resources/)) Это не нужно, надо сразу от папки resources путь писать, слеш в начале строки оставь один: /Splashes/SplashesRu.txt
Вот так заработало
Java:
if (Minecraft.getInstance().getLanguageManager().getSelected().getCode() == "ru_ru") {
            try {
                modeSplashes = (ArrayList<String>) IOUtils.readLines(WWSplashes.class.getClassLoader().getResourceAsStream("/Splashes/SplashesRu.txt"), StandardCharsets.UTF_8);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        else {
            try {
                modeSplashes = (ArrayList<String>) IOUtils.readLines(WWSplashes.class.getClassLoader().getResourceAsStream("/Splashes/SplashesEn.txt"), StandardCharsets.UTF_8);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
 
1,074
72
372
Вот так заработало
За приведение в ArrayList<String> когда-нибудь ClassCastException словишь. Работай через интерфейсы, тебя не должно волновать ArrayList там или иная реализация, для работы хватит методов описанных в интерфейсе. Это более универсально.

От дублирования try-catch избавься, запихнув в него IF блок. Просто сохраняй в переменную имя нужного файла в переменную и потом его запрашивай. Будет компактно.
 
Сверху