Android File Read LoadFile(String fileName, Resources resources)

Here you can find the source of LoadFile(String fileName, Resources resources)

Description

Load File

License

Open Source License

Declaration

public static String LoadFile(String fileName, Resources resources)
            throws IOException 

Method Source Code

//package com.java2s;
/**//from ww  w  . ja  v a  2s .  co  m
 * This file is part of C.T.M.Droid.
 *
 * C.T.M.Droid is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * C.T.M.Droid is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with C.T.M.Droid.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.content.res.Resources;

public class Main {
    public static String LoadFile(String fileName, Resources resources)
            throws IOException {
        //Create a InputStream to read the file into  
        InputStream iS;
        //get the file as a stream  
        iS = resources.getAssets().open(fileName);
        //create a buffer that has the same size as the InputStream  
        byte[] buffer = new byte[iS.available()];
        //read the text file as a stream, into the buffer  
        iS.read(buffer);
        //create a output stream to write the buffer into  
        ByteArrayOutputStream oS = new ByteArrayOutputStream();
        //write this buffer to the output stream  
        oS.write(buffer);
        //Close the Input and Output streams  
        oS.close();
        iS.close();
        //return the output stream as a String  
        return oS.toString();
    }
}

Related

  1. readStringFromFile(File file)
  2. readTemplate(String filePath)
  3. readTextFile(File file)
  4. loadFile(String str)
  5. getFileContent(File file)
  6. readFileToByteArray(String pathToFile)