编写一个Java程序将111,112,113,114,115 这5个数写入到Dest.txt文件中,并以相反的顺序读出显示在屏幕上
1个回答
展开全部
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class DestTxt {
public static void writeTo(String filename) throws IOException {
FileOutputStream fout = new FileOutputStream(filename);
DataOutputStream dout = new DataOutputStream(fout);
for (int i=1; i<= 5; i++)
dout.writeInt(110 + i);
dout.close();
fout.close();
}
public static void readFrom(String filename) throws IOException
{
FileInputStream fin = new FileInputStream(filename);
DataInputStream din = new DataInputStream(fin);
System.out.println(filename + ": ");
List list = new ArrayList();
while (true)
try {
list.add(0, din.readInt());
} catch (EOFException e) {
break;
}
for(Object o : list)
System.out.println(o);
din.close();
fin.close();
}
public static void main(String[] args) throws IOException {
writeTo("dest.txt");
readFrom("dest.txt");
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询