Read the file under the classpath as a character string with spring

Code that reads src / main / resources / application.yml as a string for the time being.

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.springframework.core.io.ClassPathResource;
import org.springframework.util.FileCopyUtils;

import jp.co.rakuten.firstparty.base.exception.FirstPartyBizException;

public class Hoge {

  public static void main(String[] args) {
    
    String path = "application.yml";
    try {
      String str = new String(FileCopyUtils.copyToByteArray(new ClassPathResource(path).getInputStream()),
          StandardCharsets.UTF_8);
      System.out.println(str);
    } catch (IOException e) {
      throw new FirstPartyBizException(e);
    }
  }

}

Recommended Posts

Read the file under the classpath as a character string with spring
Read a string in a PDF file with Java
Invoke the character string passed as an argument as a method with send
[Spring] Read a message from a YAML file with MessageSource
Read the file under WEB-INF when executing the Servlet
<Java> Quiz to batch convert file names separated by a specific character string with a part of the file name
I want to read the property file with a file name other than application.yml or application- [profile name] .yml with Spring Boot.
How to save a file with the specified extension under the directory specified in Java to the list
Error handling when the maximum file size is exceeded when uploading a file with Spring Boot
[Java] Cut out a part of the character string with Matcher and regular expression
Form and process file and String data at the same time with Spring Boot + Java
Note No. 6 "Calculate the formula for the one-digit sum difference received as a character string" [Java]
How to request a CSV file as JSON with jMeter
Put the file in the properties of string in spring xml configuration
The first WEB application with Spring Boot-Making a Pomodoro timer-
A story packed with the basics of Spring Boot (solved)
Set the date and time from the character string with POI
About the behavior when doing a file map with java
File upload with Spring Boot
Convert Excel to Blob with java, save it, read it from DB and output it as a file!
Code to specify the image name as a character string and paste it into ImageView (android)
[Ruby] Difference between symbol variables and character string variables. About the difference between [: a] and ['a'].
How to read a file and treat it as standard input
Even though the property file path is specified in the -cp option, it is not recognized as a classpath.
Read dump file with Docker MySQL
Implement file download with Spring MVC
Returns the string as md5 hash
I read the source of String
Create a Spring Boot app development project with the cURL + tar command
How to check before sending a message to the server with Spring Integration
[Java] Create a jar file with both compressed and uncompressed with the jar command
How to get the current date as a string in yyyyMMdd format
Decrypt the entire bean in a properties file with some values encrypted
Send a request to the backend after authenticating with Spring Cloud Gateway
[Ruby] I want to make an array from a character string with the split method. And vice versa.