NetBeans IDE 8.2正在从src文件夹中读取文件而没有任何问题。当我希望将文件写回同一文件时,NetBeans将其写入build文件夹,而不是回到src文件夹。
文件位于
下\ ProjectName \ src \ main \ resources \ player.csv
并从文件PresentationModel.java中触发,该文件位于
中\项目名\ SRC \主\ presentationmodel \ PresentationModel.java
private final String fileName = "../resources/player.csv";
private Path getPath(String fileName, boolean locatedInSameFolder) {
try {
if (!locatedInSameFolder) {
fileName = "/" + fileName;
}
return Paths.get(getClass().getResource(fileName).toURI());
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
}
private Stream<String> getStreamOfLines(String fileName, boolean locatedInSameFolder) {
try {
return Files.lines(getPath(fileName, locatedInSameFolder), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
private List<Player> readFromFile() {
try (Stream<String> stream = getStreamOfLines(fileName, true)) {
return stream.skip(1)
.map(s -> new Player(s.split(";")))
.collect(Collectors.toList());
}
}
保存功能是:
public void save() {
try (BufferedWriter writer = Files.newBufferedWriter(getPath(fileName, true))) {
writer.newLine();
data.stream()
.map(player -> player.infoAsLine())
.forEach(line -> {
try {
writer.write(line);
writer.newLine();
} catch (IOException e) {
throw new IllegalStateException(e);
}
});
} catch (IOException e) {
throw new IllegalStateException("save failed");
Logger.getLogger(PresentationModel.class.getName()).log(Level.SEVERE, null, ex);
}
}
当我触发保存功能时,我得到以下文件夹结构:
项目名\建立\资源\主\资源\ player.csv
当我重新运行程序时,文件未被修改,因为它是从src文件夹中重新获取的。
有谁知道如何解决这个问题?
最诚挚的问候。