用IDEA运行JAVA程序时读取本地文件时的意外

来源:互联网 发布:常德网络推广公司 编辑:程序博客网 时间:2024/06/05 10:19

好久没写东西了,最近太忙了。今天做文件的操作发现点有趣的现象。放上来跟大家分享一下。

今天用IDEA做程序的本地文件的部分时,碰到一点小问题。程序如下:

1 package example;
2
3 import java.io.BufferedReader;
4 import java.io.File; JAVA天堂
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 = ""; JAVA天堂
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); JAVA天堂
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 }


程序运行,IDEA输出空白,整个IDEA近乎进入假死状态。哪儿出问题了呢?经过排查否定了String和StringBuffer的问题,后来查看fo1.txt的文件格式居然是utf-8,难道跟这个有关系?经过测试,当fo1.txt只有10几K的情况下,没有问题,输出乱码,而当 fo1.txt达到几十K的时候,IDEA就没法正常输出了,当fo1.txt文件格式改为gbk的话,文件1M照样正常运行。后又拿Eclipse测试,fo1.txt在utf-8下1M大小正常输出乱码(不过感觉好慢,除了IDEA那个假死状态,程序在正常运行情况下,感觉Eclipse比IDEA 慢上不少,有点难以忍受了)。

嗯。写这个出来,只是想大家探讨探讨这到底是什么问题呢?为啥IDEA会出现这种状况呢?马上去NetBeans里测试测试。

来自:http://www.javah.net/JAVAjichu/20070522/1765.html

原创粉丝点击