日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java基础day19

發布時間:2025/3/12 java 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java基础day19 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Java基礎day19

  • Java基礎day19-IO流&Properties集合
  • 1.IO流案例
    • 1.1集合到文件數據排序改進版
      • 1.1.1案例需求
      • 1.1.2分析步驟
      • 1.1.3代碼實現
    • 1.2復制單級文件夾
      • 1.2.1案例需求
      • 1.2.2分析步驟
      • 1.2.3代碼實現
    • 1.3復制多級文件夾
      • 1.3.1案例需求
      • 1.3.2分析步驟
      • 1.3.3代碼實現
    • 1.4復制文件的異常處理
      • 1.4.1基本做法
      • 1.4.2JDK7版本改進
      • 1.4.3JDK9版本改進
    • 2.IO特殊操作流
    • 2.2標準輸出流【應用】
    • 2.3字節打印流【應用】
    • 2.4字符打印流
    • 2.5復制Java文件打印流改進版
    • 2.6對象序列化流
    • 2.7對象反序列化流
    • 2.8serialVersionUID&transient
  • 3.Properties集合
    • 3.1Properties作為Map集合的使用
    • 3.2Properties作為Map集合的特有方法
    • 3.3Properties和IO流相結合的方法
    • 3.4游戲次數案例

Java基礎day19-IO流&Properties集合

1.IO流案例

1.1集合到文件數據排序改進版

1.1.1案例需求

  • 鍵盤錄入5個學生信息(姓名,語文成績,數學成績,英語成績)。要求按照成績總分從高到低寫入文本文件
  • 格式:姓名,語文成績,數學成績,英語成績 舉例:林青霞,98,99,100

