Java File Content Read getFileContent(String fileName)

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

Description

get File Content

License

Open Source License

Declaration

public static String getFileContent(String fileName) 

Method Source Code


//package com.java2s;
import java.io.BufferedReader;

import java.io.File;

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

public class Main {
    public static String getFileContent(String fileName) {

        BufferedReader reader = null;
        StringBuilder fileContent = new StringBuilder();
        try {//www  .jav a  2s  .c o  m
            File f = new File(fileName);

            reader = new BufferedReader(new FileReader(f));
            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();
    }

    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(String fileName)
  2. getFileContent(String filename)
  3. getFileContent(String fileName)
  4. getFileContent(String fileName)
  5. getFileContent(String fileName)
  6. getFileContent(String filePath)
  7. getFileContent(String filePath)
  8. getFileContent(String filepath)
  9. getFileContent(String fName)