好久沒(méi)寫(xiě)東西了,最近太忙了。今天做文件的操作發(fā)現(xiàn)點(diǎn)有趣的現(xiàn)象。放上來(lái)跟大家分享一下。
今天用IDEA做程序的本地文件的部分時(shí),碰到一點(diǎn)小問(wèn)題。程序如下:
1 package example;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileNotFoundException;
6 import java.io.FileReader;
7 import java.io.IOException;
8
9
10
11 public class FileOperation {
12
13 /**
14 * @param args
15 */
16 public static void main(String[] args) {
17 // TODO Auto-generated method stub
18 FileOperation fo = new FileOperation();
19 String s = "";
20 try {
21 s = fo.bufferedReaderDemo("D:/Project/IntelliJ/BrowseVisual/Resource/res/fo1.txt");
22 } catch (IOException e) {
23 // TODO Auto-generated catch block
24 e.printStackTrace();
25 }
26
27 System.out.println(s);
28
29 }
30
31 public String bufferedReaderDemo(String filename) throws IOException {
32 File file = new File(filename);
33 if (!file.exists() || file.isDirectory()) {
34 throw new FileNotFoundException();
35 }
36 BufferedReader br = new BufferedReader(new FileReader(file));
37 String temp;
38 StringBuffer sb = new StringBuffer();
39 temp = br.readLine();
40 while (temp != null) {
41 sb.append(temp).append(" ");
42 temp = br.readLine();
43 }
44 System.out.println(sb.length());
45
46 return sb.toString();
47 }
48
49 }
程序運(yùn)行,IDEA輸出空白,整個(gè)IDEA近乎進(jìn)入假死狀態(tài)。哪兒出問(wèn)題了呢?經(jīng)過(guò)排查否定了String和StringBuffer的問(wèn)題,后來(lái)查看fo1.txt的文件格式居然是utf-8,難道跟這個(gè)有關(guān)系?經(jīng)過(guò)測(cè)試,當(dāng)fo1.txt只有10幾K的情況下,沒(méi)有問(wèn)題,輸出亂碼,而當(dāng)fo1.txt達(dá)到幾十K的時(shí)候,IDEA就沒(méi)法正常輸出了,當(dāng)fo1.txt文件格式改為gbk的話,文件1M照樣正常運(yùn)行。后又拿Eclipse測(cè)試,fo1.txt在utf-8下1M大小正常輸出亂碼(不過(guò)感覺(jué)好慢,除了IDEA那個(gè)假死狀態(tài),程序在正常運(yùn)行情況下,感覺(jué)Eclipse比IDEA慢上不少,有點(diǎn)難以忍受了)。
嗯。寫(xiě)這個(gè)出來(lái),只是想大家探討探討這到底是什么問(wèn)題呢?為啥IDEA會(huì)出現(xiàn)這種狀況呢?馬上去NetBeans里測(cè)試測(cè)試。