Java FileReader Create readTextFile(File file)

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

Description

read a text file

License

Open Source License

Declaration

public static String readTextFile(File file) throws IOException 

Method Source Code


//package com.java2s;
/*/*from   www  . ja v  a2  s . com*/
 * This file is part of the Jose Project
 * see http://jose-chess.sourceforge.net/
 * (c) 2002-2006 Peter Sch?fer
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 */

import java.io.*;

public class Main {
    /**
     * read a text file
     */
    public static String readTextFile(File file) throws IOException {
        FileReader in = new FileReader(file);
        StringWriter out = new StringWriter();
        copyReader(in, out);
        in.close();
        out.close();

        return out.toString();
    }

    /**
     * copies the contens of a stream
     */
    public static void copyReader(Reader in, Writer out) throws IOException {
        char[] buffer = new char[4096];
        for (;;) {
            int count = in.read(buffer, 0, 4096);
            if (count < 0)
                break;
            out.write(buffer, 0, count);
        }
    }
}

Related

  1. readTextFile(File f)
  2. readTextFile(File f)
  3. readTextFile(File file)
  4. readTextFile(File file)
  5. readTextFile(File file)
  6. readTextFile(File file)
  7. readTextFile(File file)
  8. readTextFile(File file)
  9. readTextFile(File file)