Persistence in Java: File I/O, XML and JAR files Lynsay A. Shepherd.

17
Persistence in Java: File I/O, XML and JAR files Lynsay A. Shepherd

Transcript of Persistence in Java: File I/O, XML and JAR files Lynsay A. Shepherd.

Page 1: Persistence in Java: File I/O, XML and JAR files Lynsay A. Shepherd.

Persistence in Java:File I/O, XML and JAR files

Lynsay A. Shepherd

Page 2: 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

Page 3: Persistence in Java: File I/O, XML and JAR files Lynsay A. Shepherd.

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…

Page 4: Persistence in Java: File I/O, XML and JAR files Lynsay A. Shepherd.

Introduction

• previously did the TiledMap case study

• this time, focus is on a Music Library– familiar concept

– most will have used one

Page 5: Persistence in Java: File I/O, XML and JAR files Lynsay A. Shepherd.

Introduction

• what will the case study cover?– file I/O

– XML

– serialization

– interacting with databases

Page 6: Persistence in Java: File I/O, XML and JAR files Lynsay A. Shepherd.

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

Page 7: Persistence in Java: File I/O, XML and JAR files Lynsay A. Shepherd.

Basic Example

public class testFileIO { public static void main(String args[]) throws IOException { FileInputStream in = null; ...

try { in = new FileInputStream("test.txt"); ...

Page 8: Persistence in Java: File I/O, XML and JAR files Lynsay A. Shepherd.

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

Page 9: Persistence in Java: File I/O, XML and JAR files Lynsay A. Shepherd.

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

Page 10: Persistence in Java: File I/O, XML and JAR files Lynsay A. Shepherd.

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

Page 11: Persistence in Java: File I/O, XML and JAR files Lynsay A. Shepherd.

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>

Page 12: Persistence in Java: File I/O, XML and JAR files Lynsay A. Shepherd.

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

Page 13: Persistence in Java: File I/O, XML and JAR files Lynsay A. Shepherd.

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

Page 14: Persistence in Java: File I/O, XML and JAR files Lynsay A. Shepherd.

Generating XML from Javaimport javax.xml.parsers.*;import javax.xml.transform.*;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import org.w3c.dom.Attr;import org.w3c.dom.Document;import org.w3c.dom.Element;

public class generateSimpleXML {

//root elementElement rootElement =

doc.createElement("company");doc.appendChild(rootElement);

//additional elements

//make XML file

Page 15: Persistence in Java: File I/O, XML and JAR files Lynsay A. Shepherd.

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 sessionSystem.out.println("First Name : " + eElement.getElementsByTagName("artistname").item(0).getTextContent());

Page 16: Persistence in Java: File I/O, XML and JAR files Lynsay A. Shepherd.

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