Java从项目基目录读取一个文件夹。

时间:2023-02-06 08:20:20

I have a maven project with typical project structure. At the base of the project, I have a directory names res that has some resources (> 500 MB). I am trying to use

我有一个具有典型项目结构的maven项目。在项目的基础上,我有一个目录名,它有一些资源(> 500 MB)。我正在尝试使用。

this.class().getClassLoader().getResourceAsStream("res/xxx")

.getResourceAsStream .getClassLoader this.class()()(“res / xxx”)

this code fragment to read from that folder, but it returns a null resource stream. I have a few workarounds below, but none of these are acceptable due to reasons explained below.

此代码片段从该文件夹中读取,但返回一个空资源流。下面我有一些变通方法,但是由于下面解释的原因,这些都不能被接受。

  1. I can move the folder to {base}/target/classes, and it will be read, but it will also get cleaned when I do a mvn clean. Hence, this approach doesn't work. For some reason, specifying the path as ../../res/xxx also doesn't work.
  2. 我可以将文件夹移动到{base}/target/类,它将被读取,但是当我做一个mvn clean时它也会被清理。因此,这种方法不起作用。由于某些原因,指定路径为../../ res / xxx也不工作。
  3. I can move the folder to {base}/src/resources, but then it will get copied to target/classes and the jar. Hence this is also not acceptable.
  4. 我可以将文件夹移动到{base}/src/resources,但随后它将被复制到目标/类和jar中。因此,这也是不可接受的。
  5. Though I am open to trying some other java APIs, I may have to use the class loader mechanism only as there is some external library component that is also trying to access the res folder using the similar approach.
  6. 尽管我愿意尝试其他的java api,但我可能不得不使用类加载器机制,因为有一些外部库组件也在尝试使用类似的方法访问res文件夹。

Is there some way I can read the res folder from projects base directory? Is there some setting in pom.xml file that can help me with that?

有什么方法可以从projects base目录中读取res文件夹吗?pom有一些设置吗?xml文件可以帮助我吗?

2 个解决方案

#1


1  

If you use this.class().getClassLoader().getResourceAsStream("/res/xxx"), it is going to try to load resources from the classpath. If that's not want you want to do, you're going to need to specify an absolute path.

如果您使用this.class(). getclassloader (). getresourstream(“/res/xxx”),它将尝试从类路径加载资源。如果这不是你想要的,你需要指定一个绝对路径。


Resource on Classpath

If you don't want the resource built into your JAR, I'd suggest having a different maven project with the file in it, under the src/main/resources directory. This will create a jar file with the file in it, which will be ~500MB.

如果您不想将资源构建到您的JAR中,我建议在src/main/resources目录下有一个不同的maven项目,其中包含文件。这将创建一个包含文件的jar文件,它将是~500MB。

After this, you can include a dependency on this project in your project containing your application code. This will then be able to reference it from the jar file using getResourceAsStream(...).

在此之后,您可以在包含应用程序代码的项目中包含对该项目的依赖关系。这样就可以使用getresourcestream(…)从jar文件中引用它。

If you don't want this large jar file to ship with your application, make sure you mark the dependency with <scope>provided</scope>.

如果您不希望这个大的jar文件与您的应用程序一起使用,请确保您标记了与 提供 >的依赖关系。


File from Absolute Path

You will need to take the file location as a parameter in your main method, and then use new File("C:\\path\\to\\your\\file.txt") and then use a FileReader to read it in.

您需要将文件位置作为主要方法中的一个参数,然后使用新文件(“C:\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \ / \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\文件。”),然后使用FileReader读取它。

#2


4  

Use this.class().getClassLoader().getResourceAsStream("/res/xxx") and then you will be reading from the root of the classpath irrespective of the actual file/folder location in windows or Linux. This is actually one of the safest ways to read files especially when you do not know how your application will eventually be deployed.

使用this.class(). getclassloader (). getresourcestream(“/res/xxx”),然后您将从类路径的根中读取,而不考虑windows或Linux中的实际文件/文件夹位置。这实际上是最安全的读取文件的方法之一,尤其是当您不知道如何最终部署应用程序时。

It means though that your classpath must include the parent of res and that res/ must be copied over when you deploy your app

这意味着,您的类路径必须包含res的父类,并且在部署应用程序时必须将其复制。

Otherwise, if you want to use relative paths, try this snippet (taken from this SO answer):

否则,如果您想要使用相对路径,请尝试以下代码片段(从这个答案中获取):

String filePath = new File("").getAbsolutePath();
filePath.concat("path to the property file");

#1


1  

If you use this.class().getClassLoader().getResourceAsStream("/res/xxx"), it is going to try to load resources from the classpath. If that's not want you want to do, you're going to need to specify an absolute path.

如果您使用this.class(). getclassloader (). getresourstream(“/res/xxx”),它将尝试从类路径加载资源。如果这不是你想要的,你需要指定一个绝对路径。


Resource on Classpath

If you don't want the resource built into your JAR, I'd suggest having a different maven project with the file in it, under the src/main/resources directory. This will create a jar file with the file in it, which will be ~500MB.

如果您不想将资源构建到您的JAR中,我建议在src/main/resources目录下有一个不同的maven项目,其中包含文件。这将创建一个包含文件的jar文件,它将是~500MB。

After this, you can include a dependency on this project in your project containing your application code. This will then be able to reference it from the jar file using getResourceAsStream(...).

在此之后,您可以在包含应用程序代码的项目中包含对该项目的依赖关系。这样就可以使用getresourcestream(…)从jar文件中引用它。

If you don't want this large jar file to ship with your application, make sure you mark the dependency with <scope>provided</scope>.

如果您不希望这个大的jar文件与您的应用程序一起使用,请确保您标记了与 提供 >的依赖关系。


File from Absolute Path

You will need to take the file location as a parameter in your main method, and then use new File("C:\\path\\to\\your\\file.txt") and then use a FileReader to read it in.

您需要将文件位置作为主要方法中的一个参数,然后使用新文件(“C:\\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \ / \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\ \\文件。”),然后使用FileReader读取它。

#2


4  

Use this.class().getClassLoader().getResourceAsStream("/res/xxx") and then you will be reading from the root of the classpath irrespective of the actual file/folder location in windows or Linux. This is actually one of the safest ways to read files especially when you do not know how your application will eventually be deployed.

使用this.class(). getclassloader (). getresourcestream(“/res/xxx”),然后您将从类路径的根中读取,而不考虑windows或Linux中的实际文件/文件夹位置。这实际上是最安全的读取文件的方法之一,尤其是当您不知道如何最终部署应用程序时。

It means though that your classpath must include the parent of res and that res/ must be copied over when you deploy your app

这意味着,您的类路径必须包含res的父类,并且在部署应用程序时必须将其复制。

Otherwise, if you want to use relative paths, try this snippet (taken from this SO answer):

否则,如果您想要使用相对路径,请尝试以下代码片段(从这个答案中获取):

String filePath = new File("").getAbsolutePath();
filePath.concat("path to the property file");