UTF-16 写的文件打不开,UTF-8 可以

File written in UTF-16 can't be opened, but UTF-8 can

所以我应该在 UTF-16 编码的文件中插入信息,而不是执行一些操作(计算行数、字数等)。问题是,如果我选择 UTF-16 编码,则会抛出异常,但 UTF-8 可以正常工作。

import java.io.*;
import java.util.Scanner;

public final class Q4 {
    public static void main(String[ ] args)throws FileNotFoundException{
        final String ENCODING = "UTF-16";
        final String FILE = " testcount";
        PrintWriter out = null;
// Given code – do not modify(!) This will create the UTF-16 test file on your drive.
        try {
            out = new PrintWriter(FILE, ENCODING);
            out.write("Test file for UTF-16\n" + "(contains surrogate pairs:\n" +
                    "Musical symbols in the range 1D100–1D1FF)\n\n");
            out.write("F-clef (1D122): \uD834\uDD22\tCrotchet (1D15F): \uD834\uDD5F\n");
            out.write("G-clef (1D120): \uD834\uDD20\tSemiquaver (1D161): \uD834\uDD61\n");
            out.write("\n(? lines, ?? words, ??? chars but ??? code points)\n");
        } catch (IOException e) { System.out.println("uh? cannot write to file!");
        } finally { if (out != null) out.close(); 
        }
// Your code – scan the test file and count lines, words, characters, and code points.

        Scanner fin = new Scanner(new File(FILE));
        String s = "";

        //get the data in file
        while (fin.hasNext()){
            s = s + fin.next();      
            System.out.println(s);            
        }

        fin.close();

        //count words and lines



    }
}

我唯一的猜测,一个牵强附会的猜测,是它与 OS (windows 8.1) 无法保存 UTF-16 代码有关,但听起来就像一个愚蠢的猜测。

读取文件时指定编码:

Scanner fin = new Scanner(new File(FILE), ENCODING);