john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

android read a file

Coded while commuting on the android device and then compiled and run on the same android device

package net.kittyandbear.androidhi;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidhiActivity extends Activity 
{

    @Override  public void onCreate( Bundle savedInstanceState )
    {
        String buffer;
        super.onCreate( savedInstanceState );
        setContentView( R.layout.main );
        TextView tv = new TextView( this );

        String pathfile = "/mnt/sdcard/test.txt";

        if( new File( pathfile ).exists()   )
        {       buffer = getText( pathfile );
        }else
        {       buffer = "file could not be found";
        }

        tv.setText( buffer );
        setContentView( tv );
    }


    private String getText( String pathfile )
    {
        StringBuilder strb = new StringBuilder();
        BufferedReader br;
        try {
            br = new BufferedReader( new FileReader( pathfile ) );
            String line = null;

            while( (line = br.readLine()) != null )
            {
                strb.append( line + System.getProperty( "line.separator") );
            }
         }catch( Exception e )
         {  e.printStackTrace();
         }

        return strb.toString();
        }

} //end class

  • « Xen virtualization ubuntu INCOMPLETE
  • file randomaccessfile lock INCOMPLETE »

Published

Apr 22, 2012

Category

android

~112 words

Tags

  • android 9
  • java 252
  • readfile 3