Download presentation
Presentation is loading. Please wait.
Published byArnold Lang Modified over 9 years ago
1
File Management Android Club 2015
2
Agenda Working with files Networking XML JSON
3
Copy file 1: File File file1 = new File("melon.txt"); File file2 = new File("apple.txt");
4
Copy file: stream FileInputStream in = new FileInputStream(file1); FileOutputStream out = new FileOutputStream(file2);
5
Copy file: example byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer))>0) { out.write(buffer, 0, len); }
6
Practice: step 1/4 Import JPEG file into project Create reference to this JPEG file Create reference to the file which will be created
7
Practice: step 2/4 Create input stream Create output stream
8
Practice: step 3/4 Create empty byte array: buffer Size: 2048 Create int variable: length while ((length = in.read(buffer))>0) { out.write(buffer, 0, length); }
9
Practice: step 4/4 Print “File was copied” Surround our code with try-catch block Run
10
How to get file from computer? File file = new File( System.getProperty("user.home") + \\Desktop\\+”favorite.mp3” )
11
Example String path = System.getProperty("user.home") + "\\Desktop\\"; File file1 = new File(path + "1.mp3"); File file2 = new File(path + "2.mp3");
12
Let’s practice Put big (1GB+) file to your desktop Copy that file
13
Easy way Files.copy(source.toPath(), dest.toPath());
14
Easy way: practice Copy your file with Files class
15
Networking: step 1 URL url = new URL("http://www.w3schools.com/xml/si mple.xml");
16
Networking: step 2 InputStream in = url.openStream(); BufferedInputStream buf = new BufferedInputStream(in);
17
Networking: step 3 StringBuilder sb = new StringBuilder(); while (true) { Int data = in.read(); if (data == -1) { break; } else { sb.append((char) data); } System.out.println(sb);
18
Networking: practice http://www.cbu.uz/section/rates/widget/xml 1)Create URL instance with this url 2)Create input stream and convert it to BufferedInputStream 3)Read stream to StringBuilder 4)Print
19
XML parsing XML – extensive markup language
20
XML: example Uzbekistan Tashkent Sum Sum >
21
XML: practice Add following countries: Russia – Moscow – Ruble China – Beijing – Yuan Japan – Tokyo – Yen US – Washington – US Dollar Germany – Berlin - Euro
22
DOM: example DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse("http://www.w3schools.com/x ml/simple.xml");
23
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); }
24
DOM: Practice Get all countries list Print all countries name http://joerichard.net/api/ac/countries.xml
25
Questions? Any questions
26
Next lesson Game: Tic Tac Toe
27
Thank you Thank you very much for your attention!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.