0

I have to take the text file from SD card in a folder and display them in textView. I am using this code

if(file!=null){
            File list[] = file.listFiles();

            Log.d("file size>>",""+file.length());

            if(list!=null){
                for( int i=0; i< list.length; i++)
                {
                    Log.d(">>>>",""+list[i].getName());
                    if(list[i].isFile() && list[i].getName().endsWith(".txt")) {
                        myList.add(list[i]);

                        Log.d("myList size>>",""+myList.size());

                        try {
                            FileInputStream fIn = new FileInputStream(list[i]);

                            BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));

                            while ((aDataRow = myReader.readLine()) != null) {
                                aBuffer += aDataRow + "\n";

                            }
                            abc = aBuffer;

                            Log.d("log valuee>>",abc);

                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

abc is the string variable in which I am saving the value of each file but it prints the repetitive data every time loop runs. Other Logs are printing accurate data

Devraj
  • 1,479
  • 1
  • 22
  • 42

1 Answers1

0

abc and aBuffer are never cleared.

So every time your loop runs, they will just get appended to.

For example, if your first file contains "The quick brown fox", and the second contains " jumps over ", and the third contains "the lazy dog", you'll get

The quick brown fox
The quick brown fox jumps over
The quick brown fox jumps over the lazy dog

Instead of

The quick brown fox
jumps over
the lazy dog

The solution is simply to set abc="" before using it again.
Or maybe aBuffer = "", depending on what you intend to keep there.

As is pointed out by @qbeck in their comment, you may want to use a StringBuilder or StringBuffer instead of String. Since your buffer is going to contain entire files, it may affect the performance.

  • 3
    Or use `StringBuilder` (or `StringBuffer` if you care about concurrency) instead of `String` in `aBuffer += aDataRow` to avoid unnecessary creation of `String` for every line and clear it at the begining of each iteration. – Luke Jul 01 '16 at 08:43
  • Sir, can you plz tell me how can I convert this "abc" string into JSONObject – Devraj Jul 01 '16 at 10:08
  • @Devraj That's another question entirely. It sounds like you need a JSON parser. Maybe the answers to [this SO question](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) will help you. – S.L. Barth is on codidact.com Jul 01 '16 at 10:12
  • can I add this question in this current question too as I am trying something but nothing is happening. :( – Devraj Jul 01 '16 at 10:41
  • @Devraj No, you should not add it to this question. It is a new issue. You should create a [mcve] of the problem you have now, and ask it as a new question. Good luck! – S.L. Barth is on codidact.com Jul 01 '16 at 11:21