急!!!java文件操作的问题!!!在线等~~
求教各位大侠:我是java的初学者,现在在使用java进行文件操作时有问题。
问题:我在java程序中select数据库中的表(结果大约几百行),结果在屏幕上输出无误,但写入文件却是空的,求教各位大侠,java中文件写入的具体操作,望能指点一二,谢谢!
附带:部分代码
while(rs.next())
{
id=rs.getInt("ID");
type=rs.getInt("MSGTYPE");
//System.out.println("ID="+id+" "+"MSGTYPE="+type);
result="ID="+id+" "+"MSGTYPE="+type;
try
{
tofile=new FileWriter("result.txt");
out=new BufferedWriter(tofile);
//System.out.println(result);
out.write(result,0,result.length());
out.newLine();
}
catch(IOException e)
{
System.out.println("write to file fail!");
System.out.println("The error is:"+e);
}
}
问题点数:20、回复次数:11Top
1 楼believefym(feng)回复于 2005-11-02 16:08:32 得分 0
应为用了BufferedWriter,所以写操作不会马上执行,会先放入缓存,这样可以提升性能
楼主的问题是写的数据量太小,结束的时候没有对BufferedWriter进行flush,导致缓存的内容没有写入文件
可以添加out.flush(),或者out.close()Top
2 楼zghmminwx()回复于 2005-11-02 16:16:02 得分 0
万分谢谢believefym(暮色,miss,迷失,miss。。。) 的解答,我也曾经加过out.flush(),out.close()加了之后程序运行就十分缓慢,而且最后result.txt中只有一行结果(不是我在屏幕上看到的那么多),不知何故?再次谢谢您!!!Top
3 楼zghmminwx()回复于 2005-11-02 16:17:54 得分 0
补:前面代码的定义部分
int id;
int type;
String result;
FileWriter tofile;
BufferedWriter out;Top
4 楼believefym(feng)回复于 2005-11-02 16:24:15 得分 0
out=new BufferedWriter(tofile);
这个在while循环内部,就是说你有几条记录,就要重新打开这个io,记录越多性能就越差了,而且一旦打开文件就会被覆盖,所以原来的内容会消失
你把out=new BufferedWriter(tofile);放到while前面
在while结束之后对out进行close就可以了Top
5 楼zghmminwx()回复于 2005-11-02 16:30:07 得分 0
好的,我试试先。在此先谢~Top
6 楼zghmminwx()回复于 2005-11-02 17:04:42 得分 0
问题解决了,谢谢老大!!!我是新手,请多关照 :)Top
7 楼zghmminwx()回复于 2005-11-02 17:06:13 得分 0
最终代码:
int id;
int type;
String result;
FileWriter tofile;
BufferedWriter out=null;
try
{
tofile=new FileWriter("result.txt");
out=new BufferedWriter(tofile);
//System.out.println(result);
}
catch(IOException e)
{
System.out.println("write to file fail!");
System.out.println("The error is:"+e);
}
while(rs.next())
{
id=rs.getInt("ID");
type=rs.getInt("MSGTYPE");
//System.out.println("ID="+id+" "+"MSGTYPE="+type);
result="ID="+id+" "+"MSGTYPE="+type;
try
{
out.write(result,0,result.length());
out.newLine();
}
catch(IOException e)
{
System.out.println("out.write to file fail!");
System.out.println("The error is:"+e);
}
}Top
8 楼believefym(feng)回复于 2005-11-02 17:16:18 得分 0
没看到flush或者close,没问题的吗?Top
9 楼zghmminwx()回复于 2005-11-02 17:18:13 得分 0
不好意思,没有贴全
int id;
int type;
String result;
FileWriter tofile;
BufferedWriter out=null;
try
{
tofile=new FileWriter("result.txt");
out=new BufferedWriter(tofile);
//System.out.println(result);
}
catch(IOException e)
{
System.out.println("write to file fail!");
System.out.println("The error is:"+e);
}
while(rs.next())
{
id=rs.getInt("ID");
type=rs.getInt("MSGTYPE");
//System.out.println("ID="+id+" "+"MSGTYPE="+type);
result="ID="+id+" "+"MSGTYPE="+type;
try
{
out.write(result,0,result.length());
out.newLine();
}
catch(IOException e)
{
System.out.println("out.write to file fail!");
System.out.println("The error is:"+e);
}
}
try
{
out.close();
}
catch(IOException e)
{
System.out.println("close file fail!");
System.out.println("The error is:"+e);
}
}Top
10 楼zdsdiablo(十分钟年华老去)回复于 2005-11-02 17:19:41 得分 0
每次while都写文件的确不是好想法~~
Top
11 楼zghmminwx()回复于 2005-11-02 17:25:12 得分 0
呵呵,菜鸟嘛,一开始没有考虑到:)Top




