Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.app.Activity;

import java.io.*;

public class Main {
    /**
     * Read file from given file path and return data in string
     * @param filePath
     * @return
     */
    public static String readFileToString(String filePath) {
        String result = "";
        try {
            FileInputStream fileInputStream = new Activity().openFileInput(filePath);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
            BufferedReader reader = new BufferedReader(inputStreamReader);

            String data = reader.readLine();
            while (data != null) {
                result += data;
                data = reader.readLine();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result = "";
        }

        return result;
    }
}