Data Storage: Part 2 (File System). Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal.

Slides:



Advertisements
Similar presentations
Lecture 15: I/O and Parsing
Advertisements

Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.
Android og persistens
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
James Tam Exception handling in Java Java Exception Handling Dealing with errors using Java’s exception handling mechanism.
Single Application Persistent Data Storage.  Files  SharedPreferences  SQLite database.
Java Review 2. The Agenda The following topics were highlighted to me as issues: –File IO (Rem) –Wrappers (Rem) –Interfaces (Rem) –Asymptotic Notation.
Debugging Android Applications
Slides prepared by Rose Williams, Binghamton University Chapter 10 File I/O.
Android Development: File System Approaches Richard S. Stansbury 2015.
Reading/Writing Files, Webpages CS2110, Recitation 10 1.
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
Performance measurements for inter-process communication.
Data Persistence in Android
Data Storage: Part 1 (Preferences)
Streams and File I/O Chapter 14. I/O Overview I/O = Input/Output In this context it is input to and output from programs Input can be from keyboard or.
CS0007: Introduction to Computer Programming File IO and Recursion.
CSE 486/586, Spring 2013 CSE 486/586 Distributed Systems Content Providers & Services.
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.
1 Chapter 11 – Files and Streams 1 Introduction What are files? –Long-term storage of large amounts of data –Persistent data exists after termination of.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
CSE 486/586, Spring 2012 CSE 486/586 Distributed Systems Recitation.
CSC 213 – Large Scale Programming. Project #1 Recap.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Program data (instance variables, local variables, and parameters) is transient, because its lifetime ends with the program...if not, before. Sometimes.
SE-1020 Dr. Mark L. Hornick 1 File Input and Output.
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.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
By Rachel Thompson and Michael Deck.  Java.io- a package for input and output  File I/O  Reads data into and out of the console  Writes and reads.
Introduction to Java Files. Text Files A sequential collection of data stored on a permanent storage device Hard drive USB memory CD/DVD Has a name and.
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.
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
Lecture 5 I/O and Parsing
Chapter 10 Text Files Section 10.2 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Reading/Writing Files, Webpages CS2110, Recitation 8 1.
Chapter 9 1 Chapter 9 – Part 2 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Files and Serialization. Files Used to transfer data to and from secondary storage.
1 Text File Input and Output. Objectives You will be able to Write text files from your Java programs. Read text files in your Java programs. 2.
CS 116 Object Oriented Programming II Lecture 11 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Data Storage in Android Димитър Н. Димитров. Why talk about data? Why not 3D graphics or network connectivity? Data as fundamental term in computer science.
Data Persistence Chapter 9. Objectives Learn about data storage methods Understand use of Shared Preferences Understand file-based storage and the differences.
Sequential files creation & writing
CS499 – Mobile Application Development
Permissions.
COMP Streams and File I/O
Data Storage: Part 2 (File System)
CS371m - Mobile Computing Persistence.
Android Application Data Storage 1.
CMSC 202 Text File I/O.
Lesson 8: More File I/O February 5, 2008
Mobile Software Development for Android - I397
Android – Read/Write to External Storage
CHAPTER 5 JAVA FILE INPUT/OUTPUT
I/O Basics.
Creating and Modifying Text part 2
Mobile Computing With Android ACST 4550 Android Data Storage
CSS161: Fundamentals of Computing
File class File myFile=new File(“c:/javaDemo/aa
Streams and File I/O Chapter 14.
CSS 161: Fundamentals of Computing
Unit 6 Working with files. Unit 6 Working with files.
18 File i/o, Parsing.
CMPE419 Mobile Application Development
EEC 484/584 Computer Networks
Exceptions and Exception Handling
Streams A stream is an object that enables the flow of data between a program and some I/O device or file If the data flows into a program, then the stream.
Presentation transcript:

Data Storage: Part 2 (File System)

Internal Storage versus External Storage Internal storage − for private data –By default, files saved to the internal storage are private to your application and other applications cannot access them. –When the user uninstalls an application, files and directories created by that application in internal storage are removed. External storage − for public shared data –Every Android-compatible device supports a shared “external storage” that can be used to save files. –External storage can be a removable storage media (such as an SD card) an internal (non-removable) storage. –Files saved to the external storage are world-readable and can be modified by the user when they enable USB mass storage to transfer files on a computer. Slide 2©SoftMoore Consulting

Using Java I/O for Data Storage The standard Java I/O capabilities provided in package java.io can be used to read and write data files on an Android device’s internal and external storage. Android provides several helper methods inherited from android.content.Context that can be used access files on the device. Application files are saved in the folder /data/data/ /files DDMS can be used to explore a device’s file system and to transfer files between a computer and an Android device. Slide 3©SoftMoore Consulting Access to the internal storage of a “real” device is restricted.

Helper Methods Inherited From android.content.Context FileInputStream openFileInput(String name) –Open a private file for reading. FileOutputStream openFileOutput(String name, int mode) –Open a private file for writing or appending. File getFilesDir() –Get the absolute path to the file system directory where your internal files are saved (i.e., returns a File object for /data/data/ /files/ ). File getDir(String name, int mode) –Create (or open an existing) directory within your internal storage space. Slide 4©SoftMoore Consulting

Helper Methods Inherited From android.content.Context (continued) boolean deleteFile(String name) –Delete the given private file associated with this application. –Returns true if the file was successfully deleted; otherwise returns false. String[] fileList() –Returns an array of strings naming the private files associated with this application. Slide 5©SoftMoore Consulting

File Modes Selected file modes from android.content.Context MODE_APPEND –If the file already exists, then write data to the end of the existing file instead of erasing it. MODE_PRIVATE –Created file can only be accessed by the calling application (default mode). MODE_WORLD_READABLE (deprecated in API level 17) –Allow all other applications read access to the created file. MODE_WORLD_WRITEABLE (deprecated in API level 17) –Allow all other applications write access to the created file. Slide 6©SoftMoore Consulting

Example: Reading from a Text File BufferedReader br = null; try { FileInputStream fis = openFileInput(FILE_NAME); br = new BufferedReader(new InputStreamReader(fis)); StringBuilder builder = new StringBuilder(200); String line = null; while((line = br.readLine()) != null) builder.append(line + '\n'); EditText editText = (EditText) findViewById(R.id.editText); editText.setText(builder.toString()); } Slide 7©SoftMoore Consulting

Example: Reading from a Text File (continued) catch (FileNotFoundException ex) { String errorMsg = FILE_NAME + " not found"; Log.e(LOG_TAG, errorMsg, ex); Toast toast = Toast.makeText(FileStorage.this, errorMsg, Toast.LENGTH_SHORT); toast.show(); } catch (IOException ex) {... // log and report error message } finally {... // if br != null, try to call br.close() // log and report error message } Slide 8©SoftMoore Consulting

Example: Writing to a Text File BufferedWriter bw = null; try { FileOutputStream fos = openFileOutput(FILE_NAME, MODE_PRIVATE); bw = new BufferedWriter(new OutputStreamWriter(fos)); EditText editText = (EditText) findViewById(R.id.editText); String text = editText.getText().toString(); bw.write(text, 0, text.length()); } Slide 9©SoftMoore Consulting

Example: Writing to a Text File (continued) catch (FileNotFoundException ex) { String errorMsg = FILE_NAME + " not found"; Log.e(LOG_TAG, errorMsg, ex); Toast toast = Toast.makeText(FileStorage.this, errorMsg, Toast.LENGTH_SHORT); toast.show(); } catch (IOException ex) {... // log and report error message } finally {... // if bw != null, try to call bw.close() // log and report error message } Slide 10©SoftMoore Consulting

Checking Media Availability for External Storage There is no security enforced upon files you save to the external storage. All applications can read and write files placed on the external storage and the user can remove them. Call getExternalStorageState() before using external storage to check whether the media is available. The media could be mounted to a computer, missing, read-only, or in some other state. Slide 11©SoftMoore Consulting

Example: Checking Media Availability boolean isExternalStorageAvailable = false; boolean isExternalStorageWriteable = false; String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { // We can both read and write. isExternalStorageAvailable = true; isExternalStorageWriteable = true; } else if (state.equals(Environment.MEDIA_MOUNTED_READ_ONLY) { // We can only read. isExternalStorageAvailable = true; isExternalStorageWriteable = false; } else // (continued on next slide) Slide 12©SoftMoore Consulting

Example: Checking Media Availability (continued) { // We can neither read nor write. isExternalStorageAvailable = false; isExternalStorageWriteable = false; } Slide 13©SoftMoore Consulting

Public Directories on External Storage Files saved in one of the public directories on external storage are not deleted when an application is uninstalled. Examples include the following Music/ - Media scanner classifies all media found here as user music. Ringtones/ - Media scanner classifies all media found here as a ringtone. Alarms/ - Media scanner classifies all media found here as an alarm sound. Pictures/ - All photos (excluding those taken with the camera). Movies/ - All movies (excluding those taken with the camcorder). Slide 14©SoftMoore Consulting

Accessing Files on External Storage For API Level 8 or greater –Use getExternalFilesDir() to open a File that represents the external storage directory where you should save your files. –Parameter specifies the type of subdirectory. DIRECTORY_MUSIC DIRECTORY_RINGTONES... null to get the root of your application’s file directory –Method will create the appropriate directory if necessary. API Level 7 or lower –Use getExternalStorageDirectory() to open a File representing the root of the external storage. –Write your data in the following directory: /Android/data/ /files/ Slide 15©SoftMoore Consulting

Relevant Links Data Storage Class Context The Java Tutorials: Basic I/O Slide 16©SoftMoore Consulting