Java File Content Read getFileContent(String fileName)

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

Description

get file content as string

License

Open Source License

Parameter

Parameter Description
fileName a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static String getFileContent(String fileName) throws IOException 

Method Source Code


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

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Main {
    /**/*from  w ww.  j  a v a  2 s . c o  m*/
     * get file content as string
     * 
     * @param fileName
     * @throws IOException
     */
    public static String getFileContent(String fileName) throws IOException {
        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;
        while ((numRead = reader.read(buf)) != -1) {
            fileData.append(String.valueOf(buf, 0, numRead));
        }
        reader.close();
        return fileData.toString();
    }
}

Related

  1. getFileContent(File file)
  2. getFileContent(File file)
  3. getFileContent(File file)
  4. getFileContent(String filename)
  5. getFileContent(String filename)
  6. getFileContent(String filename)
  7. getFileContent(String fileName)
  8. getFileContent(String fileName)
  9. getFileContent(String fileName)