Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用新运算符将字节[]转换为字符串(性能)_Java_String_Memory_Bytearray - Fatal编程技术网

Java 使用新运算符将字节[]转换为字符串(性能)

Java 使用新运算符将字节[]转换为字符串(性能),java,string,memory,bytearray,Java,String,Memory,Bytearray,根据下面的问题, http://stackoverflow.com/questions/23753342/java-memory-allocation-for-local-variables 我使用SerialPortEvent从串行端口读取数据 public String logtext = ""; public void serialEvent(SerialPortEvent evt) { if (evt.getEventType() == SerialPortEvent.DATA_AV

根据下面的问题,

http://stackoverflow.com/questions/23753342/java-memory-allocation-for-local-variables

我使用SerialPortEvent从串行端口读取数据

public String logtext = ""; 
public void serialEvent(SerialPortEvent evt) {

if (evt.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
    try {
        StringBuilder sBuilder = new StringBuilder();
        int length = input.available();
        byte[] array = new byte[length];
        int numBytes = input.read(array);
        for (byte b : array) {
          logText = new String(new byte[] {b});                 
          sBuilder.append(logText);
        }
       //Finally i append the StringBuilder to JtextPane
       .......
       ......
        }
每次调用serialEvent时创建新字符串()将每次创建新实例,这将增加内存使用率。。在某些场景中,将每秒调用此serialEvent

在不使用新操作符的情况下,还有其他有效的方法吗


请帮助

您不需要一次执行一个字节的操作。我会一次完成所有的字节

int length = input.available();
byte[] array = new byte[length];
String logText = new String(array, 0); // assume ISO-8859-1 encoding

也许可以阅读字符串文档?是的,我会这样做,但在我的字节数组中,我有新行和bell ASCII的ASCII值。为了删除它,如果字节数组内容等于ASCII,我会这样做,如果在JtextPane中不显示。是否有任何方法可以通过使用您提到的代码从字符串中删除ascii。