CS 193A. Data files and storage



Similar documents
getsharedpreferences() - Use this if you need multiple preferences files identified by name, which you specify with the first parameter.

ANDROID APPS DEVELOPMENT FOR MOBILE GAME

Introduction to Java. CS 3: Computer Programming in Java

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

13 File Output and Input

5 HDFS - Hadoop Distributed System

Programming in Java

WHAT ARE PACKAGES? A package is a collection of related classes. This is similar to the notion that a class is a collection of related methods.

File System. /boot /system /recovery /data /cache /misc. /sdcard /sd-ext. Also Below are the for SD Card Fie System Partitions.

GX/GP Firmware Upgrade Procedure

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Xerox Mobile Link 2.0 Frequently Asked Questions (FAQ) - Android

List of FTP commands for the Microsoft command-line FTP client

Basics of Java Programming Input and the Scanner class

Junos Pulse for Google Android

HP AppPulse Mobile. Adding HP AppPulse Mobile to Your Android App

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Android Persistency: Files

FAQ for Eee Pad TF201

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Practice Fusion API Client Installation Guide for Windows

Cybozu Garoon 3 Server Distributed System Installation Guide Edition 3.1 Cybozu, Inc.

The Java I/O System. Binary I/O streams (ascii, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

Sample CSE8A midterm Multiple Choice (circle one)

Sophos Mobile Control user help. Product version: 6.1

How to install the TLW Android Mobile Application:

iaccess FOR ios Contents

OS Upgrade Manual. Close any applications before proceeding with the update. Please connect your USB cable directly to. USB hub.) Do not disconnect

Downloading Electro Scan Smartphone App From Google Play* * Previously known as The Android Market

How to build secure Apache Tomcat deployments with RPM.

Principles of Software Construction: Objects, Design, and Concurrency. Design Case Study: Stream I/O Some answers. Charlie Garrod Jonathan Aldrich

Mobile Print/Scan Guide for Brother iprint&scan

What is an I/O Stream?

TOTAL DEFENSE MOBILE SECURITY USER S GUIDE

AP Computer Science File Input with Scanner

Mobile Print/Scan Guide for Brother iprint&scan

COEN 152 / 252 Lab Exercise 1. Imaging, Hex Editors & File Types

App Development for Smart Devices. Lec #4: Files, Saving State, and Preferences

Web Testing, Java Testing, Server Monitoring. AppPerfect Installation Guide

Readings and References. Topic #10: Java Input / Output. "Streams" are the basic I/O objects. Input & Output. Streams. The stream model A-1.

Mobile Print/Scan Guide for Brother iprint&scan

Chapter 20 Streams and Binary Input/Output. Big Java Early Objects by Cay Horstmann Copyright 2014 by John Wiley & Sons. All rights reserved.

ID TECH UniMag Android SDK User Manual

How to develop your own app

READING DATA FROM KEYBOARD USING DATAINPUTSTREAM, BUFFEREDREADER AND SCANNER

TIBCO Fulfillment Provisioning Session Layer for FTP Installation

Installation Instructions

Installing, Uninstalling, and Upgrading Service Monitor

This software will update your Samsung Galaxy S II to Android software version GB28.

INSTALLATION GUIDE Netop Mobile for Android

CPSC 2800 Linux Hands-on Lab #7 on Linux Utilities. Project 7-1

7 Mini Tablet User Guide

Basic File Recording

TANDBERG MANAGEMENT SUITE 10.0

Files and input/output streams

World Class Security Experts. Security Considerations for the IFS

Uploading files to FTP server

Integrating VoltDB with Hadoop

User Manual. UniMag Magnetic Stripe Reader For Mobile Devices

D06 PROGRAMMING with JAVA

Sharpdesk V3.5. Push Installation Guide for system administrator Version

P3PC ENZ0. ScanSnap N1800 Network Scanner Salesforce Chatter Add-in User s Guide

Point of View SmartTV-500 Center - Android 4.2. General notices for use...2 Disclaimer...2 Box Contents...2

Intel Compute Stick STCK1A32WFC User Guide. Intel Compute Stick STCK1A32WFC

FAQ for Transformer TF201

INPUT AND OUTPUT STREAMS

Continuous Integration Part 2

COMMANDS 1 Overview... 1 Default Commands... 2 Creating a Script from a Command Document Revision History... 10

TIBCO Hawk SNMP Adapter Installation

A+ Practical Applications Solution Key

McAfee Web Gateway 7.4.1

Android Development Exercises Version Hands On Exercises for. Android Development. v

Configuring Your Gateman File Server

Topic 11 Scanner object, conditional execution

March How Eye-Fi Works Overview Flowcharts

Frequently Asked Questions: Cisco Jabber 9.x for Android

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

MFCF Grad Session 2015

FAQ for Eee Pad TF201

Silk Test Testing Mobile Applications

Android Application Development. Daniel Switkin Senior Software Engineer, Google Inc.

Exercises: FreeBSD: Apache and SSL: pre SANOG VI Workshop

Super Manager User Manual. English v /06/15 Copyright by GPC

Comodo Disk Encryption

DS License Server. Installation and Configuration Guide. 3DEXPERIENCE R2014x

Penpower WorldocScan. User's Guide

