Preferences. 2 Persistent storage Information is persistent if it is kept between one invocation of a program and the next Many programs keep user preferences.

Slides:



Advertisements
Similar presentations
Bruce Scharlau, University of Aberdeen, 2012 Data storage options for mobiles Mobile Computing.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
CS0007: Introduction to Computer Programming
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Overview of Data Structures and Algorithms
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Slide 10.1 Advanced Programming 2004, based on LY Stefanus’s Slides Object Serialization Object serialization: the process of converting an object into.
 data/data-storage.html#pref data/data-storage.html#pref 
CompSci Searching & Sorting. CompSci Searching & Sorting The Plan  Searching  Sorting  Java Context.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
CS 106 Introduction to Computer Science I 09 / 14 / 2007 Instructor: Michael Eckmann.
1 The Properties Pattern Based on
The Properties Pattern
Lecture 17 FS APIs and vsfs. File and File Name What is a File? Array of bytes. Ranges of bytes can be read/written. File system consists of many files,
Data Storage: Part 1 (Preferences)
UML Basics & Access Modifier
IS-907 Java EE JPA: Simple Object-Relational Mapping.
CSC 213 – Large Scale Programming. Today’s Goals  Look at how Dictionary s used in real world  Where this would occur & why they are used there  In.
CS378 - Mobile Computing Persistence. Saving State We have already seen saving app state into a Bundle on orientation changes or when an app is killed.
4.1 Instance Variables, Constructors, and Methods.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
The Java Programming Language
JDBC. JDBC stands for Java Data Base Connectivity. JDBC is different from ODBC in that – JDBC is written in Java (hence is platform independent, object.
Nilesh Singh Local Data Storage option Android provides several options for you to save persistent application data. - Shared preferences - Creation.
Data persistence How to save data using SharedPreferences, Files, and SQLite database 1Data persistence.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
23-Oct-15 Abstract Data Types. 2 Data types A data type is characterized by: a set of values a data representation, which is common to all these values,
JDBC Java and Databases. RHS – SOC 2 JDBC JDBC – Java DataBase Connectivity An API (i.e. a set of classes and methods), for working with databases in.
Operating Systems COMP 4850/CISG 5550 File Systems Files Dr. James Money.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Mathematical Calculations in Java Mrs. G. Chapman.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
Activity 생명주기 UNIT 13 로봇 SW 콘텐츠 교육원 조용수. 학습 목표 Logcat 액티비티 생명주기를 설명할 수 있다. 현재 상태를 저장할 수 있다. 2.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Programming in Java CSCI-2220 Object Oriented Programming.
(1) A Proposal for the Java Public Middleware API Vito Baggiolini SL/CO.
Data Types – Reference Types Objective To understand what reference types are The need to study reference types To understand Java standard packages To.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.
Module 7: Constructors #1 2000/2001Scientific Computing in OOCourse code 3C59 Module 7: Constructors and Destructors: In this module we will cover: Constructors.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Preferences. 2 Persistent storage Information is persistent if it is kept between one invocation of a program and the next Many programs keep user preferences.
CHAPTER 9 File Storage Shared Preferences SQLite.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Building Java Programs Generics, hashing reading: 18.1.
Android Application Data Storage 1.
Working with Java.
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Programming Language Concepts (CIS 635)
CompSci 230 Software Construction
null, true, and false are also reserved.
Chapter 1: Computer Systems
Testing and debugging A short interlude 2-Dec-18.
Java Classes and Objects 3rd Lecture
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Java Programming Language
Testing and debugging A short interlude 17-Apr-19.
Preferences.
Preferences.
Preferences.
Preferences.
Preferences.
Preferences.
Preference Activity class
Presentation transcript:

Preferences

2 Persistent storage Information is persistent if it is kept between one invocation of a program and the next Many programs keep user preferences in a persistent fashion Sometimes the user sets these explicitly (in, say, a dialog box) Sometimes these are implicit settings, such as window size and position To keep persistent information, Write it to a file whenever it is changed (or when the program quits), and Read it in again whenever the program starts up Java makes this very easy to do

3 Types of preferences Java distinguishes user preferences, for a particular user, and system preferences, for everybody Each “program” may have its own preferences file Java saves and restores preferences for a particular class Preferences are kept in a hierarchical tree structure We will consider only the simplest version, in which preference settings act like a hash table

4 Constructing a Preferences object import java.util.prefs.*; public class MyProgram {... } private Preferences userPrefs; private Preferences systemPrefs; userPrefs = Preferences.userNodeForPackage(MyProgram.class); systemPrefs = Preferences.systemNodeForPackage(MyProgram.class); Note that MyProgram.class returns the Class of MyProgram

5 Saving preferences The following are methods of your Preferences object: void put(String key, String value) void putBoolean(String key, boolean value) void putByteArray(String key, byte[] value) void putDouble(String key, double value) void putFloat(String key, float value) void putInt(String key, int value) void putLong(String key, long value) void remove(String key) // remove this preference void clear() // remove all preferences void sync() // update preferences file now

6 Getting saved preferences All getter methods must supply a default value, in case the preferences files does not exist or cannot be read The following are methods of your Preferences object: String get(String key, String default) boolean getBoolean(String key, boolean default) byte[] getByteArray(String key, byte[] default) double getDouble(String key, double default) float getFloat(String key, float default) int getInt(String key, int default) long getLong(String key, long default) String[] keys()

7 How does this work? Changes to the Preferences object (via put or put XXX) happen automatically--all file I/O is done for you You only need sync() if you want to force the file update to happen immediately rather than eventually We have treated the preferences file as if it were a flat file; however, it’s actually a tree structure If you want to know more, go to the API The preferences file is in XML format Java puts the preferences file in a system-dependent location on your computer You can export to/import from a particular file You can view/edit this XML directly if you like

8 Final comments As described in these slides, a Preferences object is simply a hash table that is “magically” kept between invocations of your program As such, preferences are really easy to use Preference files are really intended to be used for small amounts of persistent data (for example, preferences!) For large amounts of data, you really should use a database instead

9 The End