1. Trang chủ
  2. » Thể loại khác

java properties class

4 160 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Lớp Properties trong Java

    • Ví dụ

Nội dung

http://vietjack.com/java/index.jsp Copyright © vietjack.com Lớp Properties Java Lớp Properties Java lớp phụ lớp Hashtable Nó sử dụng để trì danh sách value key String value String Lớp Properties sử dụng nhiều lớp Java khác Ví dụ, kiểu đối tượng trả System.getProperties() đạt value môi trường Để hiểu sâu khái niệm trình bày chương này, mời bạn tham khảo loạt bài: Ví dụ Cấu trúc liệu (Data Structure) Java Properties định nghĩa biến instance sau Biến giữ property list mặc định mà liên kết với đối tượng Properties Java Properties defaults; Properties định nghĩa hai constructor Phiên tạo đối tượng Properties mà khơng có giá trị mặc định: Properties( ) Form thứ hai tạo đối tượng mà sử dụng propDefault cho value mặc định Trong hai trường hợp, property list trống Properties(Properties propDefault) Ngoài phương thức định nghĩa Hashtable, lớp Properties Java định nghĩa phương thức sau: STT Phương thức Miêu tả String getProperty(String key) Trả value mà liên kết với key Một đối tượng null trả key danh sách khơng có property list mặc định String getProperty(String key, String defaultProperty) Trả value mà liên kết với key defaultPropperty trả key khơng có http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java/index.jsp Copyright © vietjack.com danh sách khơng có property list mặc định void list(PrintStream streamOut) Gửi property list tới output stream mà liên kết tới streamOut void list(PrintWriter streamOut) Gửi property list tới output stream mà liên kết tới streamOut void load(InputStream streamIn) throws IOException Nhập property list từ input stream mà liên kết tới streamIn Enumeration propertyNames( ) Trả liệt kê key Nó bao gồm key tìm thấy property list mặc định Object setProperty(String key, String value) Liên kết value với key Trả value trước mà liên kết với key, trả null khơng có liên kết tồn void store(OutputStream streamOut, String description) Sau ghi chuỗi xác định description, property list ghi tới output stream mà liên kết tới streamOut Ví dụ Chương trình sau minh họa số phương thức hỗ trợ lớp Properties Java: import java.util.*; public class PropDemo { public static void main(String args[]) { http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java/index.jsp Copyright © vietjack.com Properties capitals = new Properties(); Set states; String str; capitals.put("Illinois", "Springfield"); capitals.put("Missouri", "Jefferson City"); capitals.put("Washington", "Olympia"); capitals.put("California", "Sacramento"); capitals.put("Indiana", "Indianapolis"); // Show all states and capitals in hashtable states = capitals.keySet(); // get set-view of keys Iterator itr = states.iterator(); while(itr.hasNext()) { str = (String) itr.next(); System.out.println("The capital of " + str + " is " + capitals.getProperty(str) + "."); } System.out.println(); // look for state not in list specify default str = capitals.getProperty("Florida", "Not Found"); System.out.println("The capital of Florida is " + str + "."); } } Nó cho kết sau: The capital of Missouri is Jefferson City The capital of Illinois is Springfield The capital of Indiana is Indianapolis The capital of California is Sacramento The capital of Washington is Olympia http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com/java/index.jsp Copyright © vietjack.com The capital of Florida is Not Found http://vietjack.com/ Trang chia sẻ học online miễn phí Page ... lớp Properties Java: import java. util.*; public class PropDemo { public static void main(String args[]) { http://vietjack.com/ Trang chia sẻ học online miễn phí Page http://vietjack.com /java/ index.jsp... học online miễn phí Page http://vietjack.com /java/ index.jsp Copyright © vietjack.com Properties capitals = new Properties( ); Set states; String str; capitals.put("Illinois", "Springfield"); capitals.put("Missouri",...http://vietjack.com /java/ index.jsp Copyright © vietjack.com danh sách khơng có property list mặc định void list(PrintStream

Ngày đăng: 03/12/2017, 00:16

TỪ KHÓA LIÊN QUAN

w