Java File Content Read getFileContent(String fileName)

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

Description

get File Content

License

Apache License

Declaration

public static String getFileContent(String fileName) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
    public static String getFileContent(String fileName) {
        try {/*from  w w  w.  j a  va  2s  .  co m*/
            return getFileContent(new FileInputStream(fileName));
        } catch (FileNotFoundException e1) {
            return "";
        }
    }

    public static String getFileContent(InputStream is) {

        BufferedReader reader = null;
        StringBuilder fileContent = new StringBuilder();
        try {
            reader = new BufferedReader(new InputStreamReader(is));
            String line = "";
            while ((line = reader.readLine()) != null) {
                fileContent.append(line);
                fileContent.append("\n");
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                    reader = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return fileContent.toString();

    }
}

Related

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