在线揭帖:使用类封装操作后就执行出问题了

h3w4 2007-01-30 10:10:25
///////////////////////////////////sort.java
import java.io.*;
public class sort
{
public static void main(String args[]) throws IOException
{
int myArray[]={99,88,77,66,55,44,33,22,11};
sortTest st = new sortTest ( myArray );
st.init(); //初始化写文件的那些对象
// logic operation
st.out_file ( "Before Sorting:" ); // 没有写入文件
st.out_crt ("before"); // 执行正确
st.sortnub (); // 进行排序
st.out_crt ("after"); // 执行正确
st.out_file ( "After Sorting:" ); // 没有写入文件
}
}
///////////////////////////////sortTest.java
import java.io.*;
class sortTest
{
// data member
private int myArray[];
private FileWriter fw;
private BufferedWriter bf;
private PrintWriter out;
// member function
sortTest ( int arr[] ) throws IOException
{
myArray = arr;
}
protected void finalize() throws IOException
{
out.close();
}
void init() throws IOException
{
fw = new FileWriter("hh");
bf = new BufferedWriter(fw);
out = new PrintWriter(bf);
}
void sortnub()
{
for( int i=0;i<myArray.length;i++){
for( int j=i+1;j<myArray.length;j++){
if ( myArray[i]>myArray[j]){
int temp=myArray[i];
myArray[i]=myArray[j];
myArray[j]=temp;
}
}
}
};
void out_crt ( String s )
{
System.out.println(s);
for (int i=0;i<myArray.length;i++)
System.out.print(myArray[i]+" ");
System.out.println();
};
void out_file ( String text ) throws IOException
{
out.print(text+"\n");
for (int i=0;i<myArray.length;i++)
out.print(myArray[i]);
out.print("\n");
};
};

%java sort
before
99 88 77 66 55 44 33 22 11
after
11 22 33 44 55 66 77 88 99
% ls -l
-rwxr-xr-x 1 root wheel 0 Jan 30 09:53 hh

但是一开始sortTest.init的内容是些在sort.main中的,然后传参进去,执行都好着呢,不知改后为什么会这样
由于第一次写java,请大家不吝赐教,此外如果觉得程序的写法,分布...等任何地方有问题的请提出来,谢谢哦
...全文
225 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
believefym 2007-01-30
  • 打赏
  • 举报
回复
楼主认为会自动调用finalize()?
china8848 2007-01-30
  • 打赏
  • 举报
回复
try
{
可能出现异常的语句;
}
catch(Exception e)
{System.out.println(e.getStackTrace());}
我是初学者,不对的地方请谅解。
「已注销」 2007-01-30
  • 打赏
  • 举报
回复
main()方法后面添加代码
st=null;
System.runFinalization();

问题的关键还是在于没有关闭文件.由于该实例的finalize方法没执行
(没有调用runFinalization或垃圾回收),正确关闭文件之后就可以正确写入文件了
h3w4 2007-01-30
  • 打赏
  • 举报
回复
哦,去掉分号(空语句,但不会报错)
如果throws IOException不是好方法,那么怎样写才更合适呢?
china8848 2007-01-30
  • 打赏
  • 举报
回复
哦,没想明白是为什么,另外方法{}后面的分号是不是应该去掉啊,还有throws IOException不是太好的做法吧。
h3w4 2007-01-30
  • 打赏
  • 举报
回复
to china8848(永远在一起):
一开始我就是这样做的,也确实可以写如文件
但经过封装后就不行了,代码如题
china8848 2007-01-30
  • 打赏
  • 举报
回复
import java.io.*;
public class sort
{

private static FileWriter fw;
private static BufferedWriter bf;
private static PrintWriter out;
public static void main(String args[]) throws IOException
{
int myArray[]={99,88,77,66,55,44,33,22,11};
fw = new FileWriter("hh.txt");
bf = new BufferedWriter(fw);
out = new PrintWriter(bf);
for (int i=0;i<myArray.length;i++)
out.print(myArray[i]);
out.close();

}
就能写入文件了
believefym 2007-01-30
  • 打赏
  • 举报
回复
楼主什么问题?
还有,}后面加那么多分号干吗
china8848 2007-01-30
  • 打赏
  • 举报
回复
protected void finalize() throws IOException
{
out.close();
}
关了啊
journay 2007-01-30
  • 打赏
  • 举报
回复
别忘了关闭文件操作

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