Java Text File Load loadText(String path)

Here you can find the source of loadText(String path)

Description

Reads a whole text file into a String.

License

Apache License

Parameter

Parameter Description
path path to file or resource

Exception

Parameter Description

Return

contents of the file

Declaration

public static String loadText(String path) throws IOException 

Method Source Code


//package com.java2s;
/*/*from  w  w w.ja  v  a2s .  c  o m*/
 * #%L
 * MiscUtil.java - method51 - University of Sussex - 2,013
 * %%
 * Copyright (C) 2013 - 2014 University of Sussex
 * %%
 * Licensed 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.
 * #L%
 */

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

public class Main {
    /**
     * Reads a whole text file into a String.
     *
     * @param path path to file or resource
     * @return contents of the file
     * @throws java.io.IOException
     */
    public static String loadText(String path) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(path));

        String line;

        StringBuilder text = new StringBuilder();

        while ((line = reader.readLine()) != null) {

            text.append(line);
            text.append("\n");
        }

        return text.toString();
    }
}

Related

  1. loadText(File file)
  2. loadText(File file, int bufsize)
  3. loadText(java.io.File f)
  4. loadText(String fname)
  5. loadText(String path)
  6. loadTextFile(File file)
  7. loadTextFile(File textFile)
  8. loadTextFile(final String filePath)
  9. loadTextFile(String fileFullPath)