当前位置: 代码迷 >> J2SE >> 初学者的字节数组有关问题啊 
  详细解决方案

初学者的字节数组有关问题啊 

热度:164   发布时间:2016-04-24 13:28:04.0
菜鸟的字节数组问题啊 求助
byte[] buf = new byte[1024];

System.out.println("请输入姓名:");
System.in.read(buf);
String name = new String(buf).trim();
//some codes in here

System.out.println("请输入密码:");
System.in.read(buf);
String pass = new String(buf).trim();



后面还有很多地方要用到字节数组的对象来保存输入数据
但是我是用的同一个字节数组的对象来保存输入的 就存在上一次输入的数据和这次的输入混淆的问题啊
请问各位高手有没有这样的方法添加到代码注释的位置 实现保存完一次数据到String类型的变量里面后 然后把字节对象buf里面的东西清空的功能啊?


------解决方案--------------------
byte[] buf = new byte[1024]; 

System.out.println("请输入姓名:"); 
System.in.read(buf); 
String name = new String(buf).trim(); 



Arrays.fill(buf , (byte)0);


System.out.println("请输入密码:"); 
System.in.read(buf); 
String pass = new String(buf).trim();
  相关解决方案