下面是我该怎么做的!自2000年以来,我几乎完全是一名Java程序员,所以它可能有点老套。有一行特别我有点得意:
new InputStreamReader(fin, "UTF-8");
http://www.joelonsoftware.com/articles/Unicode.html
享受!
import java.io.*;
import java.util.*;
public class StackOverflow2565230 {
public static void main(String[] args) throws Exception {
Map<String, String> m = new LinkedHashMap<String, String>();
FileInputStream fin = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
fin = new FileInputStream(args[0]);
isr = new InputStreamReader(fin, "UTF-8");
br = new BufferedReader(isr);
String line = br.readLine();
while (line != null) {
// Regex to scan for 1 or more whitespace characters
String[] toks = line.split("\\s+");
m.put(toks[0], toks[1]);
line = br.readLine();
}
} finally {
if (br != null) { br.close(); }
if (isr != null) { isr.close(); }
if (fin != null) { fin.close(); }
}
System.out.println(m);
}
}
而这里的输出:
[email protected]:~$ javac StackOverflow2565230.java
[email protected]:~$ java -cp . StackOverflow2565230 file.txt
{grn129=agri-, ac-214=ahss, hud114=ahss, lov1150=ahss, lov1160=ahss, lov1170=ahss, lov1210=ahss}
是的,我的电脑的名字是花。以Bambi的臭鼬命名。
最后一点:因为close()方法可以抛出IOException,这是我会怎样真的关闭流:
} finally {
try {
if (br != null) br.close();
} finally {
try {
if (isr != null) isr.close();
} finally {
if (fin != null) fin.close();
}
}
}
的StringTokenizer正在如果不是很过时,至少根据它的JavaDoc认为是传统类,是不是教科书的解决方案了。 – Thilo 2010-04-02 06:15:13
@Thilo:你应该用什么来代替? – 2010-04-02 06:38:38
根据JavaDoc,String.split。 – Thilo 2010-04-02 07:10:33