Persistence in Java

Download Report

Transcript Persistence in Java

Persistence in Java:
File I/O, XML and JAR files
Lynsay A. Shepherd
What will this lecture cover?
• providing a basic overview
– introduction to persistence
– simple file I/O
– XML and persistence
– generating executable JAR files
• plenty of scope for you to explore these topics
further
Introduction
• what is persistence?
– various methods of storing data
– data which outlives the process that created it.
– without persistence, data related to programs
would only exist in RAM
• we won't cover the Java Persistence API
– Java Persistence Units
– feel free to read up on this if you wish…
Introduction
• previously did the TiledMap case study
• this time, focus is on a Music Library
– familiar concept
– most will have used one
Introduction
• what will the case study cover?
– file I/O
– XML
– serialization
– interacting with databases
File I/O
• Begin with some simple file I/O
– some of you may have done this last semester
with the Space Invaders game
– high score
• get some data, write it to a file
• read data in from a file
• basic persistence
Basic Example
public class testFileIO {
public static void main(String args[]) throws
IOException
{
FileInputStream in = null;
...
try {
in = new FileInputStream("test.txt");
...
Issues
•
•
•
•
•
•
•
check if the file you want to write to exists
overwrite file completely?
append new data to the file?
hard code the location of the file?
allow user to choose where to save the file?
different types of streams
tasks for the practical
What is XML?
•
•
•
•
eXtensible Markup Language
text based data
designed to describe data.
a way to define data that can be passed from
one application to another in text format
• XML technology allows split of data and
processing
What is XML?
• does not seek to replace HTML
• is often used in web applications
– specific variations like KML for Google Earth
– RSS feeds
– bank transactions
• variations like CML (chemical mark-up
language)
– describes molecules
What does XML look like?
<musiclibrary>
<artist id="1">
<artistname>Bryan Adams</artistname>
<album>
<albumname>Reckless</albumname>
<year>1984</year>
<genre>Rock</genre>
</album>
</artist>
<artist id="2">
<artistname>Bruce Springsteen</artistname>
<album>
<albumname>Born To Run</albumname>
<year>1975</year>
<genre>Rock</genre>
</album>
</artist>
</musiclibrary>
Writing XML
• no pre-defined tags
• tags can have attributes e.g. <font
color=”red”>
• <!-- comment format -->
• must be "well formed” like HTML 5
• tags can start with any letter, an underscore or
a hyphen
• special chars must be escaped
– less than, greater than, ampersand, single and
double quotes
Java and XML
• how can we read and write XML with Java?
• why are we using XML?
– can “transformed” into other formats e.g.
websites
• lots of useful libraries in Java which aid in the
creation of XML files
Generating XML from Java
import
import
import
import
import
import
import
javax.xml.parsers.*;
javax.xml.transform.*;
javax.xml.transform.dom.DOMSource;
javax.xml.transform.stream.StreamResult;
org.w3c.dom.Attr;
org.w3c.dom.Document;
org.w3c.dom.Element;
public class generateSimpleXML {
//root element
Element rootElement = doc.createElement("company");
doc.appendChild(rootElement);
//additional elements
//make XML file
Reading and XML file
• what is required?
– read in XML file (file I/O)
– get root element
– loop through nested elements
– print details to screen
//small code hint for practical session
System.out.println("First Name : " +
eElement.getElementsByTagName("artistname").item(0).get
TextContent());
Creating JAR files
• select which files you want to include in your jar file
• make file called manifest.txt containing the following• Main-Class: mainGenerateSimpleXML
• must have an empty blank line underneath this
• in the terminal/command prompt type: jar cvfm
jarName.jar manifest.txt oneFile.class mainFile.class
• will explain fully in the practical- different flags used
Further Reading
• http://docs.oracle.com/javase/tutorial/deploy
ment/jar/build.html
• http://docs.oracle.com/javase/tutorial/jaxp/in
tro/package.html
• http://docs.oracle.com/javase/tutorial/essenti
al/io/