import java.sql.*;
import java.io.*;
import java.util.*;
import java.net.*;
import java.util.Date;
class LogToFile{
String logStr = new String();
PrintWriter log;
String logFile = new String();
String extFile = new String();
String tmpFile = new String();
int countNum=0;
final int WarningNum=20000;
Date now=null;
int a=1;
public LogToFile(String inLogFile){
tmpFile=inLogFile;
now = new Date();
a=now.getDate();
logFile = "./tmplog/"+inLogFile+(1900+now.getYear())+"-"+(1+now.getMonth())+"-"+now.getDate();
extFile = ".txt";
try { 
log = new PrintWriter(new FileWriter(logFile+extFile, true), true); 

catch (IOException e) { 
System.out.println("无法打开日志文件: " + logFile); 

}
public synchronized void DoLog(String inStr){
now = new Date();
log.println((1900+now.getYear())+"-"+(1+now.getMonth())+"-"+now.getDate() + "-"+now.getHours()+"-"+now.getMinutes()+"-"+now.getSeconds()+"<$>" + inStr.replace('\n','N').replace('\r','R') + "<$>end<$>"); 
//countNum++;
if (now.getDate()!=a)
{
try {
logFile = "./tmplog/"+tmpFile+(1900+now.getYear())+"-"+(1+now.getMonth())+"-"+now.getDate();
log = new PrintWriter(new FileWriter(logFile+extFile, true), true);
//log = new PrintWriter(new FileWriter(logFile+"_a"+extFile, true), true);
//countNum=0;
a=now.getDate();

catch (IOException e) { 
System.out.println("无法打开日志文件: " + logFile); 
}
}
}
public synchronized PrintWriter GetLog(){
return log;
}
}//  上面是一个写文件的类,下面是用法
LogToFile myLog=new LogToFile("LogFileName");
myLog.DoLog("需要书写的文本");

解决方案 »

  1.   

    我用的是TextFile 输入的,在我按按钮的时激发事件
    //
    void jButton1_actionPerformed(ActionEvent e) {
       String a1;StringBuffer buf=new StringBuffer();
    try{
    a1=textField1.getText();
    buf.append(a1);
    }catch(IOException ae)//我在运行的时候这句话不对execption java.io.IOException is never thrown in the corresponding try block 
    {
    System.out.println(ae);
    }
    byte b[]=buf.toString() .trim().getBytes();
    FileOutputStream f1=new FileOutputStream("qmjfile1.txt");
    f1.write(b);
    f1.close() ;
    FileInputStream f2=new FileInputStream("qmjfile1.txt");
    FileOutputStream f3=new FileOutputStream("qmjfile2.txt");
    int c2;
    c2=f2.read();
    while (c2!=-1)
    {
    f3.write(c2);
    }
    f2.close() ;
    f3.close() ;
    }