Printer Connection Manager

Initial DUO 2 Factor Setup, Install, Login and Verification

PrinterOn Mobile App for ios and Android

Persistent Data Storage. Akhilesh Tyagi

Cloud Storage Quick Start Guide

Cloud Attached Storage 3.1 EA

Transcription:

CS 193A Data files and storage This document is copyright (C) Marty Stepp and Stanford Computer Science. Licensed under Creative Commons Attribution 2.5 License. All rights reserved.

Files and storage Android can read/write files from two locations: internal and external storage. Both are persistent storage; data remains after power-off / reboot. internal storage: Built into the device. guaranteed to be present typically smaller (~1-4 gb) can't be expanded or removed specific and private to each app wiped out when the app is uninstalled

File (link) and Streams (link) java.io.file - Objects that represent a file or directory. methods: canread, canwrite, create, delete, exists, getname, getparent, getpath, isfile, isdirectory, lastmodified, length, listfiles, mkdir, mkdirs, renameto java.io.inputstream, OutputStream - Stream objects represent flows of data bytes from/to a source or destination. Could come from a file, network, database, memory,... Normally not directly used; they only include low-level methods for reading/writing a byte (character) at a time from the input. Instead, a stream is often passed as parameter to other objects like java.util.scanner, java.io.bufferedreader, java.io.printstream to do the actual reading / writing.

Using internal storage (link) An activity has methods you can call to read/write files: getfilesdir() - returns internal directory for your app getcachedir() - returns a "temp" directory for scrap files getresources().openrawresource(r.raw.id) - read an input file from res/raw/ openfileinput("name", mode) - opens a file for reading openfileoutput("name", mode) - opens a file for writing You can use these to read/write files on the device. many methods return standard java.io.file objects some return java.io.inputstream or OutputStream objects, which can be used with standard classes like Scanner, BufferedReader, and PrintStream to read/write files (see Java API)

Internal storage example 1 // read a file, and put its contents into a TextView // (assumes hello.txt file exists in res/raw/ directory) Scanner scan = new Scanner( getresources().openrawresource(r.raw.hello)); String alltext = ""; // read entire file while (scan.hasnextline()) { String line = scan.nextline(); alltext += line; } mytextview.settext(alltext); scan.close();

Internal storage example 2 // write a short text file to the internal storage PrintStream output = new PrintStream openfileoutput("out.txt", MODE_PRIVATE)); output.println("hello, world!"); output.println("how are you?"); output.close();... // read the same file, and put its contents into a TextView Scanner scan = new Scanner( openfileinput("out.txt", MODE_PRIVATE)); String alltext = ""; // read entire file while (scan.hasnextline()) { String line = scan.nextline(); alltext += line; } mytextview.settext(alltext); scan.close();

External storage external storage: Card that is inserted into the device. (such as a MicroSD card) can be much larger than internal storage (~8-32 gb) can be removed or transferred to another device if needed may not be present, depending on the device read/writable by other apps and users; not private to your app not wiped when the app is uninstalled, except in certain cases

External storage permission If your app needs to read/write the device's external storage, you must explicitly request permission to do so in your app's AndroidManifest.xml file. On install, the user will be prompted to confirm your app permissions. <manifest...> <uses-permission android:name="android.permission.read_external_storage" /> <uses-permission android:name="android.permission.write_external_storage" />... </manifest>

Using external storage Methods to read/write external storage: getexternalfilesdir("name") - returns "private" external directory for your app with the given name Environment.getExternalStoragePublicDirectory(name) - returns public directory for common files like photos, music, etc. pass constants for name such as Environment.DIRECTORY_ALARMS, DIRECTORY_DCIM, DIRECTORY_DOWNLOADS, DIRECTORY_MOVIES, DIRECTORY_MUSIC, DIRECTORY_NOTIFICATIONS, DIRECTORY_PICTURES, DIRECTORY_PODCASTS, DIRECTORY_RINGTONES You can use these to read/write files on the external storage. the above methods return standard java.io.file objects these can be used with standard classes like Scanner, BufferedReader, and PrintStream to read/write files (see Java API)

External storage example // write short data to app-specific external storage File outdir = getexternalfilesdir(null); // root dir File outfile = new File(outDir, "example.txt"); PrintStream output = new PrintStream(outFile); output.println("hello, world!"); output.close(); // read list of pictures in external storage File picsdir = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES); for (File file : picsdir.listfiles()) {... }

Checking if storage is available /* Checks if external storage is available * for reading and writing */ public boolean isexternalstoragewritable() { return Environment.MEDIA_MOUNTED.equals( Environment.getExternalStorageState()); } /* Checks if external storage is available * for reading */ public boolean isexternalstoragereadable() { return isexternalstoragewritable() Environment.MEDIA_MOUNTED_READ_ONLY.equals( Environment.getExternalStorageState()); }

Accessing web data (link) To read data from the web, first request the INTERNET permission in your AndroidManifest.xml: <uses-permission android:name="android.permission.internet" /> Then you can use the standard java.net.url class to connect to a file or page at a given URL and read its data: URL url = new URL("http://foobar.com/example.txt"); Scanner scan = new Scanner(url.openStream()); while (scan.hasnextline()) { String line = scan.nextline();... }