string inserting issue

asked 2016-08-14 19:05:27 -0800

I try to follow the tutorial on how to save & load data online. The basic part I'm okay. like the first following codes. But when I try to change it so the string can insert all infos from array then it is not working like second codes.

First

   public void dsave(View View){
   String mta1 = String.valueOf(ta1array[600]);

    String file_name = "hello_file";
    try{
    FileOutputStream fileOutputStream = openFileOutput(file_name,MODE_PRIVATE); 
    fileOutputStream.write(mta1.toString().getBytes());
    fileOutputStream.close();
Toast makeText = Toast.makeText(this,"Save", Toast.LENGTH_SHORT);
    makeText.show();
    }
    catch (FileNotFoundException e){
       e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
       e.printStackTrace();
}

  }

public void dload(View View){
try{
    String Mta1;
FileInputStream fileInputStream = openFileInput("hello_file");
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer stringBuffer = new StringBuffer();
while((Mta1 = bufferedReader.readLine()) != null){
    stringBuffer.append(Mta1+"");
   }

ta1array[600]= Byte.parseByte(stringBuffer.toString());

}
catch (FileNotFoundException e){
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}

Second

     public void dsave(View View){
StringBuffer mta1 = null;
for (int index = 0; index < ta1array.length; index++){
    mta1.append(String.valueOf(ta1array[index]));
}

String file_name = "hello_file";
try{
FileOutputStream fileOutputStream = openFileOutput(file_name,MODE_PRIVATE); 
fileOutputStream.write(mta1.toString().getBytes());
fileOutputStream.close();
Toast makeText = Toast.makeText(this,"Save", Toast.LENGTH_SHORT);
makeText.show();
}
catch (FileNotFoundException e){
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}

public void dload(View View){
try{
    String Mta1;
FileInputStream fileInputStream = openFileInput("hello_file");
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer stringBuffer = new StringBuffer();
while((Mta1 = bufferedReader.readLine()) != null){
    stringBuffer.append(Mta1+"");
}
  for (int index = 0; index < ta1array.length; index++){
    ta1array[index]= Byte.parseByte(Mta1, stringBuffer.charAt(index));
  }
}
catch (FileNotFoundException e){
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}
edit retag flag offensive close merge delete