Search code examples
androidfilereaderstringbuffer

FileReader to read from bottom to top or stringbuffer?


this file saves (date,time, voice input newline()) im wondering how to process this file into the textview so it reads it from the bottom to the top so i can put the most recent at the top of the textview, thankyou for your time

 wi =(TextView)findViewById(R.id.hes);
        try {
            BufferedReader inputReader = new BufferedReader(new FileReader("/data/data/jip.lam.ru/file"));
            String inputString;
            StringBuffer stringBuffer = new StringBuffer();                
            while ((inputString = inputReader.readLine()) != null) {
                stringBuffer.append(inputString + "\n");
            }
            wi.setText(stringBuffer.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

Solution

  • You can implement a Stack.

     String inputString;
        Stack<String> readbuffer =new Stack<String>();
         while ((inputString = inputReader.readLine()) != null) 
         {
           readbuffer.push(inputString);
         }
    

    Now pop the Stack i.e wi.setText(readbuffer.pop());