read From Assets and replace all space - Android App

Android examples for App:Assets

Description

read From Assets and replace all space

Demo Code


//package com.java2s;
import android.content.Context;
import android.content.res.AssetManager;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static String readFromAssets(Context context, String fileName) {
        AssetManager assetManager = context.getAssets();
        ByteArrayOutputStream bos = null;
        InputStream inputStream = null;
        String result = null;/* w w w . ja v a 2 s.  c o m*/
        try {
            inputStream = assetManager.open(fileName,
                    AssetManager.ACCESS_STREAMING);
            bos = new ByteArrayOutputStream();
            byte[] data = new byte[1024];
            int length;
            while ((length = inputStream.read(data)) != -1) {
                bos.write(data, 0, length);
            }
            result = bos.toString().replaceAll("\\s*", "");
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            try {
                if (bos != null)
                    bos.close();
                if (inputStream != null)
                    inputStream.close();
            } catch (IOException ignored) {
            }
        }
        return result;
    }
}

Related Tutorials