做好了第一個程序~

   操作系統課上留的作業,終於完成一個了~應該說,基本完成,還是有點點瑕疵,等到上機的時候要修改一下~
   其實聽起來很簡單,3個線程,一個從文件讀數據,一個修改數據,一個將數據寫到文件。但現在的我,對於io和thread的掌握還差很多,邊學邊做,浪費了一些時間,但收穫也頗豐~
   談談這次編成的收穫:
  1.API很重要,學會查API會有極大幫助                                       
  2.對於io有初步認識,瞭解了一些最基本的方法,但由於知道的API較少,有時候容易陷入迷惑~
  3.對於線程,看了看Doug Lea的書,完全看不懂,有必要以後多看,線程是一把雙刃劍,很有可能就用的不正確,但掌握之後,便是極其“爽”~尤其其同步問題,是一個很值得鑽研的問題
---------------------------------------------------------------------------------------------
import java.io.*;
public class MainTest {
 
 public static boolean life=true;
 public static long length;                              //文件大小
    public static byte[] buf;                               //緩衝區
    public static int count=0;                             //緩衝區大小
 public static void main(String[] args) throws IOException{
  
  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 int Flag=0;                              //用於判斷是否已經讀取完畢
    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;
 WriteT(File output){
        //建立輸出流,拋出異常
  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);
     }
 }
}
---------------------------------------------------------------------------------------------
發佈了3 篇原創文章 · 獲贊 0 · 訪問量 1510
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章