1.1.2分析步驟

  • 定義學生類
  • 創建TreeSet集合,通過比較器排序進行排序
  • 鍵盤錄入學生數據
  • 創建學生對象,把鍵盤錄入的數據對應賦值給學生對象的成員變量
  • 把學生對象添加到TreeSet集合
  • 創建字符緩沖輸出流對象
  • 遍歷集合,得到每一個學生對象
  • 把學生對象的數據拼接成指定格式的字符串
  • 調用字符緩沖輸出流對象的方法寫數據
  • 釋放資源
  • 1.1.3代碼實現

    /* 1. 定義學生類 2. 創建TreeSet集合,通過比較器排序進行排序 3. 鍵盤錄入學生數據 4. 創建學生對象,把鍵盤錄入的數據對應賦值給學生對象的成員變量 5. 把學生對象添加到TreeSet集合 6. 創建字符緩沖輸出流對象 7. 遍歷集合,得到每一個學生對象 8. 把學生對象的數據拼接成指定格式的字符串 9. 調用字符緩沖輸出流對象的方法寫數據 10. 釋放資源*/ //學生類 public class Student {private String name;private int chinese;private int math;private int english;public Student(String name, int chinese, int math, int english) {this.name = name;this.chinese = chinese;this.math = math;this.english = english;}public Student() {}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getChinese() {return chinese;}public void setChinese(int chinese) {this.chinese = chinese;}public int getMath() {return math;}public void setMath(int math) {this.math = math;}public int getEnglish() {return english;}public void setEnglish(int english) {this.english = english;}public int getSum() {return this.chinese + this.math + this.english;} }//測試類 import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.Comparator; import java.util.Scanner; import java.util.TreeSet;public class test {public static void main(String[] args) throws IOException {//創建TreeSet集合,通過比較器排序進行排序TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {@Overridepublic int compare(Student s1, Student s2) {//成績總分從高到低int num = s2.getSum() - s1.getSum();int num2 = num == 0 ? s2.getChinese() - s1.getChinese() : num;int num3 = num == 0 ? s2.getMath() - s1.getMath() : num2;int num4 = num == 0 ? s2.getName().compareTo(s2.getName()) : num3;return num4;}});//鍵盤錄入學生數據for (int i = 1; i <= 5; i++) {Scanner sc = new Scanner(System.in);System.out.println("請輸入第" + i + "個學生信息");System.out.println("姓名:");String name = sc.nextLine();System.out.println("語文成績:");int chinese = sc.nextInt();System.out.println("數學成績:");int math = sc.nextInt();System.out.println("英語成績:");int english = sc.nextInt();//創建學生對象,把鍵盤錄入的數據對應賦值給學生對象的成員變量Student s = new Student();s.setName(name);s.setChinese(chinese);s.setMath(math);s.setEnglish(english);//把學生對象添加到TreeSet集合ts.add(s);}//創建字符緩沖輸出流對象BufferedWriter bw = new BufferedWriter(new FileWriter("src\\ts.txt"));//遍歷集合,得到每一個學生對象for (Student s : ts) {//把學生對象的數據拼接成指定格式的字符串// 格式:姓名,語文成績,數學成績,英語成績StringBuilder sb = new StringBuilder();sb.append("[").append(s.getName()).append(",").append(s.getChinese()).append(",").append(s.getMath()).append(",").append(s.getEnglish()).append("]").append(",").append(s.getSum());System.out.println(sb);//調用字符緩沖輸出流對象的方法寫數據bw.write(sb.toString());bw.newLine();bw.flush();}//釋放資源bw.close();} }

    1.2復制單級文件夾

    1.2.1案例需求

    • 把“E:\itcast”這個文件夾復制到模塊目錄下

    1.2.2分析步驟

    1. 創建數據源目錄File對象,路徑是E:\itcast 2. 獲取數據源目錄File對象的名稱 3. 創建目的地目錄File對象,路徑由(模塊名+第2步獲取的名稱)組成 4. 判斷第3步創建的File是否存在,如果不存在,就創建 5. 獲取數據源目錄下所有文件的File數組 6. 遍歷File數組,得到每一個File對象,該File對象,其實就是數據源文件 7. 獲取數據源文件File對象的名稱 8. 創建目的地文件File對象,路徑由(目的地目錄+第7步獲取的名稱)組成 9. 復制文件由于不清楚數據源目錄下的文件都是什么類型的,所以采用字節流復制文件采用參數為File的構造方法

    1.2.3代碼實現

    /* 1. 創建數據源目錄File對象,路徑是E:\itcast 2. 獲取數據源目錄File對象的名稱 3. 創建目的地目錄File對象,路徑由(模塊名+第2步獲取的名稱)組成 4. 判斷第3步創建的File是否存在,如果不存在,就創建 5. 獲取數據源目錄下所有文件的File數組 6. 遍歷File數組,得到每一個File對象,該File對象,其實就是數據源文件 7. 獲取數據源文件File對象的名稱 8. 創建目的地文件File對象,路徑由(目的地目錄+第7步獲取的名稱)組成 9. 復制文件 由于不清楚數據源目錄下的文件都是什么類型的,所以采用字節流復制文件 采用參數為File的構造方法*/import java.io.*;public class test2 {public static void main(String[] args) throws IOException {//創建數據源目錄File對象,路徑是E:\\itcastFile srcFolder = new File("E:\\itcast");//獲取數據源目錄File對象的名稱(itcast)String srcFloderName = srcFolder.getName();//創建目的地目錄File對象,路徑名是模塊名+itcast組成(myCharStream\\itcast)File destFloder = new File("src", srcFloderName);//判斷目的地目錄對應的File是否存在,如果不存在,就創建if (!destFloder.exists()) {destFloder.mkdir();}//獲取數據源目錄下所有文件的File數組File[] listFiles = srcFolder.listFiles();//遍歷File數組,得到每一個File對象,該File對象,其實就是數據源文件for (File srcFile : listFiles) {//數據源文件:E:\\itcast\\mn.jpg// 獲取數據源文件File對象的名稱(mn.jpg)String srcFileName = srcFile.getName();//創建目的地文件File對象,路徑名是目的地目錄+mn.jpg組成 (myCharStream\\itcast\\mn.jpg)File destFile = new File(destFloder, srcFileName);//調用方法復制文件copyFile(srcFile, destFile);}}private static void copyFile(File srcFile, File destFile) throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));byte[] bys = new byte[1024];int len;while ((len = bis.read(bys)) != -1) {bos.write(bys, 0, len);}//釋放資源bis.close();bos.close();} }

    1.3復制多級文件夾

    1.3.1案例需求

    • 把“E:\itcast”這個文件夾復制到 F盤目錄下

    1.3.2分析步驟

  • 創建數據源File對象,路徑是E:\itcast
  • 創建目的地File對象,路徑是F:\
  • 寫方法實現文件夾的復制,參數為數據源File對象和目的地File對象
  • 判斷數據源File是否是文件
    是文件:直接復制,用字節流
    不是文件:
    在目的地下創建該目錄
    遍歷獲取該目錄下的所有文件的File數組,得到每一個File對象
    回到3繼續(遞歸)
  • 1.3.3代碼實現

    import java.io.*;/* 1. 創建數據源File對象,路徑是E:\itcast 2. 創建目的地File對象,路徑是F:\ 3. 寫方法實現文件夾的復制,參數為數據源File對象和目的地File對象 4. 判斷數據源File是否是文件 是文件:直接復制,用字節流 不是文件: 在目的地下創建該目錄 遍歷獲取該目錄下的所有文件的File數組,得到每一個File對象 回到3繼續(遞歸) */ public class test {public static void main(String[] args) throws IOException {//創建數據源File對象,路徑是E:\itcastFile srcFile = new File("E:\\itcast");//創建目的地File對象,路徑是主文件夾下File destFile = new File("src");//寫方法實現文件夾的復制,參數為數據源File對象和目的地File對象copyFolder(srcFile, destFile);}//復制文件夾private static void copyFolder(File srcFile, File destFile) throws IOException {//判斷數據源File是否是目錄if (srcFile.isDirectory()) {//在目的地下創建該目錄String srcFileName = srcFile.getName();File newFolder = new File(destFile, srcFileName);if (!newFolder.exists()) {newFolder.mkdir();}//獲取數據源File下所有文件或者目錄的File數組File[] filearray = srcFile.listFiles();//遍歷獲取該目錄下的所有文件的File數組,得到每一個File對象for (File file : filearray) {//把該File作為數據源File對象,遞歸調用復制文件夾的方法copyFolder(file, newFolder);}} else {//說明是文件,直接復制,用字節流File newFile = new File(destFile, srcFile.getName());copyFile(srcFile, newFile);}}//字節緩沖流復制文件private static void copyFile(File srcFile, File newFile) throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(newFile));byte[] bys = new byte[1024];int len;while ((len = bis.read()) != -1) {bos.write(bys, 0, len);}bos.close();bis.close();} }

    1.4復制文件的異常處理

    1.4.1基本做法

    import java.io.FileReader; import java.io.FileWriter; import java.io.IOException;public class test2 {public static void main(String[] args) {}//拋出處理private static void method1() throws IOException {FileReader fr = new FileReader("fr.txt");FileWriter fw = new FileWriter("fw.txt");char[] chs = new char[1024];int len;while ((len = fr.read()) != -1) {fw.write(chs, 0, len);}fw.close();fr.close();}//try...catch...finallyprivate static void method2() {FileReader fr = null;FileWriter fw = null;try {fr = new FileReader("fr.txt");fw = new FileWriter("fw.txt");char[] chs = new char[1024];int len;while ((len = fr.read()) != -1) {fw.write(chs, 0, len);}} catch (IOException e) {e.printStackTrace();} finally {if (fw != null) {try {fw.close();} catch (IOException e) {e.printStackTrace();}}if ((fr != null)) {try {fr.close();} catch (IOException e) {e.printStackTrace();}}}}}

    1.4.2JDK7版本改進

    public class test2 {public static void main(String[] args) {}//JDK7的改進方案public static void method3(){try {FileReader fr = new FileReader("fr.txt");FileWriter fw = new FileWriter("fw.txt");char[] chs = new char[1024];int len;while ((len = fr.read()) != -1) {fw.write(chs, 0, len);}}catch (IOException e){e.printStackTrace();}}

    1.4.3JDK9版本改進

    public class test2 {public static void main(String[] args) {}//JDK9的改進方案public static void method4() throws IOException{FileReader fr = new FileReader("fr.txt");FileWriter fw = new FileWriter("fw.txt");try (fr;fw){char[] chs = new char[1024];int len;while ((len = fr.read()) != -1) {fw.write(chs, 0, len);}}catch (IOException e){e.printStackTrace();}}

    2.IO特殊操作流

    2.1標準輸入流【應用】

    • System類中有兩個靜態的成員變量
      public static final InputStream in:標準輸入流。通常該流對應于鍵盤輸入或由主機環境或用戶指定的另一個輸入源
      public static final PrintStream out:標準輸出流。通常該流對應于顯示輸出或由主機環境或用戶指定的另一個輸出目標
    • 自己實現鍵盤錄入數據
    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner;public class test3 {public static void main(String[] args) throws IOException {//public static final InputStream in:標準輸入流 // InputStream is = System.in; // // int by; // while ((by = is.read()) != -1){ // System.out.print((char)by); // }//如何把字節流轉換為字符流?用轉換流 // InputStreamReader isr = new InputStreamReader(is); // //使用字符流能不能夠實現一次讀取一行數據呢?可以 // // 但是,一次讀取一行數據的方法是字符緩沖輸入流的特有方法 // BufferedReader br = new BufferedReader(isr);//化簡BufferedReader br = new BufferedReader(new InputStreamReader(System.in));System.out.println("請輸入一個字符串:");String line = br.readLine();System.out.println("你輸入的字符串是:" + line);System.out.println("請輸入一個整數:");int i = Integer.parseInt(br.readLine());System.out.println("你輸入的整數是:" + i);//自己實現鍵盤錄入數據太麻煩了,所以Java就提供了一個類供我們使用Scanner sc = new Scanner(System.in);} }

    2.2標準輸出流【應用】

    • System類中有兩個靜態的成員變量
      public static final InputStream in:標準輸入流。通常該流對應于鍵盤輸入或由主機環境或用戶指定的另一個輸入源
      public static final PrintStream out:標準輸出流。通常該流對應于顯示輸出或由主機環境或用戶指定的另一個輸出目標
    • 輸出語句的本質:是一個標準的輸出流
      PrintStream ps = System.out;
      PrintStream類有的方法,System.out都可以使用
    • 示例代碼
    import java.io.PrintStream;public class test4 {public static void main(String[] args) {//public static final PrintStream out:標準輸出流PrintStream ps = System.out;//能夠方便地打印各種數據值ps.print("hello");ps.print(100);//System.out的本質是一個字節輸出流System.out.println("hello");System.out.println(100);System.out.println(); // System.out.print();} }

    2.3字節打印流【應用】

    • 打印流分類
      字節打印流:PrintStream
      字符打印流:PrintWriter
    • 打印流的特點
      只負責輸出數據,不負責讀取數據
      永遠不會拋出IOException
      有自己的特有方法
    • 字節打印流
      PrintStream(String fileName):使用指定的文件名創建新的打印流
      使用繼承父類的方法寫數據,查看的時候會轉碼;使用自己的特有方法寫數據,查看的數據原樣輸出
      可以改變輸出語句的目的地
      public static void setOut(PrintStream out):重新分配“標準”輸出流
    • 示例代碼
    import java.io.IOException; import java.io.PrintStream;public class test5 {public static void main(String[] args) throws IOException {//PrintStream(String fileName):使用指定的文件名創建新的打印流PrintStream ps = new PrintStream("src\\ps.txt");//寫數據//字節輸出流有的方法ps.write(97); //a//使用特有方法寫數據ps.print(98); //98ps.println();ps.println(100);//釋放資源ps.close();} }

    2.4字符打印流

    • 字符打印流構造房方法
    方法名說明
    PrintWriter(String fileName)使用指定的文件名創建一個新的PrintWriter,而不需要自動執行刷新
    PrintWriter(Writer out, boolean autoFlush)創建一個新的PrintWriter out:字符輸出流 autoFlush: 一個布爾值,如果為真,則println , printf ,或format方法將刷新輸出緩沖區
    • 示例代碼
    import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter;public class test6 {public static void main(String[] args) throws IOException {//PrintWriter(String fileName) :使用指定的文件名創建一個新的PrintWriter,而不需要自動執行行刷新 // PrintWriter pw = new PrintWriter("src\\pw.txt"); // pw.write("hello"); // pw.write("\r\n"); // pw.flush(); // pw.println("world"); // pw.flush();//PrintWriter(Writer out, boolean autoFlush):創建一個新的PrintWriterPrintWriter pw = new PrintWriter(new FileWriter("src\\pw.txt"),true);pw.println("hello");pw.close();} }

    2.5復制Java文件打印流改進版

    • 案例需求
      把模塊目錄下的PrintStreamDemo.java 復制到模塊目錄下的 Copy.java
    • 分析步驟
      根據數據源創建字符輸入流對象
      根據目的地創建字符輸出流對象
      讀寫數據,復制文件
      釋放資源
    • 代碼實現
    import java.io.*;public class test7 {public static void main(String[] args) throws IOException {/* //根據數據源創建字符輸入流對象BufferedReader br = new BufferedReader(new FileReader("myOtherStream\\PrintStreamDemo.java"));//根據目的地創建字符輸出流對象BufferedWriter bw = new BufferedWriter(new FileWriter("myOtherStream\\Copy.java"));//讀寫數據,復制文件String line;while ((line=br.readLine())!=null) {bw.write(line);bw.newLine();bw.flush();}//釋放資源bw.close();br.close();*///根據數據源創建字符輸入流對象BufferedReader br = new BufferedReader(new FileReader("src\\ps.txt"));//根據目的地創建字符輸出流對象PrintWriter pw = new PrintWriter(new FileWriter("src\\Copy.txt"),true);//讀寫數據,復制文件String line;while ((line=br.readLine())!=null) {pw.println(line);}//釋放資源pw.close();br.close();} }

    2.6對象序列化流

    • 對象序列化介紹
      對象序列化:就是將對象保存到磁盤中,或者在網絡中傳輸對象
      這種機制就是使用一個字節序列表示一個對象,該字節序列包含:對象的類型、對象的數據和對象中存儲的屬性等信息
      字節序列寫到文件之后,相當于文件中持久保存了一個對象的信息
      反之,該字節序列還可以從文件中讀取回來,重構對象,對它進行反序列化
    • 對象序列化流: ObjectOutputStream
      將Java對象的原始數據類型和圖形寫入OutputStream。 可以使用ObjectInputStream讀取(重構)對象。 可以通過使用流的文件來實現對象的持久存儲。 如果流是網絡套接字流,則可以在另一個主機上或另一個進程中重構對象
    • 構造方法
    方法名說明
    ObjectOutputStream(OutputStream out)創建一個寫入指定的OutputStream的ObjectOutputStream
    • 序列化對象的方法
    方法名說明
    void writeObject(Object obj)將指定的對象寫入ObjectOutputStream
    • 示例代碼
    //學生類 import java.io.Serializable;public class Student implements Serializable {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';} } //測試類 import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream;public class test8 {public static void main(String[] args) throws IOException {//ObjectOutputStream(OutputStream out):創建一個寫入指定的OutputStream 的ObjectOutputStreamObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src\\oos.txt"));//創建對象Student s = new Student("林青霞", 30);//void writeObject(Object obj):將指定的對象寫入ObjectOutputStreamoos.writeObject(s);//釋放資源oos.close();} }
    • 注意事項
      一個對象要想被序列化,該對象所屬的類必須必須實現Serializable 接口
      Serializable是一個標記接口,實現該接口,不需要重寫任何方法

    2.7對象反序列化流

    • 對象反序列化流: ObjectInputStream
      ObjectInputStream反序列化先前使用ObjectOutputStream編寫的原始數據和對象
    • 構造方法
    方法名說明
    ObjectInputStream(InputStream in)創建從指定的InputStream讀取的ObjectInputStream
    • 反序列化對象的方法
    方法名說明
    Object readObject()從ObjectInputStream讀取一個對象
    • 示例代碼
    import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream;public class test9 {public static void main(String[] args) throws IOException, ClassNotFoundException {//ObjectInputStream(InputStream in):創建從指定的InputStream讀取的 ObjectInputStreamObjectInputStream ois = new ObjectInputStream(new FileInputStream("src\\oos.txt"));//Object readObject():從ObjectInputStream讀取一個對象Object obj = ois.readObject();Student s = (Student) obj;System.out.println(s.getName() + "," + s.getAge());ois.close();} }

    2.8serialVersionUID&transient

    • serialVersionUID
      用對象序列化流序列化了一個對象后,假如我們修改了對象所屬的類文件,讀取數據會不會出問題呢?
      ? ? ? ? 會出問題,會拋出InvalidClassException異常
      如果出問題了,如何解決呢?
      ? ? ? ? 重新序列化
      ? ? ? ? 給對象所屬的類加一個serialVersionUID
      ? ? ? ? ? ? ? ? private static final long serialVersionUID = 42L;
    • transient
      如果一個對象中的某個成員變量的值不想被序列化,又該如何實現呢?
      ??????給該成員變量加transient關鍵字修飾,該關鍵字標記的成員變量不參與序列化過程
    • 示例代碼
    //學生類 import java.io.Serializable;public class Student implements Serializable {private static final long serialVersionUID = 42L;private String name;//private int age;private transient int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}// @Override // public String toString() { // return "Student{" + // "name='" + name + '\'' + // ", age=" + age + // '}'; // }} //測試類 import java.io.*;public class test10 {public static void main(String[] args) throws IOException, ClassNotFoundException {//write();read();}//反序列化private static void read() throws IOException, ClassNotFoundException {ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src\\oos.txt"));Object obj = ois.readObject();Student s = (Student) obj;System.out.println(s.getName() + "," + s.getAge());ois.close();}//序列化private static void write() throws IOException {ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src\\oos.txt"));Student s = new Student("林青霞", 30);oos.writeObject(s);oos.close();} }

    3.Properties集合

    3.1Properties作為Map集合的使用

    • Properties介紹
      是一個Map體系的集合類
      Properties可以保存到流中或從流中加載
      屬性列表中的每個鍵及其對應的值都是一個字符串
    • Properties基本使用
    import java.util.Properties; import java.util.Set;public class test1 {public static void main(String[] args) {//創建集合對象// Properties<String,String> prop = new Properties<String,String>(); //錯誤Properties prop = new Properties();//存儲元素prop.put("林青霞", 30);prop.put("張曼玉", 33);prop.put("王祖賢", 32);//遍歷集合Set<Object> keySet = prop.keySet();for (Object key : keySet) {Object value = prop.get(key);System.out.println(key + "," + value);}} }

    3.2Properties作為Map集合的特有方法

    • 特有方法
    方法名說明
    Object setProperty(String key, String value)設置集合的鍵和值,都是String類型,底層調用 Hashtable方 法 put
    String getProperty(String key)使用此屬性列表中指定的鍵搜索屬性
    Set stringPropertyNames()從該屬性列表中返回一個不可修改的鍵集,其中鍵及其對應的值是字符串
    • 示例代碼
    import java.util.Properties; import java.util.Set;public class test2 {public static void main(String[] args) {//創建集合對象Properties prop = new Properties();//Object setProperty(String key, String value):設置集合的鍵和值,都是 String類型,底層調用Hashtable方法putprop.setProperty("林青霞","30");/*Object setProperty(String key, String value) {return put(key, value);}Object put(Object key, Object value) {return map.put(key, value); }*/prop.setProperty("張曼玉", "33");prop.setProperty("王祖賢", "32");//String getProperty(String key):使用此屬性列表中指定的鍵搜索屬性System.out.println(prop.getProperty("林青霞"));System.out.println(prop.getProperty("王祖賢"));System.out.println(prop);//Set<String> stringPropertyNames():從該屬性列表中返回一個不可修改的鍵集,其中 鍵及其對應的值是字符串Set<String> names = prop.stringPropertyNames();for (String key:names){//System.out.println(key);String value = prop.getProperty(key);System.out.println(key+","+value);}} }

    3.3Properties和IO流相結合的方法

    • 和IO流結合的方法
    方法名說明
    void load(InputStream inStream)從輸入字節流讀取屬性列表(鍵和元素對)
    void load(Reader reader)從輸入字符流讀取屬性列表(鍵和元素對)
    void store(OutputStream out, String comments)將此屬性列表(鍵和元素對)寫入此 Properties表中,以適合于使用load(InputStream)方法的格式寫入輸出字節流
    void store(Writer writer, String comments)將此屬性列表(鍵和元素對)寫入此 Properties表中,以適合使用load(Reader)方法的格式寫入輸出字符流
    • 示例代碼
    import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Properties;public class test3 {public static void main(String[] args) throws IOException {//把集合中的數據保存到文件//myStore();//把文件中的數據加載到集合myLoad();}private static void myLoad() throws IOException {Properties prop = new Properties();FileReader fr = new FileReader("src\\fw.txt");prop.load(fr);fr.close();System.out.println(prop);}private static void myStore() throws IOException {Properties prop = new Properties();prop.setProperty("林青霞","30");prop.setProperty("張曼玉", "33");prop.setProperty("王祖賢", "32");//void store(Writer writer, String comments):FileWriter fw = new FileWriter("src\\fw.txt");prop.store(fw,null);fw.close();} }

    3.4游戲次數案例

    • 案例需求
      實現猜數字小游戲只能試玩3次,如果還想玩,提示:游戲試玩已結束,想玩請充值(www.itcast.cn)
    • 分析步驟
  • 寫一個游戲類,里面有一個猜數字的小游戲
  • 寫一個測試類,測試類中有main()方法,main()方法中寫如下代碼:
    從文件中讀取數據到Properties集合,用load()方法實現
    文件已經存在:game.txt
    里面有一個數據值:count=0
    通過Properties集合獲取到玩游戲的次數
    判斷次數是否到到3次了
    如果到了,給出提示:游戲試玩已結束,想玩請充值(www.itcast.cn)
    如果不到3次:
    次數+1,重新寫回文件,用Properties的store()方法實現玩游戲
    • 代碼實現
    import java.util.Random; import java.util.Scanner;public class GuessNumber {public GuessNumber() {}public static void start() {Random r = new Random();int number = r.nextInt(100) + 1;while (true) {Scanner sc = new Scanner(System.in);System.out.println("請輸入你要猜的數字:");int guessNumber = sc.nextInt();if (guessNumber > number) {System.out.println("你猜的數字" + guessNumber + "太大了");} else if (guessNumber < number) {System.out.println("你猜的數字" + guessNumber + "太小了");} else {System.out.println("恭喜你猜中了");break;}}} }import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.Properties;public class test4 {public static void main(String[] args) throws IOException {//從文件中讀取數據到Properties集合,用load()方法實現Properties prop = new Properties();FileReader fr = new FileReader("src\\game.txt");prop.load(fr);fr.close();//通過Properties集合獲取到玩游戲的次數String count = prop.getProperty("count");int number = Integer.parseInt(count);//判斷次數是否到到3次了if (number >= 3) {//如果到了,給出提示:游戲試玩已結束,想玩請充值(www.itcast.cn)System.out.println("游戲試玩已結束,想玩請充值(www.itcast.cn)");} else {//玩游戲GuessNumber.start();//次數+1,重新寫回文件,用Properties的store()方法實現number++;prop.setProperty("count", String.valueOf(number));FileWriter fw = new FileWriter("src\\game.txt");prop.store(fw, null);fw.close();}} }

    總結

    以上是生活随笔為你收集整理的Java基础day19的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。