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

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

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.

@Service
public class NewsSourceServiceImpl implements NewsSourceService {

    private final NewsSourceRepository newsSourceRepository;
    private final ThreadPoolTaskScheduler threadPoolTaskScheduler;
    private final CronTrigger cronTrigger;

    public NewsSourceServiceImpl(NewsSourceRepository newsSourceRepository, ThreadPoolTaskScheduler threadPoolTaskScheduler, CronTrigger cronTrigger) {
        this.newsSourceRepository = newsSourceRepository;
        this.threadPoolTaskScheduler = threadPoolTaskScheduler;
        this.cronTrigger = cronTrigger;
    }

    @PostConstruct
    private void statisticalTask() {
        threadPoolTaskScheduler.schedule(new StatisticalTask(), cronTrigger);
    }

    private class StatisticalTask implements Runnable {

        @Override
        public void run() {
            List<Statistics> statisticalTasks = newsSourceRepository.countNewsForStatistics();
            // Here I have to create a txt file and 'statisticalTasks' contents. Then store the files.
        }
    }

}

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

        String directoryPath = "stats";
        String fileName = "file-name.txt";

        File directory = new File(directoryPath);
        if (!directory.exists()) {
            directory.mkdirs(); // 如果目录不存在,则创建目录
        }

        File file = new File(directory, fileName);

        try (Writer writer = new BufferedWriter(new FileWriter(file))) {
            String newLine = System.getProperty("line.separator");
            String contents = "第一行"
                    + newLine
                    + "下一行";

            writer.write(contents);
        } catch (IOException e) {
            e.printStackTrace();
        }
英文:
  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

        String directoryPath = "stats";
        String fileName = "file-name.txt";

        File directory = new File(directoryPath);
        if (!directory.exists()) {
            directory.mkdirs(); // Create the directory if it doesn't exist
        }

        File file = new File(directory, fileName);

        try (Writer writer = new BufferedWriter(new FileWriter(file))) {
            String newLine = System.getProperty("line.separator");
            String contents = "first line"
                    + newLine
                    + "next line";

            writer.write(contents);
        } catch (IOException e) {
            e.printStackTrace();
        }

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:

确定