简体   繁体   English

如何在我的程序中加载文本文件而不将其放在源文件夹之外?

[英]How do I load a text file in my program without it being located outside of the sources folder?

So up until now I can only get access to a text file in my java project by placing it outside of the sources folder and outside of any package.所以到目前为止,我只能通过将它放在源文件夹之外和任何包之外来访问我的 java 项目中的文本文件。 I don't think that's how it is supposed to be and I am wondering how else I can load the text file if I put it in a package?我不认为它应该是这样的,我想知道如果我把它放在一个包中,我还能如何加载文本文件? Here's what I did for loading the text file, which worked but only since the file is located outside of the sources folder这是我为加载文本文件所做的,它有效,但只是因为文件位于源文件夹之外

BufferedWriter bw = new BufferedWriter(new FileWriter("saveScores.txt")); //writing on it with a bufferedwriter
Scanner scan = new Scanner(new FileReader("saveData.txt")); //reading the data with a scanner

I would highly recommend to choose a distribution tool (Gradle, Maven, Ant...).我强烈建议选择一个分发工具(Gradle、Maven、Ant...)。 These Frameworks/Tools usually create destinct directories.这些框架/工具通常会创建明确的目录。 For example a "source" or "src" directory, where your source code is supposed to live.例如“source”“src”目录,您的源代码应该存放在其中。 One of the other directories they will create is something like a "resource" or "res" directory, where static resources (like a ".txt" file) are supposed to live in.他们将创建的其他目录之一类似于“resource”“res”目录,静态资源(如“.txt”文件)应该存在于其中。

Distribution of software is not that trivial.软件的分发并不是那么简单。 You could do it all by your own, but I wouldn't recommed it.你可以自己做这一切,但我不建议这样做。 You have to invest a little work to learn using one of these Frameworks/Tools if you want to distribute your code.如果你想分发你的代码,你必须投入一些工作来学习使用这些框架/工具之一。 Choose wisely from the 3 top dogs in the Java world (Personal opinion: Choose Gradle):从 Java 世界的 3 大狗中明智地选择(个人意见:选择 Gradle):

  1. Gradle摇篮
  2. Maven马文
  3. Ant蚂蚁

If you want to walk down that road to do it all by your self, follow best practises:如果您想自己走这条路,请遵循最佳实践:

  1. Create a new directory where all your resources should live in创建一个新目录,所有资源都应存放在其中
  2. Add this directory to your classpath将此目录添加到您的类路径
  3. Get the resources during runtime via getResourceAsStream .在运行时通过getResourceAsStream获取资源。
    • Java will start looking on the classpath for the files you're asking for by searching through all directories/subdirectories you added on your classpath. Java 将通过搜索您在类路径中添加的所有目录/子目录,开始在类路径中查找您要求的文件。 If Java can't find it, an exception is thrown.如果 Java 找不到它,则会引发异常。
  4. Compile your finsihed Programm with this "resource" directory.用这个“资源”目录编译你完成的程序。 Since all files/directories in a compiled JAR, are automatically on the classpath, your code will still work.由于已编译 JAR 中的所有文件/目录都自动位于类路径中,因此您的代码仍然可以工作。

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

相关问题 如何写入/读取/删除位于资源文件夹中的文本文件的内容? - How do I write/read/delete contents of a text file located in my resources folder? 如何将文本文件加载到此程序中? - How do i load a text file into this program? 如何在程序之外加载类? - How do I load classes outside of the program? 如何处理文本文件中两个单词之间的大间隙,而我的程序没有将它们作为空格读取? - How do I handle large gaps between two words in text file that aren't being read by my program as spaces? 如何在工作区外部的文件夹中的java类中读取属性文件? - How can I read properties file in java class located in a folder outside workspace? 如何迭代位于运行的 .jar 文件之外的文件夹 - How to iterate on a folder located outside a .jar file running 如何创建一个String数组,其元素是位于资源文件夹中的所有png文件? - How do I create a String array with its elements being all the png files located in a resource folder? 如何从可执行的Jar文件中加载xml资源文件并将其保存到jar所在的文件夹中? - How can I load an xml resource file from within an executible Jar file and save it to the folder the jar is located in? 加载位于“src”maven文件夹中的fxml文件 - Load fxml file located in “src” maven folder 如何获得Java程序以读取所需的文本文件? - How do I get my Java program to read the text file I want?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM