在输入板输入
public class KeyBoardListener extends JFrame {
public KeyBoardListener() throws FileNotFoundException {
JFrame jf = new JFrame();
jf.setTitle("监听键盘事件");
jf.setSize(800, 600);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
jf.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
char ch = e.getKeyChar();
try {
if(ch != '#'){
System.out.print(ch);
}else{
System.exit(0);
}
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "出错了");
}
}
});
}
public static void main(String[] args) throws FileNotFoundException {
new KeyBoardListener();
}
}
public static void main(String[] args) throws IOException {
BufferedInputStream in=new BufferedInputStream(System.in);
File file=new File("F:\\tst.txt");
Writer wr=new FileWriter(file);
while(true){
int i=in.read();
if((char)i!='#'){
wr.write(i);
}else break;
}
wr.flush();
wr.close();
in.close();
}
请问字符长度是多大?你要以#结尾,是什么时候结尾?