Load Internal File as String - Android File Input Output

Android examples for File Input Output:Text File

Description

Load Internal File as String

Demo Code


import android.net.Uri;
import java.io.*;

public class Main{
    static String LoadInternalFile(String fileName) {
        try {//from  www  .j av  a  2  s .  c  o  m
            FileInputStream fin = MonkeyGame.activity
                    .openFileInput(fileName);
            StringBuffer fileContent = new StringBuffer("");

            byte[] buffer = new byte[1024];
            int length;

            while ((length = fin.read(buffer)) != -1) {
                fileContent.append(new String(buffer));
            }

            return fileContent.toString();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";
    }
}

Related Tutorials