public class MainTest {
public static boolean life=true;
public static long length; //文件大小
public static byte[] buf; //缓冲区
public static int count=0; //缓冲区大小
File input=new File("input.txt");
File output=new File("output.txt");
if(!input.exists()||!output.exists()){
System.out.println("Error!There is no file to input or output!");
}
else{
ReadT reader=new ReadT(input);
ModifyT modifyer=new ModifyT();
WriteT writer=new WriteT(output);
reader.start();
modifyer.start();
writer.start();
}
}
---------------------------------------------------------------------------------------------
*ReadT类用来读取input文件中的数据
*/
import java.io.*;
public class ReadT extends Thread {
private FileInputStream inputFile;
//构造函数,根据传进来的File对象建立输入流,并且将MainTest中的数据进行初始化
ReadT(File input){
//建立输入流,抛出异常
try{
inputFile=new FileInputStream(input);
}
catch(IOException e){
System.out.println(e);
System.exit(1);
}
MainTest.length=input.length(); //得到文件大小
//如果文件什么都没有,那么报错并退出程序
if(MainTest.length==0){
System.out.println("Nothing to do!");
System.exit(1);
}
MainTest.count=(int)MainTest.length/7; //建立缓冲区
MainTest.buf=new byte[MainTest.count];
}
public void run(){
try{
while((Flag=inputFile.read(MainTest.buf))!=-1){
System.out.println("ReadT");
this.yield();
}
MainTest.life=false;
System.out.println("ReadOver!");
inputFile.close();
}
catch(IOException e){
System.out.println(e);
System.exit(1);
}
}
}
---------------------------------------------------------------------------------------------
*ModifyT用来修改文件,实现数据+1
*/
public class ModifyT extends Thread{
private Integer tempnumi;
private int temp;
private Byte tempnumb;
public void run(){
for(int i=0;i<MainTest.count;i++){
if(!MainTest.life) return;
/* tempnumb=new Byte(MainTest.buf[i]);
temp=tempnumb.intValue();
temp=temp+1;
tempnumi=new Integer(temp);
MainTest.buf[i]=tempnumi.byteValue(); */
System.out.println("ModifyT");
this.yield();
}
}
}
---------------------------------------------------------------------------------------------
*WriteT类用来将修改后的文件写入output
*/
import java.io.*;
public class WriteT extends Thread{
private FileOutputStream outputFile;
//建立输出流,抛出异常
try{
outputFile=new FileOutputStream(output);
}
catch(IOException e){
System.out.println(e);
System.exit(1);
}
}
public void run(){
try{
for(int i=0;i<MainTest.count;i++){
if(!MainTest.life) return;
outputFile.write(MainTest.buf);
System.out.println("WriteT");
this.yield();
}
outputFile.close();
}
catch(Exception e){
System.out.println(e);
System.exit(1);
}
}
}