How to read a file in Java with specific character encoding?

后端 未结 3 1244
别跟我提以往
别跟我提以往 2020-12-09 16:14

I am trying to read a file in as either UTF-8 or Windows-1252 depending on the output of this method:

public Charset getCorrectCharsetToApply() {
    // Retu         


        
相关标签:
3条回答
  • 2020-12-09 16:46

    With Java 7+, you can create the Reader in one line:

    BufferedReader buffReader = Files.newBufferedReader(Paths.get(fileName), getCorrectCharsetToApply());

    0 讨论(0)
  • 2020-12-09 16:47

    So, first, as a heads up, do realize that fileName.getBytes() as you have there gets the bytes of the filename, not the file itself.

    Second, reading inside the docs of FileReader:

    The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. To specify these values yourself, construct an InputStreamReader on a FileInputStream.

    So, sounds like FileReader actually isn't the way to go. If we take the advice in the docs, then you should just change your code to have:

    String fileName = getFileNameToReadFromUserInput();
    FileInputStream is = new FileInputStream(fileName);
    InputStreamReader isr = new InputStreamReader(is, getCorrectCharsetToApply());
    BufferedReader buffReader = new BufferedReader(isr);
    

    and not try to make a FileReader at all.

    0 讨论(0)
  • 2020-12-09 17:03

    Note that if you are using Google Guava, you can use Files.newReader:

    final BufferedReader reader =
            Files.newReader(new File(filename), getCorrectCharsetToApply());
    
    0 讨论(0)
提交回复
热议问题