Java Utililty Methods File Content Read

List of utility methods to do File Content Read

Description

The list of methods to do File Content Read are organized into topic(s).

Method

byte[]getFileContent(File file)
Read a file content into a byte array.
InputStream in = null;
byte[] content = new byte[(int) file.length()];
in = new FileInputStream(file);
int i = 0;
int tmp = 0;
while ((tmp = in.read()) != -1) {
    content[i] = (byte) tmp;
    i++;
...
StringgetFileContent(File file)
get File Content
FileReader reader = new FileReader(file);
char[] content = new char[(int) file.length()];
reader.read(content, 0, content.length);
return new String(content);
StringgetFileContent(File file)
Returns the content of file as a string.
FileReader fileReader = new FileReader(file);
char[] ioBuffer = new char[1024];
StringBuffer content = new StringBuffer();
int number = -1;
while ((number = fileReader.read(ioBuffer)) != -1) {
    content.append(ioBuffer, 0, number);
return content.toString();
...
StringgetFileContent(File file)
get File Content
Writer swriter = null;
Reader reader = null;
try {
    reader = new FileReader(file);
    swriter = new StringWriter();
    int len = 0;
    char[] buffer = new char[1024];
    while ((len = reader.read(buffer)) > 0) {
...
StringgetFileContent(File file)
Method to retrive the content of a file in a String
BufferedReader br = new BufferedReader(new FileReader(file));
String content = "";
String strLine;
while ((strLine = br.readLine()) != null) {
    content += strLine + "\n";
return content;
StringgetFileContent(File file)
Reads a file content and return it as String.
Scanner scanner = null;
String content = null;
Exception reThrow = null;
if (file != null && file.length() > 0) {
    try {
        scanner = new Scanner(file);
        content = scanner.useDelimiter("\\A").next();
    } catch (Exception e) {
...
StringgetFileContent(String filename)
get File Content
File file = new File(filename);
if (!file.exists()) {
    return null;
StringBuffer result = new StringBuffer();
FileInputStream fis = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(fis, "UTF-8");
LineNumberReader lineReader = new LineNumberReader(reader);
...
StringgetFileContent(String filename)
Helper function for reading file
String content = null;
File file = new File(filename);
if (!file.exists()) {
    return null;
try {
    FileReader reader = new FileReader(file);
    char[] chars = new char[(int) file.length()];
...
StringgetFileContent(String fileName)
get file content as string
File file = new File(fileName);
if (!file.exists() || !file.isFile() || !file.canRead()) {
    return null;
StringBuffer fileData = new StringBuffer();
BufferedReader reader = new BufferedReader(new FileReader(file));
char[] buf = new char[1024];
int numRead = 0;
...
StringgetFileContent(String filename)
get File Content
File file = new File(filename);
if (!file.exists()) {
    return null;
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buff = new byte[1024];
int len = 0;
...