Java FileReader Create readTextFile(File file, boolean newline)

Here you can find the source of readTextFile(File file, boolean newline)

Description

read Text File

License

Apache License

Declaration

public static StringBuffer readTextFile(File file, boolean newline) throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.//from   ww w. ja va2 s.  c  om
 *******************************************************************************/

import java.io.*;

public class Main {
    public static StringBuffer readTextFile(File file, boolean newline) throws FileNotFoundException, IOException {
        if (!file.exists()) {
            throw new FileNotFoundException();
        }

        StringBuffer buf = new StringBuffer();
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(file));
            //in = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charsets.toCharset("UTF-8")));
            //in = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            //InputStreamReader read = new InputStreamReader(new FileInputStream(file));
            //System.out.println("File Encoding:["+ read.getEncoding());

            String str;
            while ((str = in.readLine()) != null) {
                buf.append(str);
                if (newline) {
                    buf.append(System.getProperty("line.separator"));
                }
            }
        } catch (IOException e) {
            throw e;
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    throw e;
                }
            }
        }

        return buf;
    }

    public static StringBuffer readTextFile(String fileName, boolean newline)
            throws FileNotFoundException, IOException {
        File file = new File(fileName);
        return readTextFile(file, newline);
    }
}

Related

  1. readTextFile(File file)
  2. readTextFile(File file)
  3. readTextFile(File file)
  4. readTextFile(File file)
  5. readTextFile(File file)
  6. readTextFile(File file, int header)
  7. readTextfile(final String filename)
  8. readTextFile(InputStream in)
  9. readTextFile(InputStream in)