日期:2014-05-20  浏览次数:20682 次

请教有关IO流的一个问题
向文件bank.dat 顺序写入整形数组的数据,请将写出需要构造的输出对象outStream
import java.io*;
class BankOutput
{
int[]banks = new int[]{0,5,7,9};
public static void main(String[]args)throw Exception
{
File out = new File("bank.dat");
BankOutput bank = new BankOutput();
FileOutputStream stream = new FileOutputStream(out);
//请在这里构造outStream
for(int i = 0; i < bank.banks.length; i++)
{
outStream.writeInt(bank.banks[i]);

}
outStream.close();
stream.close();
}
}

------解决方案--------------------

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;

public class BankOutput { 
int[]banks = new int[]{0,5,7,9}; 
public static void main(String[]args)throws Exception { //throws
File out = new File("bank.dat"); 
BankOutput bank = new BankOutput (); 
FileOutputStream stream = new FileOutputStream(out); 
//请在这里构造outStream 
DataOutputStream outStream = new DataOutputStream(stream);//构造
for(int i = 0; i < bank.banks.length; i++) 
outStream.writeInt(bank.banks[i]); 
outStream.close(); 
stream.close(); 

 }