Java UTF8 File Read getUtf8BytesFromFile(String fileName)

Here you can find the source of getUtf8BytesFromFile(String fileName)

Description

get Utf Bytes From File

License

Apache License

Declaration

public static byte[] getUtf8BytesFromFile(String fileName) throws IOException 

Method Source Code

//package com.java2s;
/**/*w  w  w  . j  a v a  2s.c  o  m*/
 * Copyright 2008 - 2009 Pro-Netics S.P.A.
 *
 *    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.
 */

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

public class Main {
    public static byte[] getUtf8BytesFromFile(String fileName) throws IOException {
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
        Reader reader = new InputStreamReader(inputStream);
        StringBuffer stringBuffer = new StringBuffer();
        char[] buffer = new char[4096];
        int len;
        while ((len = reader.read(buffer)) > 0) {
            stringBuffer.append(buffer, 0, len);
        }
        return stringBuffer.toString().getBytes("UTF-8");
    }
}

Related

  1. getBufferedUTF8Reader(InputStream inputStream)
  2. getUtf8FileWriter(String file, boolean append)
  3. getUTF8InputStreamReader(InputStream stream)
  4. getUTF8Reader(File f)
  5. loadUTF8(File file)