SpringBoot读取Linux服务器某路径下文件\读取项目Resource下文件
<div class="cnblogs_code"><pre>// SpringBoot读取Linux服务器某路径下文件<br> <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String messageToRouted() {
File file </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
file </span>= ResourceUtils.getFile("/home/admin/logs/test/routed.txt"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取文件输入流</span>
InputStream inputStream = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> FileInputStream(file);
List</span><String> fileList =<span style="color: rgba(0, 0, 0, 1)"> IOUtils.readLines(inputStream);
log.info(</span>"fileList:{}"<span style="color: rgba(0, 0, 0, 1)">, fileList);
fileList.forEach(rd </span>-><span style="color: rgba(0, 0, 0, 1)"> {
RoutedPO routedPO </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RoutedPO();
routedPO.setFulfillmentOrderName(rd.trim());
routedPO.setRoutedTo(</span>"test"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 通知分单结果</span>
<span style="color: rgba(0, 0, 0, 1)"> routedService.notifyRoutedResult(routedPO, OfcRoutedOperate.INSERT);
});
} </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (FileNotFoundException e) {
log.info(</span>"文件不存在!"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">return</span> "文件不存在!"<span style="color: rgba(0, 0, 0, 1)">;
} </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) {
log.info(</span>"文件读取异常!"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">return</span> "文件读取异常!"<span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span> "success!"<span style="color: rgba(0, 0, 0, 1)">;
}</span></pre>
</div>
<div class="cnblogs_code">
<pre>// SpringBoot读取Resource下文件<br> <span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String messageToRouted(){
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
ClassPathResource classPathResource </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> ClassPathResource("routed.txt"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 判断文件是否存在</span>
<span style="color: rgba(0, 0, 255, 1)">boolean</span> exists =<span style="color: rgba(0, 0, 0, 1)"> classPathResource.exists();
</span><span style="color: rgba(0, 0, 255, 1)">if</span>(!<span style="color: rgba(0, 0, 0, 1)">exists){
log.info(</span>"文件不存在!"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">return</span> "文件不存在!"<span style="color: rgba(0, 0, 0, 1)">;
}
</span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取文件输入流</span>
InputStream inputStream =<span style="color: rgba(0, 0, 0, 1)"> classPathResource.getInputStream();
List</span><String> fileList =<span style="color: rgba(0, 0, 0, 1)"> IOUtils.readLines(inputStream);
log.info(</span>"fileList:{}"<span style="color: rgba(0, 0, 0, 1)">,fileList);
fileList.forEach(rd </span>-><span style="color: rgba(0, 0, 0, 1)">{
RoutedPO routedPO </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> RoutedPO();
routedPO.setFulfillmentOrderName(rd.trim());
routedPO.setRoutedTo(</span>"test"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 通知分单结果</span>
<span style="color: rgba(0, 0, 0, 1)"> routedService.notifyRoutedResult(routedPO, OfcRoutedOperate.INSERT);
});
} </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (IOException e) {
log.info(e.getMessage());
}
} </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (RuntimeException e) {
log.info(e.getMessage());
}
</span><span style="color: rgba(0, 0, 255, 1)">return</span> "success!"<span style="color: rgba(0, 0, 0, 1)">;
}</span></pre>
</div>
<p> </p><br><br>
来源:https://www.cnblogs.com/thiaoqueen/p/11848651.html
頁:
[1]