在Spring Boot应用程序中定期创建的txt文件的存储位置和方式是什么?

huangapple go评论103阅读模式
英文:

Where and how to store txt files created periodically in Spring boot application

问题

I have a scheduled task which periodically creates a txt file for statistics. I have to write some content into txt files and store them somewhere. Each time a new txt file should be created with the name "statistics-'new Date()'.txt". I have no idea how to create them and store.

英文:

I have a scheduled task which periodically creates a txt file for statistics. I have to write some content into txt files and store them somewhere. Each time a new txt files should be created with name "statistics-'new Date()'.txt". I have no idea how to create them and store.

  1. @Service
  2. public class NewsSourceServiceImpl implements NewsSourceService {
  3. private final NewsSourceRepository newsSourceRepository;
  4. private final ThreadPoolTaskScheduler threadPoolTaskScheduler;
  5. private final CronTrigger cronTrigger;
  6. public NewsSourceServiceImpl(NewsSourceRepository newsSourceRepository, ThreadPoolTaskScheduler threadPoolTaskScheduler, CronTrigger cronTrigger) {
  7. this.newsSourceRepository = newsSourceRepository;
  8. this.threadPoolTaskScheduler = threadPoolTaskScheduler;
  9. this.cronTrigger = cronTrigger;
  10. }
  11. @PostConstruct
  12. private void statisticalTask() {
  13. threadPoolTaskScheduler.schedule(new StatisticalTask(), cronTrigger);
  14. }
  15. private class StatisticalTask implements Runnable {
  16. @Override
  17. public void run() {
  18. List<Statistics> statisticalTasks = newsSourceRepository.countNewsForStatistics();
  19. // Here I have to create a txt file and 'statisticalTasks' contents. Then store the files.
  20. }
  21. }
  22. }

I have tried to use 'logback-spring.xml' when we try to store log files. But, I think this approach is not appropriate here.

答案1

得分: 1

  1. 你可以将文件保存在任何你想要的地方,最好的做法是在项目根目录下创建一个名为stats的文件夹。所以绝对路径可能看起来像这样:/home/user/IdeaProjects/project-name/stats/file-name.txt

  2. 你可以使用java.io包中的内置FileWriter

  1. String directoryPath = "stats";
  2. String fileName = "file-name.txt";
  3. File directory = new File(directoryPath);
  4. if (!directory.exists()) {
  5. directory.mkdirs(); // 如果目录不存在,则创建目录
  6. }
  7. File file = new File(directory, fileName);
  8. try (Writer writer = new BufferedWriter(new FileWriter(file))) {
  9. String newLine = System.getProperty("line.separator");
  10. String contents = "第一行"
  11. + newLine
  12. + "下一行";
  13. writer.write(contents);
  14. } catch (IOException e) {
  15. e.printStackTrace();
  16. }
英文:
  1. You can save wherever you want to, good practise would be to create a folder with suitable name like stats in project root directory. So the absolute path might look like /home/user/IdeaProjects/project-name/stats/file-name.txt

  2. You can use built in File and Writer from java.io package

  1. String directoryPath = "stats";
  2. String fileName = "file-name.txt";
  3. File directory = new File(directoryPath);
  4. if (!directory.exists()) {
  5. directory.mkdirs(); // Create the directory if it doesn't exist
  6. }
  7. File file = new File(directory, fileName);
  8. try (Writer writer = new BufferedWriter(new FileWriter(file))) {
  9. String newLine = System.getProperty("line.separator");
  10. String contents = "first line"
  11. + newLine
  12. + "next line";
  13. writer.write(contents);
  14. } catch (IOException e) {
  15. e.printStackTrace();
  16. }

huangapple
  • 本文由 发表于 2023年5月14日 15:14:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76246293.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定