英文:
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
-
你可以将文件保存在任何你想要的地方,最好的做法是在项目根目录下创建一个名为
stats
的文件夹。所以绝对路径可能看起来像这样:/home/user/IdeaProjects/project-name/stats/file-name.txt
-
你可以使用
java.io
包中的内置File
和Writer
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();
}
英文:
-
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
-
You can use built in
File
andWriter
fromjava.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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论