Download presentation
Presentation is loading. Please wait.
Published byJames Weaver Modified over 9 years ago
1
PROG 38448 Mobile Java Application Development PROG 38448 Mobile Java Application Development Data Storage, Continued
2
1/6/2016Wendi Jollymore, ACES2 Review of FileConnection Can read data that other apps have written to file system A traditional file system javax.microedition.io.file package Use Connector class to create the actual connection
3
1/6/2016Wendi Jollymore, ACES3 Review Connector Class javax.microedition.io.Connector A factory class used to create connections File connections, network connections, etc.. Connector.open(string) opens a connection and returns a Connection object {scheme}:[{target}][{parms}] (see docs for a list of schemes) {scheme} - name of a protocol, such as http:// or file:// {target} - normally some kind of network address or resource Any {parms} are formed as a series of equates of the form ";x=y". i.e. ";type=a“
4
1/6/2016Wendi Jollymore, ACES4 Review File System It’s important to know the locations of the various storage locations: store/ device memory home - internal flash memory SDCard/ sd card home system/ internal sd card on Bold and Storm (transient data objects and runtime processes - 860MB)
5
1/6/2016Wendi Jollymore, ACES5 Review Creating Connection Exception is thrown if path is invalid Doesn’t have to exist - *could* exist Only the last item can be non-existent This allows you to create new files/directories try { FileConnection storeDirectory = FileConnection)Connector.open("file:///store/"); } catch (IOException e) { Dialog.alert(e.getMessage()); }
6
1/6/2016Wendi Jollymore, ACES6 Review FileConnection Methods FileConnection.close() From parent interface Connection Closes file connection FileConnection.isDirectory() True if the object points to a directory FileConnection.exists() True of the directory/file exists
7
1/6/2016Wendi Jollymore, ACES7 FileConnection Methods FileConnection.create() Creates a file corresponding to the string provided in the Connection.open() method Created with 0 length Does not create directories in the path Throws IOException Use output streams to write data to the file
8
1/6/2016Wendi Jollymore, ACES8 FileConnection Methods FileConnection.mkdir() Creates a directory corresponding to the string provided in the Connection.open() method Not recursive – you must create all new dirs in a path if they don’t exist throws IOException
9
1/6/2016Wendi Jollymore, ACES9 Working with Dirs and Files If you have a file that doesn’t exist in a directory that doesn’t exist You have to create the directory first: FileConnection fileConn = (FileConnection) Connector.open(aPath); if (!fileConn.exists()) { fileConn.mkdir(); }
10
1/6/2016Wendi Jollymore, ACES10 Working with Dirs and Files Then you have to reset the file connection: Then you have to create a connection to the file and create it: fileConn.close(); fileConn = null; fileConn = (FileConnection) Connection.open(fileName); if (!fileConn.exists()) { fileConn.create(); }
11
1/6/2016Wendi Jollymore, ACES11 Working with Dirs and Files If your directory and file already exist, there’s a shorter way: This still allows you to use the same FileConnection object But it only works when every part of the path/file exists Throws an IOException fileConn.setFileConnection(fileName);
12
1/6/2016Wendi Jollymore, ACES12 Working with Streams To read and write data, you need an input or output stream We use binary steams only more efficient can use regular InputStream or OutputStream DataInputStream or DataOutputStream a bit more convenient you can read/write data types and strings (sort of)
13
1/6/2016Wendi Jollymore, ACES13 Working with Streams FileConnection.openInputStream(); Opens and returns an InputStream object Connected to the file specified when the connection was created The file must exist, must be accessible FileConnection.openDataInputStream(); Opens and returns a DataInputStream object Same as above Both throw IOException
14
1/6/2016Wendi Jollymore, ACES14 Working with Streams FileConnection.openOutputStream(); Opens and returns an OutputStream object File pointer positioned at start of file The file must exist, must be accessible openOutputStream(long offset) will start at byte offset FileConnection.openDataOutputStream(); Opens and returns a DataOutputTream object Same as above but can’t use offset All throw IOException
15
1/6/2016Wendi Jollymore, ACES15 Working with Streams Opening streams from the file connecton object: When you are done with a stream: Close it using close() method Assign null to stream object DataInputStream fileIn = fileConn.openDataInputStream(); DataOutputStream fileOut = fileConn.openDataOutputStream();
16
1/6/2016Wendi Jollymore, ACES16 Working with Streams Look up DataInputStream and DataOutputStream in the api docsDataInputStream DataOutputStream Note the methods to read and write data to the file Everything must be a fixed size E.g. ints are always 4 bytes, doubles are 8 bytes, Strings are 2 bytes per character Remind you of Random Access Files?
17
1/6/2016Wendi Jollymore, ACES17 Working with Streams Strings are the ones you need to pay most attention to. When storing data as “records”, all records must be exact same size So you know where to find record x All strings must be fixed width Define how many characters your string fields are Make sure they’re saved as that exact size
18
1/6/2016Wendi Jollymore, ACES18 Working with Streams Example: Field size is 25 Field is either truncated or padded to be the exact size of 25 chars int currSize = str.length(); if (currSize > size) str = str.substring(0, size); else if (currSize < size) { String temp = str; for (int i=currSize; i<=size; i++) str += “ “; }
19
1/6/2016Wendi Jollymore, ACES19 Working with Streams Example: reading back the same data Read individual chars until you build the string field Must be the correct number of chars! String temp = “”; for (int i=0; i<size; i++) { String c = String.valueOf(fileIn.readChar()); temp += c; }
20
Exercise New Program Add three EditFields One to enter an int, one for a double, and one for a String Add buttons Save, Clear, Read Add event handling stuff for buttons Add a display label Save: saves the 3 values Info: clears txt fields Read: shows some saved values 1/6/2016Wendi Jollymore, ACES20
21
Exercise We’ll set the string to 25 chars A whole record will be 62 bytes 4 for the int, 8 for the double 25 * 2 for the string We’ll need methods: Prep the string for writing Read the string from a stream Add the same loadFile() method Display file size in label, too 1/6/2016Wendi Jollymore, ACES21
22
Exercise Saving Data method: fileDataOut() Use the file connection to create a DataOutputStream writeInt(), writeDouble() Prep the string value and writeChars() Close stream and set to null 1/6/2016Wendi Jollymore, ACES22
23
Exercise Reading Data method: fileDataIn() Calc number of records File size / rec size If the file connection is readable and we have records: Create a DataInputStream For each record: readInt(), readDouble(), your readString() method Build an output string Close stream and set to null Display output from file in label 1/6/2016Wendi Jollymore, ACES23
24
1/6/2016Wendi Jollymore, ACES24 Exercise When program exits, you must: Close file connection Assign null to file connection object Override the close() method: if (fileConn != null && fileConn.isOpen()) { fileConn.close(); fileConn = null; }
25
1/6/2016Wendi Jollymore, ACES25 Simulator Notes If you want to start from scratch each time you debug: Erase File System option Be careful doing this if you’re using SDCard option!!
26
1/6/2016Wendi Jollymore, ACES26 File Issues What happens when you run the app multiple times? Add new data each time What happens to your old data? You can’t append with DataOutputStream You must use OutputStream Set the offset to the file size
27
1/6/2016Wendi Jollymore, ACES27 File Issues Example: long size = fileConn.fileSize(); OutputStream fileOut = fileConn.openOutputStream(size); Also: OutputStream works with data differently There are no methods like writeInt() You have to convert ints and doubles to bytes!
28
1/6/2016Wendi Jollymore, ACES28 File Issues Converting ints and doubles to bytes: I gave you some code in the notes Note that strings are no longer 2 bytes per character They’re one byte each You’ll need to adjust your read method You’ll need to adjust any size constants String.getBytes() returns byte[]
29
1/6/2016Wendi Jollymore, ACES29 Exercise Edit your program so that you can append records to the end of the file String.getBytes() will give you the bytes for the String value outputStream.write(byte[]) will write an array of bytes dataInputStream.readFully(byte[]) can be used to read your string data back Then String s = new String(bytes);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.