当前位置: 技术问答>java相关
我该怎么来保存?大家帮帮忙
来源: 互联网 发布时间:2015-02-05
本文导语: 我想随便输入一段字符,要把它保存为文本文件,而文本文件的名字为当前时间(精确到秒)我用的时下面的:(我的程序没能实现大家帮帮我,只要实现我回给你加分的) void jButton1_actionPerformed(ActionEvent e) { //...
我想随便输入一段字符,要把它保存为文本文件,而文本文件的名字为当前时间(精确到秒)我用的时下面的:(我的程序没能实现大家帮帮我,只要实现我回给你加分的)
void jButton1_actionPerformed(ActionEvent e) {
// boolean saveFile() {
String currFileName = null;
boolean dirty = false;
Timestamp fliename = '2001-12-31 13:15:45';
// Handle the case where we don't have a file name yet.
if (currFileName == null) {
fliename = System.currentTimeMillis();
currFileName = fliename;
}
try
{
// Open a file of the current name.
File file = new File (currFileName);
// Create an output writer that will write to that file.
// FileWriter handles international characters encoding conversions.
FileWriter out = new FileWriter(file);
String text = jTextArea1.getText();
out.write(text);
out.close();
this.dirty = false;
// Display the name of the saved directory+file in the statusBar.
statusBar.setText("Saved to " + currFileName);
return true;
}
catch (IOException e) {
statusBar.setText("Error saving " + currFileName);
}
return false;
// }
void jButton1_actionPerformed(ActionEvent e) {
// boolean saveFile() {
String currFileName = null;
boolean dirty = false;
Timestamp fliename = '2001-12-31 13:15:45';
// Handle the case where we don't have a file name yet.
if (currFileName == null) {
fliename = System.currentTimeMillis();
currFileName = fliename;
}
try
{
// Open a file of the current name.
File file = new File (currFileName);
// Create an output writer that will write to that file.
// FileWriter handles international characters encoding conversions.
FileWriter out = new FileWriter(file);
String text = jTextArea1.getText();
out.write(text);
out.close();
this.dirty = false;
// Display the name of the saved directory+file in the statusBar.
statusBar.setText("Saved to " + currFileName);
return true;
}
catch (IOException e) {
statusBar.setText("Error saving " + currFileName);
}
return false;
// }
|
程序如下,只是将:改为.
import java.io.*;
import java.text.*;
import java.util.*;
/**
* 用当前时间作为文件名
*/
public class TestFileSave {
public static void main(String argv[]) {
new TestFileSave();
}
public TestFileSave() {
String currFileName = null;
// Handle the case where we don't have a file name yet.
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 );
// Create an output writer that will write to that file.
// FileWriter handles international characters encoding conversions.
FileWriter out = new FileWriter( file );
String text = "Hello World";
out.write( text );
out.close();
// Display the name of the saved directory+file in the statusBar.
System.out.println( currFileName );
}
catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.text.*;
import java.util.*;
/**
* 用当前时间作为文件名
*/
public class TestFileSave {
public static void main(String argv[]) {
new TestFileSave();
}
public TestFileSave() {
String currFileName = null;
// Handle the case where we don't have a file name yet.
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 );
// Create an output writer that will write to that file.
// FileWriter handles international characters encoding conversions.
FileWriter out = new FileWriter( file );
String text = "Hello World";
out.write( text );
out.close();
// Display the name of the saved directory+file in the statusBar.
System.out.println( currFileName );
}
catch (IOException e) {
e.printStackTrace();
}
}
}