如果使用java创建名为当天日期的txt文件

2025-02-24 09:04:45
推荐回答(5个)
回答1:

可以通过“FileOutputStream”创建文件实例,之后过“OutputStreamWriter”流的形式进行存储,举例:
OutputStreamWriter pw = null;//定义一个流
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
String date = df.format(new Date());// new Date()为获取当前系统时间
pw = new OutputStreamWriter(new FileOutputStream(“D:/"+date +".txt”),"GBK");//确认流的输出文件和编码格式,此过程创建了“日期.txt”实例
pw.write("我是要写入到记事本文件的内容");//将要写入文件的内容,不写这句就是创建空的
pw.close();//关闭流
备注:文件流用完之后必须及时通过close方法关闭,否则会一直处于打开状态,直至程序停止,增加系统负担。

回答2:

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class test
{
public static void main(String args[])
{
String dateTime=new SimpleDateFormat("yyyy-MM-dd-HH.mm.ss.SSS").format(new Date());
String name=dateTime+".txt";
String path="F:\\";
String fileStr=path+name;
File file=new File(fileStr);
System.out.println(fileStr);
try
{
file.createNewFile();
}
catch (IOException e)
{}
}
}
刚做好的,可以使用
yyyy-MM-dd-HH.mm.ss.SSS分别为年 月 日 小时 分钟 秒 毫秒
path里可以填入想要创建的路径

回答3:

你好,点项目下点右键选new File--日期.txt文件名,即可创建好了。

回答4:

我专门试了下你这个需求的实现方法,具体参见我写的一段程序。
http://user.qzone.qq.com/108424683/blog/1309692331
写的不怎么样我可以相互学习。

回答5:

SimpleDateFormat sdf=new SimpleDateFormat();
sdf.applyPattern("yyyy-MM-dd");
String fileName = sdf.format( new Date() );
File f = new File("e:/"+fileName+".txt");
f.createNewFile();