/*
(Data 流)利用Data 流,完成下面操作:
1) 判断当前目录下是否存在一个“test.dat”的文件,如果该文件不存在,则往该文件
中写入一个long 类型的数值:10000L
2) 如果该文件存在,则从该文件中读出数值,并把该数值加1 之后,再存回文件中。
*/
import java.io.*;
public class D4 {
static void dataJudge(long l) throws IOException {
File f = new File("test.dat");
DataOutputStream dos = null;
DataInputStream dis = null;
long temp;
if (!f.exists()) {
dos = new DataOutputStream(new FileOutputStream(f, true));
dos.writeLong(l);
dos.close();
} else {
dis = new DataInputStream(new FileInputStream(f));
dos = new DataOutputStream(new FileOutputStream(f, true));
temp = dis.readLong() + 1l;
System.out.println(temp);
dos.writeLong(temp);
dis.close();
dos.close();
}
}
public static void main(String args[]) throws IOException {
dataJudge(1000l);
}
}
(Data 流)利用Data 流,完成下面操作:
1) 判断当前目录下是否存在一个“test.dat”的文件,如果该文件不存在,则往该文件
中写入一个long 类型的数值:10000L
2) 如果该文件存在,则从该文件中读出数值,并把该数值加1 之后,再存回文件中。
*/
import java.io.*;
public class D4 {
static void dataJudge(long l) throws IOException {
File f = new File("test.dat");
DataOutputStream dos = null;
DataInputStream dis = null;
long temp;
if (!f.exists()) {
dos = new DataOutputStream(new FileOutputStream(f, true));
dos.writeLong(l);
dos.close();
} else {
dis = new DataInputStream(new FileInputStream(f));
dos = new DataOutputStream(new FileOutputStream(f, true));
temp = dis.readLong() + 1l;
System.out.println(temp);
dos.writeLong(temp);
dis.close();
dos.close();
}
}
public static void main(String args[]) throws IOException {
dataJudge(1000l);
}
}