我想从一个文本文件中读取其中的内容,代码如下``但是有个缺点,就是当那个文件结束了,还是会追加一个"\n"
于是我就加个注释部分的判断,但郁闷的是,要是文本的最后一行只是空字符(不知道是不是空字符,就是打完一行之后,回车,下一行什么也不写)就不读了
本来这不影响整个文本的读取``,个人觉得总有点不完美,恳请各位大大帮忙```
还有一个问题就是是read()快还是readline()快?
reader = new BufferedReader(new FileReader(new File(1.txt)));
text.setText("");
String tem;
JTextArea text=new JTextArea();
tem = reader.readLine();
while (tem !=null) {
text.append(tem);
text.append("\n");
tem = reader.readLine();
//if((tem = reader.readLine())!=null)text.append("\n"); 1 }
reader.close();
解决方案 »
- 俄罗斯方块的问题
- FileInputStream FileNotFoundException知道问题在哪里,不知道怎么产生的
- java操作mysql,如何执行use databasename的命令
- Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
- 100分求关于Runtime的使用问题
- 关于软件设计过程的问题?
- 怎样精确设置GridLayout布局中每个小格之间的间隙???
- 我想开发一套Client为IE的CS或BS MIS系统,想了解一下实现这个目的除了Java、Delphi的WebForm,还有什么技术能实现?
- 如何调用windows下的exe?
- 菜鸟求助: Java文件读写
- 俄罗斯方块人工智能版+源码大公开
- 专家级问题
text.setText("");
String tem;
JTextArea text=new JTextArea();
tem = reader.readLine();
text.append(tem);
while (tem = reader.readLine())!=null)
text.append("\n"+tem);
reader.close();代码帮你改了,应该可以满足你的要求,个人认为readline()比较快,毕竟它自带了一个缓冲区
而read()是一个一个的读取,
import java.awt.*;
import java.io.*;public class Reader {
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("C:/Text.txt");
try {
FileReader fr = new FileReader(file);
BufferedReader bread = new BufferedReader(fr);
String str = null;
while ((str = bread.readLine())!= null)
{
System.out.print(str);
}
bread.close();
fr.close();
}
catch (IOException w) {
}
}}
reader = new BufferedReader(new FileReader(new File(1.txt)));
String text="";
String tem="";
while(null!=(tem=in.readLine()))
{
text.append(tem);
text.append("\n");
}
is.close();看看是不是你想要的结果!
reader = new BufferedReader(new FileReader(new File(1.txt)));
String text="";
String tem="";
while(null!=(tem=reader.readLine()))
{
text.append(tem);
text.append("\n");
}
is.close(); 看看是不是你想要的结果!
去掉试试看,这是我用的最好用的方法了,别的我也不会了,
猜一下吧
reader = new BufferedReader(new FileReader(new File(1.txt)));
text.setText("");
String tem;
JTextArea text=new JTextArea();
tem = reader.readLine();
while (tem !=null) {
text.append(tem);
tem = reader.readLine();
if(tem!=null)text.append("\n");
}
reader.close();
用read就可以了,read不会比readLine慢。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class BufferedReaderTest { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader reader=null;
try{
reader = new BufferedReader(new FileReader(new File("1.txt")));
StringBuffer tem = new StringBuffer();
String text=new String();
int LEN=1024;
char[] buf = new char[LEN];
int count=0;
while ((count=reader.read(buf,0,LEN))!=-1) {
tem.append(buf,0,count);
}
text=tem.toString();
reader.close();
System.out.print(text);
}catch(Exception ex)
{
}
}}你改一下就可以用在你的程序中了。
要改的就是JTextArea 的值只有在最后设置,刚开始放在StringBuffer中
我觉得read的速度会比readLine更快,因为我一次读取1024个字节,显然比你一行要长,不过没有做测试验证。
LEN的大小可以自己设,太大太小都不好,1024或者2048可能比较好。
用StringBuffer可以稍微提高速度
用你的方法基本行了
不过照你做了,每行还是会读到"\r\n",在textarea里要按两个才能删掉每行的最后一个``
我将所有的"\r\n"替换为"\n"就行了```,可能是我刚才问得不够清楚,我是问,为什么会读到"\r\n"而不是读到
一个换行符"\n"?
稍好一点的文本编辑器都可以把文本文件存为unix格式,这样就只有"\n"了。这种文件你如果用记事本打开会发现没有换行。
这就是所谓的硬回车和软回车。
你自己做编辑器的话,保存的时候注意一下,读取的时候就不需要替换了。
他的意思是:
第一行
第二行(不回车)
--你的程序处理这个没有问题
还有一种
第一行
第二行(回车)
(光标到了这里)
你的程序读取这两种文件会显示相同的结果,就是第一种。
楼主自己写的程序就是只会显示成第二种。
我上面已经说了因为readLine把回车换行丢掉了,所以会出现上面的问题。
只有用read才能满足楼主的要求