Check if the value already exists in the txt file

Asked

Viewed 825 times

2

I am developing an inventory application that in its freeware version will work with the data inserted in a txt file line by line, the data structure will be as follows

Barcode + Description + Location "And after localization I want to put a sum, in which each time the same code is read it will add +1 in the text file"

Writing code;

 String dados;

                        //   lstrNomeArq = (txtScanResult.getText()  + ".txt");
                     File arquivo = new File(Environment.getExternalStorageDirectory(), "/MatheusArrudaDev/InventarioFree/" + timeStamp + "/geral.txt");

                     String unidademetalicascan = (String) txtScanResult.getText();
                     Editable localizacao =  identestoque.getText();
                     Editable descricao =  txtdescricao.getText();
                     dados = unidademetalicascan + localizacao;

                     try {

                     if (!arquivo.exists()) {
                     //cria um arquivo (vazio)
                     arquivo.createNewFile();
                     }

                     //caso seja um diretório, é possível listar seus arquivos e diretórios
                     File[] arquivos = arquivo.listFiles();

                     //escreve no arquivo
                     FileWriter fw = new FileWriter(arquivo, true);

                     BufferedWriter bw = new BufferedWriter(fw);


                     bw.write(unidademetalicascan + " " + descricao +"  "+ localizacao);

                     bw.newLine();

                     bw.close();
                     fw.close();

                     //faz a leitura do arquivo
                     FileReader fr = new FileReader(arquivo);

                     BufferedReader br = new BufferedReader(fr);

                     //equanto houver mais linhas
                     while (br.ready()) {
                     //lê a proxima linha
                     String linha = br.readLine();

                     //faz algo com a linha
                     System.out.println(linha);
                     }

                     br.close();
                     fr.close();

                     } catch (IOException ex) {
                     ex.printStackTrace();

                     }

For example:

The app read "123" and the product description is "Cream", I want it to write

123 Creme 1

If he reads 123 again, I want him to edit the line above and write

123 Creme 2.

1 answer

1

  • I understand I’ll try to work with Sqlite so thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.