Java BufferedReader Read readFile(File file, boolean useSystemLineSeparator)

Here you can find the source of readFile(File file, boolean useSystemLineSeparator)

Description

read File

License

Open Source License

Declaration

public static String readFile(File file, boolean useSystemLineSeparator) 

Method Source Code

//package com.java2s;
/**/*  w  w  w  .  j  a va 2 s.  c o m*/
 * Copyright (c) 2007 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: 
 *   IBM - Initial API and implementation
 */

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

public class Main {
    public static String readFile(File file, boolean useSystemLineSeparator) {
        StringBuffer stringBuffer = new StringBuffer();
        try {
            BufferedReader in = new BufferedReader(new FileReader(file));
            try {
                int size = 0;
                char[] buff = new char[512];
                while ((size = in.read(buff)) >= 0) {
                    stringBuffer.append(buff, 0, size);
                }
            } finally {
                in.close();
            }
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }

        int length = stringBuffer.length();
        if (length > 0) {
            String nl = useSystemLineSeparator ? System.getProperties().getProperty("line.separator") : "\n";
            return stringBuffer.toString().replaceAll("\\r\\n", "\n").replaceAll("[\\n|\\r]", nl);
        }
        return stringBuffer.toString();
    }
}

Related

  1. readFile(File file)
  2. readFile(File file)
  3. readFile(File file)
  4. readFile(File file)
  5. readFile(File file)
  6. readFile(File file, int header)
  7. readFile(File filename)
  8. readFile(File from)
  9. readFile(File in)