File Management Android Club 2015. Agenda Working with files Networking XML JSON.
-
Upload
arnold-lang -
Category
Documents
-
view
227 -
download
0
Transcript of File Management Android Club 2015. Agenda Working with files Networking XML JSON.
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");
Copy file: stream
FileInputStream in = new FileInputStream(file1);FileOutputStream out = new FileOutputStream(file2);
Copy file: example
byte[] buffer = new byte[1024];int len;
while ((len = in.read(buffer))>0) {out.write(buffer, 0, len);
}
Practice: step 1/4
• Import JPEG file into project• Create reference to this JPEG file• Create reference to the file which will
be created
Practice: step 2/4
• Create input stream• Create output stream
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);
}
Practice: step 4/4
• Print “File was copied”• Surround our code with try-catch
block• Run
How to get file from computer?
File file = new File(System.getProperty("user.home") + \\Desktop\\+”favorite.mp3”)
Example
String path = System.getProperty("user.home") + "\\Desktop\\";
File file1 = new File(path + "1.mp3");File file2 = new File(path + "2.mp3");
Let’s practice
• Put big (1GB+) file to your desktop• Copy that file
Easy way
• Files.copy(source.toPath(), dest.toPath());
Easy way: practice
• Copy your file with Files class
Networking: step 1
• URL url = new URL("http://www.w3schools.com/xml/simple.xml");
Networking: step 2
InputStream in = url.openStream();
BufferedInputStream buf = new BufferedInputStream(in);
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);
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
XML parsing
• XML – extensive markup language
XML: example
<?xml version="1.0" encoding="UTF-8" ?><countries> <country> <name>Uzbekistan</name> <capital>Tashkent</capital> <currency>Sum</ currency>Sum > </country></countries>
XML: practice
• Add following countries:• Russia – Moscow – Ruble• China – Beijing – Yuan• Japan – Tokyo – Yen• US – Washington – US Dollar• Germany – Berlin - Euro
DOM: example
• DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
• DocumentBuilder builder = factory.newDocumentBuilder();
• Document doc = builder.parse("http://www.w3schools.com/xml/simple.xml");
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);• }
DOM: Practice
• Get all countries list• Print all countries name• http://joerichard.net/api/ac/
countries.xml
Questions?
• Any questions
Next lesson
• Game: Tic Tac Toe
Thank you
• Thank you very much for your attention!