package com.heima.bean;
import java.io.IOException;
public interface MyRead {
public void readTxt() throws IOException;
}
package com.heima.bean;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class MyIO implements MyRead {
private File file;
public MyIO() {
super();
}
public MyIO(File file) {
super();
this.file = file;
}
@Override
public void readTxt() throws IOException {
BufferedReader br= new BufferedReader(new FileReader(file));
int b;
while((b = br.read()) != -1) {
System.out.println((char)b);
}
}
}
package com.heima.bean;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class UseOnlyTen implements MyRead{
private MyIO m ;
public UseOnlyTen() {
super();
}
public UseOnlyTen(MyIO m) {
super();
this.m = m;
}
@Override
public void readTxt() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("config.txt"));
String s = br.readLine();
int count = Integer.parseInt(s);
br.close();
if(count > 0) {
m.readTxt();
count--;
System.out.println("你还有"+count+"次机会");
BufferedWriter bw = new BufferedWriter(new FileWriter("config.txt"));
bw.write(count);
bw.close();
}else {
System.out.println("请购买正版");
}
}
}
package com.heima.test;
import java.io.File;
import java.io.IOException;
import com.heima.bean.MyIO;
import com.heima.bean.UseOnlyTen;
public class test1 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
UseOnlyTen o = new UseOnlyTen(new MyIO(new File("xxx.txt")));
o.readTxt();
}
} |
|