Here you can find the source of readFile(String filename)
public static String readFile(String filename) throws IOException
//package com.java2s; /*/* www .j a va 2 s . c om*/ * ============================================================================ * GNU General Public License * ============================================================================ * * Copyright (C) 2015 Infinite Automation Software. All rights reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * * When signing a commercial license with Infinite Automation Software, * the following extension to GPL is made. A special exception to the GPL is * included to allow you to distribute a combined work that includes BAcnet4J * without being obliged to provide the source code for any proprietary components. * * See www.infiniteautomation.com for commercial license options. * * @author Matthew Lohbihler */ import java.io.ByteArrayOutputStream; import java.io.CharArrayWriter; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Reader; import java.io.StringWriter; import java.io.Writer; import java.nio.ByteBuffer; import java.nio.channels.SocketChannel; public class Main { public static String readFile(String filename) throws IOException { return readFile(new File(filename)); } public static String readFile(File file) throws IOException { FileReader in = null; try { in = new FileReader(file); StringWriter out = new StringWriter(); transfer(in, out); return out.toString(); } finally { if (in != null) in.close(); } } public static void transfer(InputStream in, OutputStream out) throws IOException { transfer(in, out, -1); } public static void transfer(InputStream in, OutputStream out, long limit) throws IOException { byte[] buf = new byte[1024]; int readcount; long total = 0; while ((readcount = in.read(buf)) != -1) { if (limit != -1) { if (total + readcount > limit) readcount = (int) (limit - total); } if (readcount > 0) out.write(buf, 0, readcount); total += readcount; if (limit != -1 && total >= limit) break; } out.flush(); } public static void transfer(InputStream in, SocketChannel out) throws IOException { byte[] buf = new byte[1024]; ByteBuffer bbuf = ByteBuffer.allocate(1024); int len; while ((len = in.read(buf)) != -1) { bbuf.put(buf, 0, len); bbuf.flip(); while (bbuf.remaining() > 0) out.write(bbuf); bbuf.clear(); } } public static void transfer(Reader reader, Writer writer) throws IOException { transfer(reader, writer, -1); } public static void transfer(Reader reader, Writer writer, long limit) throws IOException { char[] buf = new char[1024]; int readcount; long total = 0; while ((readcount = reader.read(buf)) != -1) { if (limit != -1) { if (total + readcount > limit) readcount = (int) (limit - total); } if (readcount > 0) writer.write(buf, 0, readcount); total += readcount; if (limit != -1 && total >= limit) break; } writer.flush(); } public static byte[] read(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream( in.available()); transfer(in, out); return out.toByteArray(); } public static char[] read(Reader reader) throws IOException { CharArrayWriter writer = new CharArrayWriter(); transfer(reader, writer); return writer.toCharArray(); } }