Java Text File Read by Charset getReader(String fileName, Charset cs)

Here you can find the source of getReader(String fileName, Charset cs)

Description

get Reader

License

Apache License

Declaration

public static Reader getReader(String fileName, Charset cs) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

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

import java.io.Reader;

import java.net.URL;
import java.nio.charset.Charset;

public class Main {
    public static final Charset defaultCharset = Charset.forName("UTF-8");

    public static Reader getReader(String fileName, Charset cs) {
        Reader reader = null;//from  w ww .  ja  v a  2  s  . c om
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), cs));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return reader;
    }

    public static Reader getReader(String fileName) {
        return getReader(fileName, defaultCharset);
    }

    public static Reader getReader(File file, Charset cs) {
        Reader reader = null;
        try {
            reader = getReader(new FileInputStream(file), cs);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return reader;
    }

    public static Reader getReader(File file) {
        return getReader(file, defaultCharset);
    }

    public static Reader getReader(URL url, Charset cs) {
        Reader reader = null;
        try {
            reader = getReader(url.openStream(), cs);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return reader;
    }

    public static Reader getReader(URL url) {
        return getReader(url, defaultCharset);
    }

    public static Reader getReader(InputStream inStream, Charset cs) {
        Reader reader = null;
        reader = new BufferedReader(new InputStreamReader(inStream, cs));
        return reader;
    }

    public static Reader getReader(InputStream inStream) {
        return getReader(inStream, defaultCharset);
    }
}

Related

  1. getDecoder(Charset charset, ThreadLocal> localDecoder)
  2. getFileContent(IFile file, String charset)
  3. getInputStreamReader(InputStream stream, Charset charset)
  4. getReader(InputStream in, String charsetName)
  5. getReader(InputStream is, String charset)
  6. getReader(String path, Charset encoding)
  7. inferCharset(byte[] bytes, int bytesRead, Charset clientCharset)
  8. initDecoder(Charset charset, ThreadLocal> localDecoder)
  9. inputStreamToString(InputStream input, Charset charset, boolean closeInputAfterRead)