Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.BufferedInputStream;

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

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

public class Main {
    public static byte[] readFile(File f) throws IOException {
        FileInputStream fis = new FileInputStream(f);
        try {
            return readBytes(fis);
        } finally {
            fis.close();
        }
    }

    public static byte[] readBytes(InputStream is) throws IOException {
        try {
            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int ch;
            while ((ch = bis.read()) >= 0) {
                bos.write(ch);
            }
            bos.flush();
            return bos.toByteArray();
        } finally {
            is.close();
        }
    }
}