Description
Reads the contents of the file with the given name, returning a String.
License
Open Source License
Parameter
Parameter | Description |
---|
filename | The file to be read |
Exception
Parameter | Description |
---|
IOException | an exception |
Return
The contents of the file
Declaration
static publicString readFile(
String filename) throws java.io.IOException
Method Source Code
//package com.java2s;
/*/*w w w. j a va2 s.c o m*/
* Copyright (C) 2000-2001 Iowa State University
*
* This file is part of mjc, the MultiJava Compiler, adapted for ESC/Java2.
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: JUnitUtils.java 71450 2008-04-10 10:31:38Z dcochran $
* Author: David R. Cok
*/
public class Main {
/** Reads the contents of the file with the given name, returning a String.
This is an optimized version that uses the byte array provided and
presumes that the file is shorter than the length of the array.
@param filename The file to be read
@param cb A temporary byte array to speed reading
@return The contents of the file
@throws java.io.IOException
*/
// FIXME - can we check that the file is too long without losing the efficiency benefits?
static public/*@ non_null */String readFile(
/*@ non_null */String filename, /*@ non_null */byte[] cb)
throws java.io.IOException {
int i = 0;
int j = 0;
java.io.FileInputStream f = null;
try {
f = new java.io.FileInputStream(filename);
while (j != -1) {
i = i + j;
j = f.read(cb, i, cb.length - i);
}
} finally {
if (f != null)
f.close();
}
return new String(cb, 0, i);
}
/** Reads the contents of the file with the given name, returning a String.
This version presumes the file is shorter than an internal buffer.
FIXME
@param filename The file to be read
@return The contents of the file
@throws IOException
*/
static public/*@ non_null */String readFile(
/*@ non_null */String filename) throws java.io.IOException {
StringBuffer sb = new StringBuffer(10000);
char[] cb = new char[10000]; // This hard-coded value can be anything;
// smaller numbers will be less efficient since more reads
// may result
int j = 0;
java.io.FileReader f = null;
try {
f = new java.io.FileReader(filename);
while (j != -1) {
j = f.read(cb, 0, cb.length);
if (j != -1)
sb.append(cb, 0, j);
}
} finally {
if (f != null)
f.close();
}
return sb.toString();
}
}
Related
- loadData(File f)
- loadFolderFromJar(String path)
- loadRes(String res)
- loadStackableItems(String fileName)
- loadTxtFile(String fileName)
- readFile(File f)
- readFile(File f)
- readFile(File f)
- readFile(File f)