所以我应该在一个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
}
}
我唯一的猜测,一个遥不可及的问题是,它必须与操作系统(Windows 8.1)无法保存UTF-16代码有关,但听起来像是一个愚蠢的猜测。
答案 0 :(得分:1)
读取文件时指定编码:
Scanner fin = new Scanner(new File(FILE), ENCODING);