Java Text File Read getChars(Reader r)

Here you can find the source of getChars(Reader r)

Description

Because this method takes a Reader it can be locale-correct by creating a FileReader (for example) that uses UTF-8 or some other charset.

License

Open Source License

Parameter

Parameter Description
r the <code>Reader</code> to obtain textual data from

Exception

Parameter Description
IOException an exception

Return

an array of char

Declaration

private static String getChars(Reader r) throws IOException 

Method Source Code

//package com.java2s;
/*//from   w ww  .j a  va 2 s  .c  o  m
 * Copyright (c) 2014 tabletoptool.com team.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     rptools.com team - initial implementation
 *     tabletoptool.com team - further development
 */

import java.io.IOException;

import java.io.Reader;
import java.io.StringWriter;

public class Main {
    /**
     * Because this method takes a <code>Reader</code> it can be locale-correct by
     * creating a FileReader (for example) that uses UTF-8 or some other charset.
     * 
     * @param r the <code>Reader</code> to obtain textual data from
     * @return an array of <code>char</code>
     * @throws IOException
     */
    private static String getChars(Reader r) throws IOException {
        if (r == null) {
            throw new IllegalArgumentException("Reader cannot be null");
        }
        try (StringWriter sw = new StringWriter(10 * 1024)) {
            char[] c = new char[4096];
            while (true) {
                int read = r.read(c);

                if (read == -1) {
                    break;
                }
                sw.write(c, 0, read);
            }
            return sw.toString();
        }
    }
}

Related

  1. fileAsString(String fileName)
  2. fileAsString(String filePath)
  3. fileContentsToString(File errFile)
  4. fileContentsToString(String file)
  5. getChars(File file)
  6. getFileContentAsString(File cpfile)
  7. getFileContentAsString(final String filePath)
  8. getFileContentAsString(InputStream inputStream)
  9. getFileContentAsString(String path)