2

Is it possible to open a .txt file or show the content of the .txt file in activity? I really need it for my project. In my project I'm making the expandablelistview which shows the unit at the parentlist and chapter in childlist. Once the child is selected the selected child will open the appropriate .txt file. Is it possible to do so. Please give some suggestions on it.

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128

3 Answers3

0

You can Have TextView to show text in Activity, to set text on TextView use method setText(CharSequence) method.

To Read Text from .txt file, use Following method:

FileInputStream fis=null;
final StringBuffer buffer= new StringBuffer();

try {
    fis = openFileInput("fileName.txt");
    DataInputStream dataIO = new DataInputStream(fis);
    String strLine = null;

    if ((strLine = dataIO.readLine()) != null) {
        buffer.append(strLine);
    }

    dataIO.close();
    fis.close();
}
catch  (Exception e) {  
}

Use

textView.setText(buffer.toString());

to show in Activity.

jeet
  • 29,001
  • 6
  • 52
  • 53
0

Yes.

You just need a File and read it. Just use the Commons IO library from apache.org

//file located directly on SD root.
File myFile = new File(Environment.getExternalStorageDirectory(), "file.txt"); 
String contents = FileUtils.readFileToString(myFile);

After that just use something like myTextView.setText(contents); to set the text to a TextView.

Download Commons IO library here.

stealthjong
  • 10,858
  • 13
  • 45
  • 84
0

i think showing an xml file with a textview in it and setting up the textview to show whatever you want to show will do the trick.

    textview.setText(open file you want to read from here);

to open a file you should use the Buffered reader and IO streams.

Akshay
  • 1
  • 2