Java FileReader Create readTextFile(File file)

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

Description

read Text File

License

Apache License

Declaration

public static String readTextFile(File file) throws IOException 

Method Source Code


//package com.java2s;
/*// w  w  w .  j av  a2s  .  com
 * Copyright 2012 Database Lab., Korea Univ.
 *
 * 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.
 */

import java.io.*;

public class Main {
    public static String readTextFile(File file) throws IOException {
        StringBuilder fileData = new StringBuilder(1000);
        BufferedReader reader = new BufferedReader(new FileReader(file));
        char[] buf = new char[1024];
        int numRead = 0;
        while ((numRead = reader.read(buf)) != -1) {
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
            buf = new char[1024];
        }
        reader.close();
        return fileData.toString();
    }
}

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, boolean newline)
  7. readTextFile(File file, int header)
  8. readTextfile(final String filename)
  9. readTextFile(InputStream in)