I need to write some char element to a .txt before it gets overwritten by me in runtime.
I've tried doing like this:
public void keyTyped(KeyEvent keyEvent) {
char c = keyEvent.getKeyChar();//this gets overwritten in runtime
try {
PrintWriter out = new PrintWriter("test.txt");
out.print(c);
} catch (FileNotFoundException x) {
x.printStackTrace();
}
}
But as you may guess, if i type "java", only 'a'(last char) will be inside of test.txt. Also, when typed "rrr", same letters need to be saved too. Sorry for bad English. Any help is appreciated.
From OP's edits in the question:
SOLUTION!
String s = "";//initializing a variable
s = s.concat(String.valueOf(c));//two-liner elegant solution!
i had stuck on this for two days, so if you were thinking about this more than me and/or have found more ellegant or effactive way, let me know.
Full code example:
String s = "";
public void keyTyped(KeyEvent keyEvent) {
char c = keyEvent.getKeyChar();
String s = s.concat(String.valueOf(c));
try {
PrintWriter out = new PrintWriter("test.txt");
out.print(s);
out.close();
} catch (FileNotFoundException x) {
x.printStackTrace();
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments