我的保存文件的代码如下:
void jButton1_actionPerformed(ActionEvent e) {
String currFileName = null;
if( currFileName == null ) {
Date iNow = new Date();
SimpleDateFormat iFormat = new SimpleDateFormat( "yyyy-MM-dd HH.mm.ss");
currFileName = iFormat.format( iNow );
} try {
// Open a file of the current name.
File file = new File( currFileName +".txt" );
FileWriter out = new FileWriter( file );
String text = jTextArea1.getText();
out.write( text );
out.close(); System.out.println( currFileName );
JOptionPane.showMessageDialog(null, "保存成功!", "恭喜成功!",JOptionPane.INFORMATION_MESSAGE);
}
catch (IOException el) {
el.printStackTrace();
}
}
void jButton1_actionPerformed(ActionEvent e) {
String currFileName = null;
if( currFileName == null ) {
Date iNow = new Date();
SimpleDateFormat iFormat = new SimpleDateFormat( "yyyy-MM-dd HH.mm.ss");
currFileName = iFormat.format( iNow );
} try {
// Open a file of the current name.
File file = new File( currFileName +".txt" );
FileWriter out = new FileWriter( file );
String text = jTextArea1.getText();
out.write( text );
out.close(); System.out.println( currFileName );
JOptionPane.showMessageDialog(null, "保存成功!", "恭喜成功!",JOptionPane.INFORMATION_MESSAGE);
}
catch (IOException el) {
el.printStackTrace();
}
}
是正确的:
现在我想:我想在我的程序中,
自动删除,三天前的文件
而我的文件名都是以当天的时间为名的.txt(时间精确到秒)
文件名例如:2001-09-19 14.39.39
大家帮我想想,我该怎么来写删除代码:(最好给我代码,我会加分给你)
try
{
File f = new File(fileName);
f.delete();
}
catch(IOException e)
{
}
我不知道如何得到文件的创建时间,有知道的请告诉我。
请大家都来帮帮我呀!
我很急,
help me!
help me!
help me!
help me!
help me!
{
File f = new File(fileName);
if(f.lastModified()==........)
f.delete();
}
catch(IOException e)
{
}
加上循环用public File[] listFiles(FileFilter filter)列出所有文件
// Open a file of the current name.
File file = new File( currFileName +".txt" ); /*
具体的函数和类名我忘记了,总之首先用类File的一个成员函数好像叫lastModified()得到文件的最后修改时间。然后和当前时间比较,符合条件就删除。
*/
FileWriter out = new FileWriter( file );
String text = jTextArea1.getText();
out.write( text );
out.close(); System.out.println( currFileName );
JOptionPane.showMessageDialog(null, "保存成功!", "恭喜成功!",JOptionPane.INFORMATION_MESSAGE);
}
catch (IOException el) {
el.printStackTrace();
}
详细的应该是多项删除呀,
try
{
File f = new File(fileName);
if(f.lastModified()==........)//这个条件该怎么添呢?
f.delete();
}
catch(IOException e)
{
}
将字符串2001-09-19 14.39.39转换成日期
然后比较
扫描所有文件名,把名字转换成一个long值,和当前时间的long项减,如果大于三天的时间的long值就把它删除。
1.去看一下SimpleDateFormat,其中有一方法SimpleDateFormat.parse(String)返回的是一Date类,然后就可以比较日期了.
2.File类中方法File[] listFiles()...可返回符合条件的多个文件File类,这样一来多个文件也解决了.
叫我,把这些文件名,存到另外的一个文件中,
然后要删除就从另外文件中提取文件名,skyyoung(路人甲) 方法是好,
怎么样才能实现呢?请赐教