Java InputStreamReader Read readFile(String fileName, String coloredLineIndicator, boolean useSections, boolean isXML)

Here you can find the source of readFile(String fileName, String coloredLineIndicator, boolean useSections, boolean isXML)

Description

read File

License

Apache License

Declaration

public static StringBuffer readFile(String fileName, String coloredLineIndicator, boolean useSections,
            boolean isXML) 

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.InputStreamReader;

public class Main {
    public static StringBuffer readFile(String fileName) {

        FileInputStream fis = null;
        StringBuffer outputBuffer = new StringBuffer();

        try {// w w  w. j a v a  2 s.  c  o  m
            fis = new FileInputStream(fileName);
            InputStreamReader inputStreamReader = new InputStreamReader(fis);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String bufferString = null;

            while ((bufferString = bufferedReader.readLine()) != null) {
                outputBuffer.append(bufferString + '\n');
            }

            fis.close();

        } catch (FileNotFoundException fnfex) {
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return outputBuffer;
    }

    public static StringBuffer readFile(String fileName, String coloredLineIndicator, boolean useSections,
            boolean isXML) {

        FileInputStream fis = null;
        StringBuffer outputBuffer = new StringBuffer();

        try {
            fis = new FileInputStream(fileName);
            InputStreamReader inputStreamReader = new InputStreamReader(fis);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String bufferString = "";

            if (useSections) {
                bufferString = "<font color=\"red\">"
                        + "****************************** File Header ******************************" + "</font>"
                        + '\n';
                outputBuffer.append(bufferString);
            }
            while ((bufferString = bufferedReader.readLine()) != null) {
                if (isXML) {
                    if (bufferString
                            .equals("<!DOCTYPE properties SYSTEM \"http://java.sun.com/dtd/properties.dtd\">")) {
                        continue;
                    }
                }
                if (bufferString.indexOf(coloredLineIndicator) > 0) {
                    outputBuffer.append("<font color=\"red\">");
                    outputBuffer.append(bufferString);
                    outputBuffer.append("</font>" + '\n');
                } else {
                    outputBuffer.append(bufferString + '\n');
                }
            }

            if (useSections) {
                bufferString = "<font color=\"red\">"
                        + "****************************** File Footer ******************************" + "</font>"
                        + '\n';
                outputBuffer.append(bufferString);
            }

            fis.close();

        } catch (FileNotFoundException fnfex) {
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return outputBuffer;
    }
}

Related

  1. readFile(String fileName, Class clazz)
  2. readFile(String fileName, String characterEncoding)
  3. readFile(String fileName, String characterEncoding)
  4. readFile(String fileName, String characterEncoding)
  5. readFile(String fileName, String charSet)
  6. readFile(String filename1)
  7. readFile(String fileNm)
  8. readFile(String filePath)
  9. readFile(String filePath)