/**
* 编写一个Java程序,要求每次运行该程序时打印出当前是第几次运行,即使计算机重启也能正常计数。
* @param file
* @throws IOException
*/
public static void countRuntime(String bathPath) throws IOException
{
File f = new File(bathPath+File.separator,"3.txt");
if(!f.exists())
{
f.createNewFile();
}
DataInputStream dis = new DataInputStream(new FileInputStream(f));
DataOutputStream dos = new DataOutputStream(new FileOutputStream(f));
int c = 0;
c++;
dos.writeInt(c);
System.out.println(c);
dos.flush();
dos.close();
System.out.println(dis.readInt());
try{int s = 0;
while((s=dis.readInt())!=-1);
{
System.out.println(s);
}
}
catch(EOFException e)
{
System.out.println("完成");
}finally{
dis.close();
}
}
main里代码:
String bathPath = System.getProperty("user.dir");
countRuntime(bathPath);
* 编写一个Java程序,要求每次运行该程序时打印出当前是第几次运行,即使计算机重启也能正常计数。
* @param file
* @throws IOException
*/
public static void countRuntime(String bathPath) throws IOException
{
File f = new File(bathPath+File.separator,"3.txt");
if(!f.exists())
{
f.createNewFile();
}
DataInputStream dis = new DataInputStream(new FileInputStream(f));
DataOutputStream dos = new DataOutputStream(new FileOutputStream(f));
int c = 0;
c++;
dos.writeInt(c);
System.out.println(c);
dos.flush();
dos.close();
System.out.println(dis.readInt());
try{int s = 0;
while((s=dis.readInt())!=-1);
{
System.out.println(s);
}
}
catch(EOFException e)
{
System.out.println("完成");
}finally{
dis.close();
}
}
main里代码:
String bathPath = System.getProperty("user.dir");
countRuntime(bathPath);
import java.io.*;public class Counter {
/**
* 编写一个Java程序,要求每次运行该程序时打印出当前是第几次运行,即使计算机重启也能正常计数。
* @param basePath
* @throws IOException
*/
public static void countRuntime(String basePath) throws IOException
{
int count=0;
File f = new File(basePath+File.separator,"counter.txt");
if(!f.exists())
{
f.createNewFile();
}else{
DataInputStream dis = new DataInputStream(new FileInputStream(f));
count=dis.readInt();
System.out.println("已经执行过"+count+"次");
dis.close();
}
DataOutputStream dos = new DataOutputStream(new FileOutputStream(f)); count+=1;
dos.writeInt(count);
System.out.println(count);
dos.flush();
dos.close();
} public static void main(String[] args)throws IOException{
String bathPath = "e:\\";
countRuntime(bathPath);
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;/**
* 编写一个Java程序,要求每次运行该程序时打印出当前是第几次运行,即使计算机重启也能正常计数。
*
* @param file
* @throws IOException
*/
public class TestCounter {
private static <T> void writeToFile(T t, String fileName) {// 将一个对象 写入文件
if (t == null)
return;// 空对象默认不写进去
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(fileName));
oos.writeObject(t);
oos.flush();
oos.writeObject(null);// //写入结束标志方便读取(如果不写入,在读取的时候无法定位读取结束);
oos.flush();
} catch (Exception e) {
} finally {
try {
if (oos != null)
oos.close();
} catch (Exception e) {
}
}
} private static <T> T readObjectFromFile(String fileName) {// 从文件中读出一个对象
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream(fileName));
T t = (T) ois.readObject();
return t; } catch (Exception e) {
} finally {
try {
if (ois != null)
ois.close();
} catch (Exception e) {
}
}
return null;
}
public static void countRuntime(String bathPath) throws IOException {
File f = new File(bathPath + File.separator, "3.txt");
Integer tempInteger = readObjectFromFile(f.getAbsolutePath());
if (tempInteger == null) {
System.out.println("First run , don't delete the file!");
writeToFile(new Integer(1),f.getAbsolutePath());
}
else {
tempInteger ++;
System.out.println(tempInteger + " visit");
writeToFile(tempInteger,f.getAbsolutePath());
}
}
public static void main(String[] args) throws IOException {
String bathPath = System.getProperty("user.dir");
countRuntime(bathPath);
}
}
/*output:
First run , don't delete the file!
2 visit
3 visit
4 visit
*/