Verify that the value already exists in the txt file

2

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

Barcode + Description + Location "And after locating I want to add a sum, where every 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 application has read "123" and the product description is "Cream", I want it to write

123 Cream 1

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

123 Cream 2.

    
asked by anonymous 15.11.2015 / 16:48

1 answer

1

Hello! One solution would be to use SQLite , it would make your life a little easier!

SQLite Documentation
TutorialsPoint article about SQLite with Java

Another solution would be to load the contents of the file into memory and use a background worker to save the file to every x seconds for example!

I hope it helps!

Embrace

    
15.11.2015 / 18:24