简体   繁体   English

使用 Java/Maven 将数据存储在文本文件中

[英]Storing data in text files with Java/Maven

I wrote a little Java app for analyzing .csv files.我写了一个小的 Java 应用程序来分析 .csv 文件。 Now I want to keep reading from and writing to a .txt file, which acts similar to a mini-database.现在我想继续读取和写入一个 .txt 文件,它的作用类似于迷你数据库。 For this purpose I simply added the .txt in my project and used the Files.readString(Path) and Files.write(Path path, byte[] bytes) methods.为此,我只是在我的项目中添加了 .txt 并使用了 Files.readString(Path) 和 Files.write(Path path, byte[] bytes) 方法。

When I do this in IntelliJ I have no problems but as soon as I build/export the file with Maven and started with the created launcher the app didn't work because the project structure / file organization isn't the same anymore.当我在 IntelliJ 中执行此操作时,我没有任何问题,但是一旦我使用 Maven 构建/导出文件并使用创建的启动器启动该应用程序就无法工作,因为项目结构/文件组织不再相同。

I also tried to just add the .txt file to the exported folder afterwards but even then I couldn't manage to implement a relative path to the file.之后我也尝试将 .txt 文件添加到导出的文件夹中,但即使如此,我也无法实现文件的相对路径。

I'm still relatively new to programming and it's just a small app so I don't think mySQL would fit my needs.我对编程还是比较陌生,它只是一个小应用程序,所以我认为 mySQL 不适合我的需求。 I've also read that I could store the data in a property file but I don't know if that would be the right way to archive what I want.我还读到我可以将数据存储在属性文件中,但我不知道这是否是我想要的存档的正确方法。 Certainly it must be possible to somehow keep a .txt for reading and writing in your exported project.当然,必须可以以某种方式在导出的项目中保留一个 .txt 用于读取和写入。 Does someone have an idea?有人有想法吗?

If you use a ´*.txt` file for storing, that file cannot be inside the jar because the jar cannot change its contents while running.如果使用 '*.txt` 文件进行存储,则该文件不能jar 中,因为 jar 在运行时无法更改其内容。

You need to put the file somewhere else, either at some dedicated location (let the user choose/configure one), or next to the jar.您需要将文件放在其他地方,或者放在某个专用位置(让用户选择/配置一个位置),或者放在 jar 旁边。 To figure out your own execution path, you can use要找出自己的执行路径,您可以使用

How to get the path of a running JAR file?如何获取正在运行的 JAR 文件的路径?

Maven is one tricky tool. Maven 是一种棘手的工具。 You need to go to the pom file and add the resource.您需要转到pom文件并添加资源。 Unable to add resources to final jar for maven project in Intellij . 无法在 Intellij 中为 Maven 项目的最终 jar 添加资源

-I hope this helps Trader -我希望这对交易者有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM