需求:批量创建文件,文件名:日期+序号,文件内容为数字,按照文件序号依次递增。
求解~
2条回答 默认 最新
小飞LOVE霞 2022-02-16 12:31关注import java.util.Date; /** * @author wangfei * @version 1.0 * @date 2022/2/16 */ public class CreateFilesTest { public static void main(String[] args) throws Exception{ String path = "D:\\test"; File file = new File(path); if (!file.exists()) { file.mkdirs(); } Date date = new Date(); SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HHmmss"); String now = s.format(date); for (int i=0;i<10;i++) { String fileName = now + "_" + i; path = path + "\\" + fileName + ".txt"; file = new File(path); file.createNewFile(); OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), "GBK"); writer.write(i + ""); writer.flush(); writer.close(); path = "D:\\test"; } } }
本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 2无用