Java InputStreamReader Read readFileUtf8(IFile file)

Here you can find the source of readFileUtf8(IFile file)

Description

read File Utf

License

Open Source License

Declaration

public static String readFileUtf8(IFile file) throws CoreException, IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2013 EclipseSource and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  www.  j  a va  2 s .  c  om*/
 *    Ralf Sternberg - initial implementation and API
 ******************************************************************************/

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;

public class Main {
    public static final String UTF_8 = "UTF-8";

    public static String readFileUtf8(IFile file) throws CoreException, IOException {
        if (file.isAccessible()) {
            InputStream inputStream = file.getContents(true);
            try {
                return readStringUtf8(inputStream);
            } finally {
                inputStream.close();
            }
        }
        return null;
    }

    public static String readStringUtf8(InputStream inputStream) throws IOException {
        BufferedReader reader = createReaderUtf8(inputStream);
        char[] buffer = new char[1024];
        StringBuilder builder = new StringBuilder();
        try {
            int read = reader.read(buffer);
            while (read != -1) {
                builder.append(buffer, 0, read);
                read = reader.read(buffer);
            }
        } finally {
            reader.close();
        }
        return builder.toString();
    }

    public static BufferedReader createReaderUtf8(InputStream inputStream) {
        try {
            return new BufferedReader(new InputStreamReader(inputStream, UTF_8));
        } catch (UnsupportedEncodingException exception) {
            throw new RuntimeException(exception);
        }
    }
}

Related

  1. readFiles(String[] files, String encoding)
  2. readFileSB(File f)
  3. readFileToLines(File file)
  4. readFileToList(String fileName, String codeing)
  5. readFileToSet(String filePath, String fileEncoding)
  6. readFileWithThrow(IFile in)