Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

import org.apache.http.util.EncodingUtils;

public class Main {
    public static String getStringFromTextFile(File f) {

        byte[] b = getBytesFromFile(f);

        if (b == null)
            return null;

        return EncodingUtils.getString(b, "UTF-8");

    }

    public static byte[] getBytesFromFile(File f) {

        if (f == null) {
            return null;
        }

        FileInputStream stream = null;
        ByteArrayOutputStream out = null;

        try {
            stream = new FileInputStream(f);
            out = new ByteArrayOutputStream(1024);
            byte[] b = new byte[1024];
            int n;
            while ((n = stream.read(b)) != -1)
                out.write(b, 0, n);

        } catch (Exception e) {
        }

        try {
            if (out != null)
                out.close();

            if (stream != null)
                stream.close();
        } catch (Exception e) {
        }

        System.gc();

        if (out == null)
            return null;

        return out.toByteArray();
    }
}