速求java笔试题:

来源:百度知道 编辑:UC知道 时间:2024/06/05 17:26:50
将int[] data_arr = new int[]{1,3,5,2,9}数据保存到temp.dat文件中,并从该文件中读取该数据,并在控件台反序输出。如:{9,2,5,3,1}(JavaIO操作),谢谢大家了。

看看这个代码,给你写的

import java.io.*;
public class DataStreamFoo
{
public static void main(String[] args) throws IOException
{
int[] data_arr = new int[]{1,3,5,2,9};
int[] arr=new int[5];
DataOutputStream dout=new DataOutputStream(new FileOutputStream(new File("C:\\temp.dat")));
DataInputStream din=new DataInputStream(new FileInputStream(new File("C:\\temp.dat")));
for (int i:data_arr )
{
dout.writeInt(i);
}
dout.flush();
for (int i=0;i<5 ;i++ )
{
arr[i]=din.readInt();
}
dout.close();
din.close();
for (int j=4;j>=0 ;j-- )
{
System.out.print(arr[j]+" ");
}
}
}