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) throws IOException 

Method Source Code


//package com.java2s;

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class Main {
    public static String getFileContent(String filename) throws IOException {
        File file = new File(filename);
        if (!file.exists()) {
            return null;
        }/*from www.  ja v  a 2  s  . c  o m*/
        StringBuffer result = new StringBuffer();
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader reader = new InputStreamReader(fis, "UTF-8");

        LineNumberReader lineReader = new LineNumberReader(reader);
        String line;

        while ((line = lineReader.readLine()) != null) {
            if (line.trim().equals(""))
                continue;
            result.append(line.trim()).append('\n');
        }
        lineReader.close();
        reader.close();
        fis.close();
        return result.toString();
    }
}

Related

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