27
File Management Android Club 2015

File Management Android Club 2015. Agenda Working with files Networking XML JSON

Embed Size (px)

Citation preview

Page 1: File Management Android Club 2015. Agenda Working with files Networking XML JSON

File Management

Android Club 2015

Page 2: File Management Android Club 2015. Agenda Working with files Networking XML JSON

Agenda

• Working with files• Networking• XML• JSON

Page 3: File Management Android Club 2015. Agenda Working with files Networking XML JSON

Copy file 1: File

• File file1 = new File("melon.txt");• File file2 = new File("apple.txt");

Page 4: File Management Android Club 2015. Agenda Working with files Networking XML JSON

Copy file: stream

FileInputStream in = new FileInputStream(file1);FileOutputStream out = new FileOutputStream(file2);

Page 5: File Management Android Club 2015. Agenda Working with files Networking XML JSON

Copy file: example

byte[] buffer = new byte[1024];int len;

while ((len = in.read(buffer))>0) {out.write(buffer, 0, len);

}

Page 6: File Management Android Club 2015. Agenda Working with files Networking XML JSON

Practice: step 1/4

• Import JPEG file into project• Create reference to this JPEG file• Create reference to the file which will

be created

Page 7: File Management Android Club 2015. Agenda Working with files Networking XML JSON

Practice: step 2/4

• Create input stream• Create output stream

Page 8: File Management Android Club 2015. Agenda Working with files Networking XML JSON

Practice: step 3/4

• Create empty byte array: buffer• Size: 2048• Create int variable: lengthwhile ((length = in.read(buffer))>0) {

out.write(buffer, 0, length);

}

Page 9: File Management Android Club 2015. Agenda Working with files Networking XML JSON

Practice: step 4/4

• Print “File was copied”• Surround our code with try-catch

block• Run

Page 10: File Management Android Club 2015. Agenda Working with files Networking XML JSON

How to get file from computer?

File file = new File(System.getProperty("user.home") + \\Desktop\\+”favorite.mp3”)

Page 11: File Management Android Club 2015. Agenda Working with files Networking XML JSON

Example

String path = System.getProperty("user.home") + "\\Desktop\\";

File file1 = new File(path + "1.mp3");File file2 = new File(path + "2.mp3");

Page 12: File Management Android Club 2015. Agenda Working with files Networking XML JSON

Let’s practice

• Put big (1GB+) file to your desktop• Copy that file

Page 13: File Management Android Club 2015. Agenda Working with files Networking XML JSON

Easy way

• Files.copy(source.toPath(), dest.toPath());

Page 14: File Management Android Club 2015. Agenda Working with files Networking XML JSON

Easy way: practice

• Copy your file with Files class

Page 15: File Management Android Club 2015. Agenda Working with files Networking XML JSON

Networking: step 1

• URL url = new URL("http://www.w3schools.com/xml/simple.xml");

Page 16: File Management Android Club 2015. Agenda Working with files Networking XML JSON

Networking: step 2

InputStream in = url.openStream();

BufferedInputStream buf = new BufferedInputStream(in);

Page 17: File Management Android Club 2015. Agenda Working with files Networking XML JSON

Networking: step 3

StringBuilder sb = new StringBuilder();while (true) {I nt data = in.read();

if (data == -1) {break;} else {sb.append((char) data);}

}System.out.println(sb);

Page 18: File Management Android Club 2015. Agenda Working with files Networking XML JSON

Networking: practice

http://www.cbu.uz/section/rates/widget/xml1) Create URL instance with this url2) Create input stream and convert it to

BufferedInputStream3) Read stream to StringBuilder4) Print

Page 19: File Management Android Club 2015. Agenda Working with files Networking XML JSON

XML parsing

• XML – extensive markup language

Page 20: File Management Android Club 2015. Agenda Working with files Networking XML JSON

XML: example

<?xml version="1.0" encoding="UTF-8" ?><countries> <country> <name>Uzbekistan</name> <capital>Tashkent</capital> <currency>Sum</ currency>Sum > </country></countries>

Page 21: File Management Android Club 2015. Agenda Working with files Networking XML JSON

XML: practice

• Add following countries:• Russia – Moscow – Ruble• China – Beijing – Yuan• Japan – Tokyo – Yen• US – Washington – US Dollar• Germany – Berlin - Euro

Page 22: File Management Android Club 2015. Agenda Working with files Networking XML JSON

DOM: example

• DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

• DocumentBuilder builder = factory.newDocumentBuilder();

• Document doc = builder.parse("http://www.w3schools.com/xml/simple.xml");

Page 23: File Management Android Club 2015. Agenda Working with files Networking XML JSON

DOM: Example

• NodeList list = doc.getElementsByTagName("food");

• for (int i = 0; i < list.getLength(); i++) {Element element = (Element) list.item(i);String value =

element.getElementsByTagName("name").item(0).getTextContent();

System.out.println(value);• }

Page 24: File Management Android Club 2015. Agenda Working with files Networking XML JSON

DOM: Practice

• Get all countries list• Print all countries name• http://joerichard.net/api/ac/

countries.xml

Page 25: File Management Android Club 2015. Agenda Working with files Networking XML JSON

Questions?

• Any questions

Page 26: File Management Android Club 2015. Agenda Working with files Networking XML JSON

Next lesson

• Game: Tic Tac Toe

Page 27: File Management Android Club 2015. Agenda Working with files Networking XML JSON

Thank you

• Thank you very much for your attention!