Java File Content Read getFileContents(String fileName)

Here you can find the source of getFileContents(String fileName)

Description

get File Contents

License

Open Source License

Declaration

public static synchronized String getFileContents(String fileName) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static synchronized String getFileContents(String fileName) {
        File file = new File(fileName);
        String results = null;/* w w  w. j  a  v a 2  s .  co m*/

        try {
            int length = (int) file.length(), bytesRead;
            byte byteArray[] = new byte[length];

            ByteArrayOutputStream bytesBuffer = new ByteArrayOutputStream(length);
            FileInputStream inputStream = new FileInputStream(file);
            bytesRead = inputStream.read(byteArray);
            bytesBuffer.write(byteArray, 0, bytesRead);
            inputStream.close();

            results = bytesBuffer.toString();
        } catch (IOException e) {
            System.out.println("Exception in getFileContents(" + fileName + "), msg=" + e);
        }

        return results;
    }
}

Related

  1. getFileContents(File file)
  2. getFileContents(File methodPatch)
  3. getFileContents(File testFile)
  4. getFileContents(final File f)
  5. getFileContents(final File file)
  6. getFileContents(String filename)