<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    復雜的事情要簡單做;   簡單的事情要認真做;   認真的事情要重復做;   重復的事情要創造性地做。

    信心 恒心 決心 創意 樂意

    も軍魂ミ 心系java

    Java的魅力:跨平臺、動態的Web、Internet計算 Java在Web、移動設備以及云計算方面前景廣闊 我的java編程不是夢!!! 俗話說的好:“授人以魚不如授人以漁”.
    posts - 10, comments - 12, trackbacks - 0, articles - 5
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    按選項選擇操作的io小程序


     


       1)、 在主程序中就調用一個Menu方法;
                                  public class Main {
                                        public static void main(String[] args){
                                                  new Menu();
                                             }
                                         }
       2)、在vo包里包裝一個Person類,并實現Serializable接口, 且定義四個屬性:姓名,學號,年齡,成績;
      
       
       3)、在op包里建立兩個類,分別為 FileOperateInputData,前者主要實現了文件的讀、寫即保存和輸出;后者主要構建了兩個方法,一個是字符串的輸入 ,另一個是整數的輸入,并判斷輸入的是否為真。 
                                      
     

    public class FileOperate {
     public static final String FILENAME="d:\\person.ser";
     //把對象保存在文件之中
     public void save(Object obj){
      ObjectOutputStream out = null;
      try{
       out = new ObjectOutputStream(new FileOutputStream(new File(FILENAME)));
       //寫入對象
       out.writeObject(obj);
      }catch(Exception e){
       try{
        throw e;
       }catch(Exception e1){}
      }
      finally{
       try{
        out.close();
       }catch(Exception e){}
      }
       }
     //把對象從文件中讀出來
     public Object read() throws Exception{
      Object obj = null;
      ObjectInputStream input = null;
      try{
       input = new ObjectInputStream(new FileInputStream(new File(FILENAME)));
                obj = input.readObject();
      }catch(Exception e){
       throw e;                                     
      }
      finally{
       try{
        input.close();
       }catch(Exception e){}
      }
      return obj;
     }

    }

    public class InputData {
     private BufferedReader buf = null;
     //將字節的輸入流量變為字符流,之后放入緩沖之中
     public InputData(){
           buf = new BufferedReader(new InputStreamReader(System.in));
     };
        public String getString(){
         String str = null;
         try{
          str=buf.readLine();
         }catch(IOException e){}
        
         return str;
        }
        
     public int getInt(){
      int temp=0;
      //如果輸入的不是數字,至少應該有一個提示,告訴用戶輸入錯了
      //可以使用正則驗證
      String str = null;
      boolean flag = true;
      while(flag){
       //輸入數據
       str = this.getString();
       if(!(str.matches("\\d+"))){
        //如果輸入的不是一個數字,則必須重新輸入
        System.out.print("輸入的內容必須是整數,請你重新輸入:");
       }
       else{
        //輸入的是一個正確的數字,則可以進行轉換
        temp=Integer.parseInt(str);
        //表示退出循環
        flag = false;
       }
      }
      return temp;
    }
    }
      


        4)、在Menu類里建立控制臺的初始信息;                       
      public class Menu {
         InputData input = null ;
         public Menu(){
          this.input=new InputData();
          //循環出現菜單
          while(true){
          this.show();
          }
         }
      
        //需要定義的菜單內容
     public void show(){
      
      System.out.println("\t\t   1、增加人員信息");                                      
      System.out.println("\t\t   2、瀏覽人員信息");             ;
      System.out.println("\t\t   3、修改人員信息");
      System.out.println("\t\t   4、退出系統");
      System.out.print("\n請選擇要使用的操作:");
      int temp = input.getInt();
      switch(temp){
       case 1:{
        new PersonOperate().add();
        break;
       }
       case 2:{
        new PersonOperate().show();
        break;
       }
       case 3:{
        new PersonOperate().update();
        break;
       }
       case 4:{
        System.out.println("選擇的是退出系統");
        System.out.println("系統退出");
        System.exit(1);
       }
       default: {
        System.out.println("你輸入的內容不正確");
        break;
       }
      }
     }

    }

     
        5)、在PersonOperate類中進行數據的具體操作,完成最終結果的顯示即核心

    import com.dr.util.FileOperate;
    import com.dr.util.InputData;
    import com.dr.vo.Person;

    public class PersonOperate {
     private InputData input =null;
     public PersonOperate(){
      this.input=new InputData();
     }
     //完成具體的Person對象操作
     public void add(){
      //要使用輸入數據的類
      String name = null;
      String id = null;
      int age = 0;
      int score = 0;
      System.out.print("輸入姓名為:");
      name = this.input.getString();
      System.out.print("輸入學號為:");
      id = this.input.getString();
      System.out.print("輸入年齡為:");
      age = this.input.getInt();
      System.out.print("輸入成績為:");
      score = this.input.getInt();
      //生成Person對象,把對象保存在文件中
      Person p = new Person(name,id,age,score);
      
      try{
       new FileOperate().save(p);  //io操作層
       System.out.println("數據保存成功!");
      }catch(Exception e){
       System.out.println("數據保存失敗!");
      }
     }
     public void show(){
      //從文件中把內容讀進來
      Person p = null;
      try{
       p = (Person) new FileOperate().read();
      }catch(Exception e){
       System.out.println("內容顯示失敗,請確定數據是否存在!");
      }
      if(p!=null){
       System.out.println(p);
      }
     }
     public void update(){
      //先將之前的信息查出來
      Person p = null;
      try{
       p = (Person) new FileOperate().read();
      }catch(Exception e){
       System.out.println("內容顯示失敗,請確定數據是否存在!");
      }
      if(p!=null){
       String name = null;
       String id= null;
       int age = 0;
       int score=0;
       System.out.print("請輸入新的姓名(原姓名為:"+p.getName()+")");
       name = this.input.getString();
       System.out.print("請輸入新的學號(原學號為:"+p.getId()+")");
          id = this.input.getString();
       System.out.print("請輸入新的年齡(原年齡為:"+p.getAge()+")");
       age = this.input.getInt();
       System.out.print("請輸入新的成績(原成績為:"+p.getScore()+")");
       score = this.input.getInt();
       
       //信息重新設置
       p.setName(name);
       p.setId(id);
       p.setAge(age);
       p.setScore(score);
       try{
        new FileOperate().save(p);
        System.out.println("數據更新成功!");
       }catch(Exception e){
        System.out.println("數據更新失敗!");
       }
      }
      
     }

    }

       
         6)、程序最后的運行結果及方式:

     最后希望大家給予點評,我好做修改!謝謝!

                                            

















    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 999在线视频精品免费播放观看| 午夜免费福利小电影| 国产麻豆免费观看91| 亚洲国产欧美国产综合一区| 毛片免费观看网站| 亚洲欧美成人av在线观看| 成年人网站在线免费观看| 青柠影视在线观看免费| 亚洲国产精品VA在线看黑人| 日本在线免费播放| 又黄又爽无遮挡免费视频| 亚洲av无码专区在线电影天堂| 精品熟女少妇a∨免费久久| 青青青视频免费观看| 在线观看特色大片免费视频| 亚洲二区在线视频| 在线a毛片免费视频观看| 国产亚洲精品免费| 亚洲精品乱码久久久久久蜜桃不卡| 9久久免费国产精品特黄| 亚洲经典在线观看| 噜噜嘿在线视频免费观看| eeuss草民免费| 久久久久亚洲AV无码专区首JN| 成人黄动漫画免费网站视频 | 国产成人涩涩涩视频在线观看免费| 精品久久久久久亚洲中文字幕| 国产午夜亚洲精品理论片不卡 | 四虎国产精品永免费| 亚洲人色婷婷成人网站在线观看| 桃子视频在线观看高清免费视频 | 亚洲人成网站18禁止久久影院 | 亚洲人成亚洲人成在线观看 | 永久看日本大片免费35分钟| 亚洲熟妇自偷自拍另欧美| 国产午夜亚洲不卡| 男女免费观看在线爽爽爽视频| 免费手机在线看片| 亚洲精品资源在线| 免费日本黄色网址| 18未年禁止免费观看|