我试图将用户输入的字符串附加到使用我发现的api使用全局按键的输入中。但是我无法弄清楚如何在不覆盖已有内容的情况下添加到字符串中。
int i = 1;
public String log = "Write";
public String[] logger = new String[i];
public static PrintStream out = null;
public BufferedWriter writer = null;
public void nativeKeyPressed(NativeKeyEvent e) {
while(i<1000000000){
logger[i] = NativeKeyEvent.getKeyText(e.getKeyCode());
i++;
System.out.println(logger[i]);
}
最佳答案
每当必须将字符或字符串追加到现有变量时,请使用StringBuilder类的append方法。
StringBuilder strBuilder= new StringBuilder();
strBuilder.append("I am a bad programmer");
strBuilder.append("to use concatenation");
strBuilder.append("Instead of StringBuilder");
strBuilder.append(" or StringBuffer");
String str= strBuilder.toString();
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StringBuilder.html