简体   繁体   English

如何在spring boot中将创建的文件保存到application.properties文件中指定的路径

[英]how to save created file to path specified in application.properties file in spring boot

I have a method which creates new file after every execution I don't want to hardcode file path in code so I added a new property in application.properties file like我有一个方法可以在每次执行后创建新文件我不想在代码中硬编码文件路径所以我在 application.properties 文件中添加了一个新属性,如

jmeter.jmx.path=D:\\\\PerformanceTesting\\\\JMXFiles\\\\ jmeter.jmx.path=D:\\\\PerformanceTesting\\\\JMXFiles\\\\

and instance variable which holds value like和保存值的实例变量,如

  @Value("${jmeter.jmx.path}")
  private String jmxPath;

want to get the value of a variable inside method想要获取方法内部变量的值

public void saveAsJmxFile(HashTree projectTree, String fileName) throws IOException {
    //TODO 
    SaveService.saveTree(projectTree, new FileOutputStream(jmxPath+fileName+".jmx"));
}

its not woking for me, but if i hardcode then it i'll work.它不适合我,但如果我硬编码,那么我会工作。

public void saveAsJmxFile(HashTree projectTree, String fileName) throws IOException {
          //TODO  remove hardcoded jmxPath
           SaveService.saveTree(projectTree, new 
           FileOutputStream("D:\\PerformanceTesting\\JMXFiles\\"+fileName+".jmx"));
    }

just make sure that the directory is exist只要确保目录存在

    Files.createDirectories(Paths.get(jmxPath));

i'm using java8+ nio here我在这里使用 java8+ nio

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

相关问题 Spring Boot 获取 application.properties 的实例(或其文件路径) - Spring Boot get istance of application.properties (or its file path) 春季启动:从“ application.properties”读取文件路径 - Spring boot: Read a file PATH from “application.properties” Spring boot application.properties 文件读取 - Spring boot application.properties file reading Spring 引导无法识别 application.properties 文件 - Spring Boot not recognizing application.properties file Spring Boot 2:如何使用application.properties文件配置HikariCP - Spring Boot 2: How to configure HikariCP using application.properties file 如何在 Spring Boot 中访问 application.properties 文件中定义的值 - How to access a value defined in the application.properties file in Spring Boot Spring 引导应用程序不使用 Application.Properties 文件 - Spring Boot Application not using Application.Properties file Spring Boot应用程序不拉入外部application.properties文件 - Spring boot application not pulling external application.properties file in Spring boot fat jar 无法从 application.properties 中定义的绝对路径读取文件 - Spring boot fat jar cannot read file from absolute path defined in application.properties 如何在 Spring Boot 应用程序的 application.properties 文件中设置 MyBatis 配置属性? - How do I set MyBatis configuration properties in an application.properties file in a Spring Boot application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM