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 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置