sinat_36930749 2016-12-11 05:34 采纳率: 80%
浏览 1080
已采纳

JAVA输出到TXT只显示最新一条记录

package newpackage;

import java.awt.event.KeyAdapter;

import java.awt.event.KeyEvent;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class KeyBoardListener extends JFrame {

    JPanel jPanel;
    JLabel UserName,PassWord;
    char charA;  
    public KeyBoardListener()
    {  

        this.setSize(300,200);  

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

        this.setTitle("Keylogger");  

        this.setVisible(true);  

        this.addKeyListener(new MyKeyListener()); 

        jPanel = new JPanel();
        UserName = new JLabel("UserName");
        jPanel.add(UserName);
        PassWord = new JLabel("PassWord");
        jPanel.add(PassWord);


        add(jPanel);

    }  




    public static void main(String[] args)

    {  

        // TODO Auto-generated method stub   
      KeyBoardListener m =  new KeyBoardListener();  

    }  

}  

class MyKeyListener extends KeyAdapter
{

  public void keyPressed(KeyEvent e)
  {  

      char charA = e.getKeyChar();  

        String  s = null;

        try {

            s = "You pressed "+charA+" at "+getTime()+"\n";
        } 
        catch (ParseException ex) 
        {

            Logger.getLogger(MyKeyListener.class.getName()).log(Level.SEVERE, null, ex);

        }

        System.out.println(s); 

        File file = new File("D:\\temp\\test");

        FileOutputStream f = null;

        try
        {

            f = new FileOutputStream(new File("D:\\temp\\test.txt"));

        } 
        catch (FileNotFoundException ex) 

        {

            Logger.getLogger(MyKeyListener.class.getName()).log(Level.SEVERE, null, ex);

        }

        PrintStream p = new PrintStream(f);



    p.println(s);
    p.close();

    }  

private Date getTime() throws ParseException 

{

    Date d = new Date();  

       String s = null;  



    s = DateFormat.getDateInstance().format(d);  

    s = DateFormat.getDateInstance(DateFormat.DEFAULT).format(d);  



    s = DateFormat.getDateInstance(DateFormat.FULL).format(d);  


    s = DateFormat.getDateInstance(DateFormat.MEDIUM).format(d);  


    s = DateFormat.getDateInstance(DateFormat.SHORT).format(d);  


    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
    s = sdf.format(d);  


    DateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
    s = sdf2.format(d);  


    DateFormat sdf3 = new SimpleDateFormat("yyyyMMddHHmmss"); 

    s = sdf3.format(d);  

    s = sdf.format(d);  
    Date today = sdf.parse(s);  

    Calendar c = Calendar.getInstance();  

    return c.getTime();

}


}  


    这是一个记录键盘的小程序
    我想把运行结果输出到一个TXT 文件内,但是运行后测试,TXT 文件内只会记录最新的一条记录,应该怎样更改才能显示所有的记录,求解答

而且在JFrame中JLabel的内容时而显示时而不显示,这是什么原因
  • 写回答

2条回答 默认 最新

  • 毕小宝 博客专家认证 2016-12-11 06:23
    关注

    修正下这行创建f的代码为如下,第二个参数表示追加到原来文件后面:
    try
    {

            f = new FileOutputStream(new File("D:\\temp\\test.txt"),true);
    
        } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算