Home » Java » Write Array Values in file text – Android Studio

Write Array Values in file text – Android Studio

Posted by: admin June 30, 2018 Leave a comment

Questions:

i have a question to make. I develop a reading software for my company. The software read’s a value from file txt and put this values in one array, show in Mainscreen the values and the operator write a new read (the new read stay’s available in flowmeter sytems).
my question is, i pretend write the value of new reads in a text file, saved in Root of android.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_leituras);
    //Carregar todas as Variáveis do Layout
    valor_roteiro = findViewById(R.id.valor_roteiro);
    valor_instalacao = findViewById(R.id.valor_instalacao);
    valor_numero_contador = findViewById(R.id.valor_n_cont);
    data_leitura = findViewById(R.id.data_leitura);
    nome_consumidor = findViewById(R.id.nome_consumidor);
    morada_inst = findViewById(R.id.morada_inst);
    tipo_leitura_1 = findViewById(R.id.tipo_leitura_1);
    leitura_anterior_valor = findViewById(R.id.leitura_anterior_valor);
    sucesso = findViewById(R.id.sucesso);
    String text = "";


    try {
        InputStream is = getAssets().open("bd");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        text = new String(buffer);
    } catch (IOException e) {
        e.printStackTrace();
    }

    //String[] fields=lines[0].split("\|");

    //Log.d("consola",lines[0]);
    //


    final String[] lines = text.split("\n");
    final int n = lines[0].split("\|").length;
    final String[][] data = new String[lines.length][n];

    for (int i = 0; i < lines.length; i++) {
        String[] fields = lines[i].split("\|");
        for (int j = 0; j < fields.length; j++) {
            data[i][j] = fields[j];
        }
    }

    //Log.d("consola2","W"+data[0][0]);



    reg_seg = findViewById(R.id.reg_seg);
    reg_ant = findViewById(R.id.reg_ant);
    ok = findViewById(R.id.ok);

    reg_seg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (index == lines.length - 1) {
                return;
            }
            valor_roteiro.setText(data[index][0]);
            valor_instalacao.setText(data[index][1]);
            valor_numero_contador.setText(data[index][2]);
            nome_consumidor.setText(data[index][3]);
            morada_inst.setText(data[index][4]);
            leitura_anterior_valor.setText(data[index][5]);
            tipo_leitura_1.setText(data[index][8]);
            data_leitura.setText(data[index][9]);
            index++;
        }
    });

    reg_ant.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            if (index <= 0) {
                return;
            }
            valor_roteiro.setText(data[index][0]);
            valor_instalacao.setText(data[index][1]);
            valor_numero_contador.setText(data[index][2]);
            nome_consumidor.setText(data[index][3]);
            morada_inst.setText(data[index][4]);
            leitura_anterior_valor.setText(data[index][5]);
            tipo_leitura_1.setText(data[index][8]);
            data_leitura.setText(data[index][9]);
            index--;
        }
    });

    ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                }
            });
        }
    }

When the operator click in “OK” button,the program save values of array in a TXT file, exemp /android/serpagu/exit.txt
Please help me!
Thx a lot

Answers: