字符串混合了从内部存储中读取的内容

时间:2018-08-07 15:02:52

标签: android string android-studio buffer read-write

我正在使用内部存储器来存储用户通过倍数编辑文本输入的多个字符串。

因此,布局由对应于字段标题的多个Textview和对应于用户可以在其中输入其字符串的字段的多个Edittext组成。

用户完成操作后,按保存按钮,将触发此功能:

public void  save(View view)  // SAVE
    {
        File file= null;
        String name = editname.getText().toString()+"\n";
        String marque = editmarque.getText().toString()+"\n";
        String longueur = editlongueur.getText().toString()+"\n";
        String largeur = editlargeur.getText().toString()+"\n";
        String tirant = edittirant.getText().toString()+"\n";
        String immatri = editImmatriculation.getText().toString()+"\n";
        String port = editPort.getText().toString()+"\n";
        String contact = editContact.getText().toString()+"\n";
        String panne = editPanne.getText().toString()+"\n";
        String poste = editPoste.getText().toString()+"\n";
        String police = editPolice.getText().toString()+"\n";
        String assurance = editAssurance.getText().toString();

        FileOutputStream fileOutputStream = null;
        try {

            file = getFilesDir();
            fileOutputStream = openFileOutput("Code.txt", Context.MODE_PRIVATE); //MODE PRIVATE
            fileOutputStream.write(name.getBytes());
            fileOutputStream.write(marque.getBytes());
            fileOutputStream.write(longueur.getBytes());
            fileOutputStream.write(largeur.getBytes());
            fileOutputStream.write(tirant.getBytes());
            fileOutputStream.write(immatri.getBytes());
            fileOutputStream.write(port.getBytes());
            fileOutputStream.write(contact.getBytes());
            fileOutputStream.write(panne.getBytes());
            fileOutputStream.write(poste.getBytes());
            fileOutputStream.write(police.getBytes());
            fileOutputStream.write(assurance.getBytes());
            Toast.makeText(this, "Saved \n" + "Path --" + file + "\tCode.txt", Toast.LENGTH_SHORT).show();
            editname.setText("");
            editmarque.setText("");
            editlargeur.setText("");
            editlongueur.setText("");
            edittirant.setText("");
            editImmatriculation.setText("");
            editPort.setText("");
            editContact.setText("");
            editPanne.setText("");
            editPoste.setText("");
            editPolice.setText("");
            editAssurance.setText("");
            return;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

然后,在另一个文件中,我通过触发该功能的另一个按钮检索了该数据:

public void  load(View view)
    {
        try {
            FileInputStream fileInputStream =  openFileInput("Code.txt");
            int read = -1;
            StringBuffer buffer = new StringBuffer();
            while((read =fileInputStream.read())!= -1){
                buffer.append((char)read);
            }
            fileInputStream.close();
            String tab[] = buffer.toString().split("\n");
            String boatname = tab[0];
            String marque = tab[1];
            String longueur = tab[2];
            String largeur = tab[3];
            String tirant = tab[4];
            String immatri = tab[5];
            String port = tab[6];
            String contact = tab[7];
            String panne = tab[8];
            String poste = tab[9];
            String assurance = tab[10];
            String police = tab[11];
            getboatname.setText(boatname);
            getmarque.setText(marque);
            getlongueur.setText(longueur);
            getlargeur.setText(largeur);
            getTirantdeau.setText(tirant);
            getImmatriculation.setText(immatri);
            getPort.setText(port);
            getContact.setText(contact);
            getPanne.setText(panne);
            getPoste.setText(poste);
            getAssurance.setText(assurance);
            getPolice.setText(police);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

因此,在保存功能中,我用\ n拆分输入的字符串,并将文件保存到内部存储中,并在 load函数中检索使用数组对字符串进行分割,然后每个\ n进行分割,然后为文本设置正确的索引。

我不明白结果是混合在一起的,例如,第一个字段的字符串显示在最后一个字段中,为什么?

1 个答案:

答案 0 :(得分:0)

您可以创建一个字符串并编写它。另外,请使用其他分隔符。您可以使用StringBuffer。

StringBuffer s=new StringBuffer();
s.append(editname.getText().toString());
s.append("##@@##@@##");
s.append(editmarque.getText().toString());
s.append("##@@##@@##");
s.append(editlongueur.getText().toString());
s.append("##@@##@@##");
s.append(editlargeur.getText().toString());
s.append("##@@##@@##");
s.append(edittirant.getText().toString());
s.append("##@@##@@##");
s.append(editPort.getText().toString());
s.append("##@@##@@##");
s.append(editContact.getText().toString());
s.append("##@@##@@##");
s.append(editPanne.getText().toString());
s.append("##@@##@@##");
s.append(editPoste.getText().toString());
s.append("##@@##@@##");
s.append(editPolice.getText().toString());
s.append("##@@##@@##");
s.append(editAssurance.getText().toString());
s.append("##@@##@@##");

 FileOutputStream fileOutputStream = null;
        try {

            file = getFilesDir();
            fileOutputStream = openFileOutput("Code.txt", Context.MODE_PRIVATE); //MODE PRIVATE
            fileOutputStream.write(s.toString().getBytes());
            Toast.makeText(this, "Saved \n" + "Path --" + file + "\tCode.txt", Toast.LENGTH_SHORT).show();
            editname.setText("");
            editmarque.setText("");
            editlargeur.setText("");
            editlongueur.setText("");
            edittirant.setText("");
            editImmatriculation.setText("");
            editPort.setText("");
            editContact.setText("");
            editPanne.setText("");
            editPoste.setText("");
            editPolice.setText("");
            editAssurance.setText("");
            return;
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

当您获得FileInputStream时,使用此“ ## @@ ## @@ ##” vlaue拆分字符串