您的位置:首页 > 房产 > 建筑 > 设计logo说明_广告发布服务包括哪些_nba最快的绝杀_广州商务网站建设

设计logo说明_广告发布服务包括哪些_nba最快的绝杀_广州商务网站建设

2025/3/11 4:24:19 来源:https://blog.csdn.net/ting4937/article/details/146063702  浏览:    关键词:设计logo说明_广告发布服务包括哪些_nba最快的绝杀_广州商务网站建设
设计logo说明_广告发布服务包括哪些_nba最快的绝杀_广州商务网站建设

在Spring Boot项目中,经常需要读取resources目录下的文件,不论是配置文件、静态资源还是其他数据文件。本文将介绍9种不同的方式,帮助您轻松读取resources目录下的文件。

方式1: 使用ClassPathResource

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.io.InputStream;public class ResourceExample {public static void main(String[] args) throws IOException {Resource resource = new ClassPathResource("my-file.txt");InputStream inputStream = resource.getInputStream();// 进一步处理 inputStream}
}

方式2: 使用ResourceLoader

import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.Resource;
import java.io.InputStream;
import org.springframework.beans.factory.annotation.Autowired;public class ResourceLoaderExample {@Autowiredprivate ResourceLoader resourceLoader;public void readFile() throws IOException {Resource resource = resourceLoader.getResource("classpath:my-file.txt");InputStream inputStream = resource.getInputStream();// 进一步处理 inputStream}
}

方式3: 使用ClassLoader

import java.io.InputStream;public class ClassLoaderExample {public static void main(String[] args) throws IOException {ClassLoader classLoader = ClassLoaderExample.class.getClassLoader();InputStream inputStream = classLoader.getResourceAsStream("my-file.txt");// 进一步处理 inputStream}
}

方式4: 使用FileCopyUtils

 import org.springframework.util.FileCopyUtils;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;public class FileCopyUtilsExample {public static void main(String[] args) throws IOException {InputStream inputStream = FileCopyUtilsExample.class.getResourceAsStream("/my-file.txt");String content = new String(FileCopyUtils.copyToByteArray(inputStream), StandardCharsets.UTF_8);// 处理 content}
}

方式5: 使用ResourceUtils

import org.springframework.util.ResourceUtils;
import java.io.File;public class ResourceUtilsExample {public static void main(String[] args) throws IOException {File file = ResourceUtils.getFile("classpath:my-file.txt");// 进一步处理 file}
}

方式6: 使用FileInputStream

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;public class FileInputStreamExample {public static void main(String[] args) throws IOException {File file = new File(FileInputStreamExample.class.getClassLoader().getResource("my-file.txt").getFile());try (FileInputStream fis = new FileInputStream(file)) {// 读取文件内容}}
}

方式7: 使用SpringResourceLoader

import org.springframework.context.annotation.Bean;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import java.io.InputStream;public class SpringResourceLoaderExample {@Beanpublic DefaultResourceLoader resourceLoader() {return new DefaultResourceLoader();}public void readFile() throws IOException {Resource resource = resourceLoader().getResource("classpath:my-file.txt");InputStream inputStream = resource.getInputStream();// 进一步处理 inputStream}
}

方式8: 使用ServletContext

import javax.servlet.ServletContext;
import java.io.InputStream;
import org.springframework.beans.factory.annotation.Autowired;public class ServletContextExample {@Autowiredprivate ServletContext servletContext;public void readFile() throws IOException {InputStream inputStream = servletContext.getResourceAsStream("/WEB-INF/classes/my-file.txt");// 进一步处理 inputStream}
}

方式9: 使用PathsFiles(适用于Spring Boot 2.x以上)

import org.springframework.core.io.Resource;
import org.springframework.core.io.DefaultResourceLoader;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Files;
import java.nio.charset.StandardCharsets;public class PathsAndFilesExample {public static void main(String[] args) throws IOException {DefaultResourceLoader resourceLoader = new DefaultResourceLoader();Resource resource = resourceLoader.getResource("classpath:my-file.txt");Path path = resource.getFile().toPath();String content = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);// 处理 content}
}

以上这9种方式可以让您轻松读取resources目录下的文件,选择适合您项目需求的方式,并根据示例代码来实现文件读取功能。希望这些方式能帮助您在Spring Boot项目中处理资源文件。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com