当前位置:网站首页>Collection (properties)
Collection (properties)
2022-07-22 10:19:00 【Hua Weiyun】
aggregate (Properties)
The reason for introducing this collection is that it can be used in conjunction with streams .
The specific description of this set is not described in detail in this article , The details will be added in the previous collection article . Give a brief explanation and its usage .
This set starts with Map Subclass under , So it's also a double column set . However, generics are not supported . Use the code to see the specific use . Simple demonstration .
package io_demo;import java.util.Properties;import java.util.Set;public class IoDemo_25 { public static void main(String[] args) { // Create a collection object Properties pro = new Properties(); // Storage elements pro.put(001,"jgdabc"); pro.put(002," Blue boat and thousand sails "); pro.put(002,"lym"); // Ergodic set Set<Object> KeySet = pro.keySet(); for(Object key:KeySet) { Object value = pro.get(key); System.out.print(key+","+value); } pro.setProperty("000","jgdabc"); pro.getProperty("000"); // Get key value Set<String> names = pro.stringPropertyNames();// Get key set // So you can traverse for(String key:names) { String value = pro.getProperty(key); System.out.println(key+value); } }}
== its put() The method key is Object Type of ,setProperty() The key of the method must be String type .== This needs attention .
Assemble to file , File to collection
package io_demo;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.Properties;public class IoDemo_26 { public static void main(String[] args) throws IOException { // Save the data in the collection to a file myStore(); // Load file data into collection myLoad(); } private static void myLoad() throws IOException { Properties prop = new Properties(); FileReader fr = new FileReader("E:\\java_doc\\src\\io_demo\\jgdabc.txt"); prop.load(fr); fr.close(); System.out.print(prop); } private static void myStore() throws IOException { Properties prop = new Properties(); prop.setProperty("000","jack"); prop.setProperty("001","jgdabc"); prop.setProperty("002","john"); FileWriter fw = new FileWriter("E:\\java_doc\\src\\io_demo\\jgdabc.txt"); prop.store(fw,null);//null Does not represent descriptive information }}
Play a number guessing game
Give a in the file count=0, Then use this set to read the data ( Need to be converted to int type , Then limit according to the value , Limit the number of times you play . Of course not, every time it runs, it will count+1, Run three times and you can't play . This is different from interacting all the time .)
First, a number guessing game
package java_practice;import java.util.Scanner;public class GuessNumber { public static void start() { Scanner input = new Scanner(System.in); int number = (int) (Math.random() * 100)+1; // Generate random numbers int guess;// The number the user guessed int count = 0;// Number of guesses System.out.println(" I have a in my heart 0 To 100 Integer between , Guess what ?"); // Users guess random numbers do { guess = input.nextInt(); if (number < guess) { System.out.println(" It's a little big , Guess again !"); count++; } else if (number > guess) { System.out.println(" It's a little smaller , Guess again !"); count++; } else { count++; break; } } while (true); System.out.println(" The number is "+number); System.out.println(" The number of times you guess is "+count); // Give an evaluation according to the number of guesses if (count == 1) { System.out.println(" You are too smart !"); } else if (count >= 2 && count <= 5) { System.out.println(" Pretty good , Make persistent efforts !"); } else { System.out.println(" Work hard !"); } }}
Then a test main class
package io_demo;import java_practice.GuessNumber;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.sql.SQLOutput;import java.util.Properties;public class IoDemo_27 { public static void main(String args[]) throws IOException { GuessNumber guessNumber = new GuessNumber(); Properties prop = new Properties(); FileReader fr = new FileReader("E:\\java_doc\\src\\io_demo\\count.txt"); prop.load(fr); fr.close(); String count = prop.getProperty("count"); int number = Integer.parseInt(count); if(number>=3) { System.out.println(" The game trial is over , If you want to play, please recharge "); }else { guessNumber.start(); prop.setProperty("count",String.valueOf(number)); number++; FileWriter fw = new FileWriter("E:\\java_doc\\src\\io_demo\\count.txt"); prop.store(fw,null); fw.close(); } }}
Be sure to create a file yourself , There is a tag value in it . In this way, the function can be realized .
边栏推荐
猜你喜欢
How many months did you write your first SCI? (Part II)
Unity C: use this keyword to expand class functions
Service & Endpoint
Use of rhcsa simple commands (LS, date, timedatectl, file, STAT), file type
Energy principle and variational method note 06: variational problems of higher-order derivatives (including higher-order derivatives of functions)
Service & Endpoint
测试员官方承认的“五个等级”,来看看你处于哪个阶段?
leetcode:1838. Frequency of the highest frequency element [sort + prefix and + dichotomy + thinking]
1307_嵌入式设计中的晶振测试小结
壁纸背景墙/头像/动态壁纸小程序源码-支持用户投稿-带部分采集功能+搭建教程
随机推荐
OSPF 综合实验
Start, close and view MySQL service (Linux)
基于卡尔曼滤波的微电网调度(Matlab代码实现)
Understanding of web standards and W3C?
壁纸背景墙/头像/动态壁纸小程序源码-支持用户投稿-带部分采集功能+搭建教程
Google Chrome browser will support taking notes on any web page and multi device synchronization
C# 与CAD二次开发,20220721随笔代码
jvm的Finalizer
el-table-column嵌套el-table-column ,多级表头横向滚动的bug
Collection collection overview
Recommend the source code of the finished live broadcast, and set the system date, time and time zone
2022 Baidu world conference ends, Xi tuyuan universe open-air giant screen & customized venue debut
2022开源PHP留言反馈管理系统 v2.0
Some visualization methods of CNN
Find all letter ectopic words in the string
[gestionnaire de projet du système d'information] chapitre IV Structure globale des connaissances en gestion
AtCoder Beginner Contest 260 E - At Least One
Service & Endpoint
Why are there career bottlenecks for test / development programmers? It seems that everything has changed
启动,关闭,查看MySQL服务(Linux)