Java FileReader Create readTextFile(String completePath)

Here you can find the source of readTextFile(String completePath)

Description

Read a textfile into a StringBuffer.

License

Open Source License

Parameter

Parameter Description
completePath complete path of the file

Return

StringBuffer

Declaration

public static StringBuffer readTextFile(String completePath) 

Method Source Code


//package com.java2s;
/*//from   ww w . j  a va 2  s.c  o m
 *  File: MiscUtil.java 
 *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
 *  A commercial license is available, see http://www.jaret.de.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 */

import java.io.File;

import java.io.FileReader;

public class Main {
    /**
     * Read a textfile into a StringBuffer.
     * 
     * @param completePath complete path of the file
     * @return StringBuffer
     */
    public static StringBuffer readTextFile(String completePath) {
        StringBuffer buf = new StringBuffer();
        File file = new File(completePath);
        try {
            FileReader fr = new FileReader(file);
            char buffer[] = new char[1024];
            int read = 1;
            while (read > 0) {
                read = fr.read(buffer);
                if (read > 0) {
                    buf.append(buffer, 0, read);
                }
            }
            fr.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("File could not be read " + completePath + " " + e.getLocalizedMessage());
        }
        return buf;
    }
}

Related

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