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
 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 
CS 106 Introduction to Computer Science I 09 / 14 / 2007 Instructor: Michael Eckmann.
1 The Properties Pattern Based on
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.
Data Storage: Part 1 (Preferences)
UML Basics & Access Modifier
IS-907 Java EE JPA: Simple Object-Relational Mapping.
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.
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.
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.
Diagnostic Pathfinder for Instructors. Diagnostic Pathfinder Local File vs. Database Normal operations Expert operations Admin operations.
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.
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.
Mathematical Calculations in Java Mrs. C. Furman.
 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.
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.
* DataSpace is a third party add-on to ArcGIS Desktop * Developed by US Bureau of Reclamations * Allows the user to arrange data without actually moving.
CHAPTER 9 File Storage Shared Preferences SQLite.
Recover iPhone Data from iTunes Backup Files As we know, the disaster is not expected before it happens on your iPhone. So, it is quite important a thing.
CompSci Toward understanding data structures  What can be put in a TreeSet?  What can be sorted?  Where do we find this information?  How do.
Android Application Data Storage 1.
More Sophisticated Behavior
Working with Java.
Module 11: File Structure
Phil Tayco Slide version 1.0 Created Sep 18, 2017
3 Introduction to Classes and Objects.
Introduction to Classes and Objects
Introduction to Computer Science / Procedural – 67130
Java Beans Sagun Dhakhwa.
Prepare data for importing
Classes & Objects.
Programming Language Concepts (CIS 635)
CompSci 230 Software Construction
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Predefined Dialog Boxes
Defining Classes and Methods
Chapter 1: Computer Systems
Testing and debugging A short interlude 2-Dec-18.
Coding Concepts (Data Structures)
Java Classes and Objects 3rd Lecture
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Classes and Objects Static Methods
Introduction to Primitives
Testing and debugging A short interlude 17-Apr-19.
Preferences.
Preferences.
CS2013 Lecture 7 John Hurley Cal State LA.
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Securing and Sharing a Presentation
Preferences.
Preferences.
Preferences.
Preference Activity class
Presentation transcript:

Preferences

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

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

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

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

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()

How does this work? Changes to the Preferences object (via put or putXXX) 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

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

The End