///////////////////////////////////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,请大家不吝赐教,此外如果觉得程序的写法,分布...等任何地方有问题的请提出来,谢谢哦
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,请大家不吝赐教,此外如果觉得程序的写法,分布...等任何地方有问题的请提出来,谢谢哦
{
out.close();
}
关了啊
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();
}
就能写入文件了
一开始我就是这样做的,也确实可以写如文件
但经过封装后就不行了,代码如题
如果throws IOException不是好方法,那么怎样写才更合适呢?
st=null;
System.runFinalization();问题的关键还是在于没有关闭文件.由于该实例的finalize方法没执行
(没有调用runFinalization或垃圾回收),正确关闭文件之后就可以正确写入文件了
{
可能出现异常的语句;
}
catch(Exception e)
{System.out.println(e.getStackTrace());}
我是初学者,不对的地方请谅解。