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.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;

import java.net.URL;

public class Main {
    public static String readTextFile(String fileName) throws FileNotFoundException, IOException {
        File file = new File(fileName);
        FileInputStream in = new FileInputStream(file);
        byte buf[] = new byte[(int) file.length()];
        in.read(buf);
        return new String(buf);
    }

    public static String readTextFile(URL url) throws IOException {
        if (url == null)
            return "";
        BufferedInputStream bis = null;
        StringBuffer sb = new StringBuffer();
        try {
            bis = new BufferedInputStream(url.openStream());
            byte buff[] = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length)))
                sb.append(new String(buff, 0, bytesRead));
        } catch (IOException e) {
            throw e;
        }
        return sb.toString();
    }
}