Java读取本地文件

先看结果:

这里只是一个测试用例,文档里面的内容也是随便写的

文件的位置写在了yml文件里

后台代码:

java 复制代码
//引入文件路径
@Vaule("${txt.path}")
private String filePath;

@GetMapping("/readTxt")
public AjaxResult readTxt() throws Exception{
	File file = new File(filePath);
	    if (!file.exists()) {
	        throw new RuntimeException("文件不存在");
	    }
	    StringBuilder sb = new StringBuilder();
	    try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8))) {
	        String line;
	        while ((line = br.readLine()) != null) {
	            sb.append(line).append(System.lineSeparator());
	        }
	    }
	    return AjaxResult.success(sb.toString());
}

前端代码:(测试demo,只展示调用,实际上应该在js中写方法,然后进入,最后调用)

结果:

相关推荐
CoderIsArt1 小时前
C#中UI 线程与 Dispatcher
开发语言·ui·c#
微尘寒风1 小时前
【Git】的安装和使用
java·git
y = xⁿ2 小时前
DeepSeek Harness 学习日记:关于Agent接口,工具调用的底层实现
android·java·学习
侧耳倾听1112 小时前
jwt使用简介
java·jwt
顶点多余3 小时前
那些在算法中适合巩固的知识点---1
java·前端·算法
AI人工智能+电脑小能手3 小时前
大白话说Java设计模式-46-责任链模式(业务实战篇)
java·设计模式·责任链模式·风控校验·servlet filter·spring interceptor·链式处理
jay神3 小时前
【计算机毕业设计】基于SpringBoot的程序教学辅助系统
java·前端·vue.js·spring boot·后端·毕业设计·课程设计
AI情绪识别开源3 小时前
检信 ALLEMOTION OS 加密打包可执行程序 — 全面测试报告版本: v1.3功能测试 / 性能测试 /
开发语言·数据结构·人工智能·功能测试
2601_962066494 小时前
【Sql Server】Update中的From语句,以及常见更新操作方式
android·java·数据库