用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 }
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
- 用IDEA运行JAVA程序时读取本地文件时的意外
- IDEA读取本地文件进行处理的方式
- Mac上IntelliJ Idea运行Java程序时出现红色的警告解决办法-亲测有效
- 用Java写的从某个本地文件中读取数据
- java 输入输出 格式化 本地文件的读取
- 关于Java读取本地文件路径的探究
- servlet---java读取本地文件
- Java 流读取本地文件
- java读取本地文件
- Permission denied: win7下面eclipse上传本地文件到DFS && 运行M/R程序时出现的同样的错误解决方法
- Android运行时出现意外stopped问题的解决
- 使用IDEA 2016运行Java程序时提示配置Edit Configurations
- 程序在resin中运行时产生意外多条线程
- Java读取本地文件中字符
- 在IDEA上用Weblogic运行Web程序的小记
- Java读取本地文件的两种方法及其区别
- java程序运行时的内存分配
- Java读取本地文件和读取指定网页
- JDBC4中的新特性
- JS代码混淆初步
- 在ASP.NET 2.0中建立站点导航层次
- ARM开发工具综述转
- google搜索原理论文
- 用IDEA运行JAVA程序时读取本地文件时的意外
- Web开发中的Drag&Drop完全手册
- 用java 获取指定url 网站内容
- 让您的Ajax应用加载数据时界面友好
- MySQL数据库函数详解(5)
- 页面进入和退出的特效
- Softeware Development Model(转)
- 写汇编程序前的准备知识
- Sql联合查询