Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Java API, Exceptions and IO

5,809 views

Published on

  • Be the first to comment

Java API, Exceptions and IO

  1. 1. Java Class Libraries, Exceptions and IO Jussi Pohjolainen Tampere University of Applied Sciences
  2. 2. CLASS LIBRARIES
  3. 3. Class Libraries <ul><li>Programming would be very time consuming if everything would be implemented from the beginning. </li></ul><ul><li>Class Libraries: premade classes that the programmer can use. </li></ul><ul><li>Example: You don't implement GUI-elements by yourself, you use the given classes! </li></ul><ul><li>You don't have to understand how the classes have been implemented. You have to understand how to use them! </li></ul>
  4. 4. Java Application Programming Interface <ul><li>Documentation available on the Internet </li></ul><ul><ul><li>http://java.sun.com/javase/6/docs/api/ </li></ul></ul><ul><li>The Java API is divided into </li></ul><ul><ul><li>Packages </li></ul></ul><ul><ul><ul><li>Contains classes or other packages </li></ul></ul></ul><ul><ul><li>Classes </li></ul></ul><ul><ul><ul><li>Contains methods </li></ul></ul></ul><ul><ul><li>Methods </li></ul></ul>
  5. 5. Structure of the Java API java.io Math static int round(float a) java.lang java.applet String Integer static double sqrt(double a) static double sin(double a) Package Class Methods . . . . . . . . .
  6. 6. Importing <ul><li>If you want to import all classes from one package: </li></ul><ul><ul><li>import java.io.*; </li></ul></ul><ul><li>If you want to import only one class from the package: </li></ul><ul><ul><li>import java.io.SomeClass; </li></ul></ul><ul><li>Every Java – app imports automatically the package java.lang.*; </li></ul>
  7. 7. Example: Date <ul><li>import java.util.Date; </li></ul><ul><li>class App { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Date mydate = new Date(); </li></ul><ul><li>String now = mydate.toString(); </li></ul><ul><li>System.out.println(now); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  8. 8. Example Date - class <ul><li>Documentation: </li></ul><ul><ul><li>http://java.sun.com/javase/6/docs/api/java/util/Date.html </li></ul></ul>
  9. 9. Example: Math.random() <ul><li>// You really don't have to do this, since </li></ul><ul><li>// the Math class is in java.lang! </li></ul><ul><li>import java.lang.Math; </li></ul><ul><li>class App { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>double randomValue = Math.random(); </li></ul><ul><li>System.out.println(randomValue); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  10. 10. static? <ul><li>If method declaration contains static, call the method via Class name: </li></ul><ul><ul><li>Class.method(); </li></ul></ul><ul><ul><li>Math.random(); </li></ul></ul><ul><li>If method declaration does NOT contain static, create object and then call object's method </li></ul><ul><ul><li>Class object = new Class(); </li></ul></ul><ul><ul><li>object.method(); </li></ul></ul><ul><ul><li>Date mydate = new Date(); </li></ul></ul><ul><ul><li>mydate.toString(); </li></ul></ul>
  11. 13. Couple Packages <ul><li>Graphical user interface and events: </li></ul><ul><ul><li>java.awt </li></ul></ul><ul><ul><li>java.awt.event </li></ul></ul><ul><ul><li>javax.swing </li></ul></ul><ul><li>Input and output </li></ul><ul><ul><li>java.io </li></ul></ul><ul><li>Data Structures, Internationalization, Utility Classes </li></ul><ul><ul><li>java.util </li></ul></ul>
  12. 14. Examples <ul><li>java.lang.String </li></ul><ul><li>java.util.Vector </li></ul>
  13. 15. EXCEPTIONS
  14. 16. Exception Handling <ul><li>Exception is a situation where application fail during runtime. </li></ul><ul><li>You can handle these exceptions so you can for example give error messages to user. </li></ul>
  15. 17. Checked vs. Unchecked <ul><li>Checked exceptions </li></ul><ul><ul><li>You have to implement exception handling </li></ul></ul><ul><ul><li>Subclass of Exception </li></ul></ul><ul><li>Unchecked exceptions </li></ul><ul><ul><li>You may implement exception handling </li></ul></ul><ul><ul><li>Subclass of RuntimeException </li></ul></ul><ul><li>For example: when doing IO you MUST handle exceptions. When handling arrays you MAY handle exceptions </li></ul>
  16. 18. Unchecked Exception <ul><li>class App { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>double result = 1/0; </li></ul><ul><li>System.out.println(result); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>java App Exception in thread &quot;main&quot; java.lang.ArithmeticException: / by zero at App.main(App.java:3)
  17. 19. Using Exceptions <ul><li>class App { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>try { </li></ul><ul><li>double result = 1/0; </li></ul><ul><li>System.out.println(result); </li></ul><ul><li>} catch (ArithmeticException e) { </li></ul><ul><li>System.out.println(&quot;You cannot divide with zero!&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>java App You cannot divide with zero!
  18. 20. Using Exceptions <ul><li>try { </li></ul><ul><li>// something that can trigger an exception </li></ul><ul><li>} catch(ExceptionClass1 e) { </li></ul><ul><li>// If it was ExceptionClass1, go here </li></ul><ul><li>} catch(ExceptionClass2 e) { </li></ul><ul><li>// If it was ExceptionClass2, go here </li></ul><ul><li>} finally { </li></ul><ul><li>// Do this no matter what </li></ul><ul><li>} </li></ul>
  19. 21. Example of Exception Usage <ul><li>class App { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>try { </li></ul><ul><li>int a1 = Integer.parseInt(args[0]); </li></ul><ul><li>int a2 = Integer.parseInt(args[1]); </li></ul><ul><li>double result = a1 / a2; </li></ul><ul><li>System.out.println(result); </li></ul><ul><li>} catch (ArithmeticException e) { </li></ul><ul><li>System.out.println(&quot;You cannot divide with zero!&quot;); </li></ul><ul><li>} catch (NumberFormatException e) { </li></ul><ul><li>System.out.println(&quot;Please give integer numbers!&quot;); </li></ul><ul><li>} catch (ArrayIndexOutOfBoundsException e) { </li></ul><ul><li>System.out.println(&quot;Please give two integer numbers!&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  20. 22. Integer.parseInt? <ul><li>Package: java.lang </li></ul><ul><li>Class: Integer </li></ul><ul><li>Method: parseInt </li></ul>It may throw an exception!
  21. 23. NumberFormatException Inherites RuntimeException =>Unchecked Exception =>You MAY implement exception handling
  22. 24. Exception is the Base Class! <ul><li>class App { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>try { </li></ul><ul><li>int a1 = Integer.parseInt(args[0]); </li></ul><ul><li>int a2 = Integer.parseInt(args[1]); </li></ul><ul><li>double result = a1 / a2; </li></ul><ul><li>System.out.println(result); </li></ul><ul><li>} catch (Exception e) { </li></ul><ul><li>System.out.println(&quot;Whatever exception is, always come here&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  23. 25. <ul><li>class App { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>try { </li></ul><ul><li>int a1 = Integer.parseInt(args[0]); </li></ul><ul><li>int a2 = Integer.parseInt(args[1]); </li></ul><ul><li>double result = a1 / a2; </li></ul><ul><li>System.out.println(result); </li></ul><ul><li>} catch (Exception e) { </li></ul><ul><li>// Exception is class, e is an object. </li></ul><ul><li>String errormsg = e.toString(); </li></ul><ul><li>System.out.println(errormsg); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>> java App 4 0 java.lang.ArithmeticException: / by zero > java App 4 k java.lang.NumberFormatException: For input string: &quot;k&quot; > java App 4 java.lang.ArrayIndexOutOfBoundsException: 1
  24. 26. JAVA IO
  25. 27. Input and Output Streams Input Output Binary FileInputStream FileOutputStream Text FileReader FileWriter
  26. 28. Reading Text <ul><li>import java.io.*; </li></ul><ul><li>class App { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>try { </li></ul><ul><li>// We use int even though characters are read! </li></ul><ul><li>int character; </li></ul><ul><li>FileReader input = new FileReader(&quot;App.java&quot;); </li></ul><ul><li>// Let's read and print the rest </li></ul><ul><li>// input.read() returns -1 when the file is done! </li></ul><ul><li>while((character = input.read()) != -1) { </li></ul><ul><li>System.out.print( (char) character); </li></ul><ul><li>} </li></ul><ul><li>// close the stream </li></ul><ul><li>input.close(); </li></ul><ul><li>} catch(IOException e) { </li></ul><ul><li>System.out.println(&quot;Some problem reading the file&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  27. 29. Writing Text <ul><li>import java.io.*; </li></ul><ul><li>class App { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>try { </li></ul><ul><li>// We use int even though characters are read! </li></ul><ul><li>int character; </li></ul><ul><li>FileWriter output = new FileWriter(&quot;Test.txt&quot;); </li></ul><ul><li>do { </li></ul><ul><li>// Read from the user one char at a time </li></ul><ul><li>character = System.in.read(); </li></ul><ul><li>// Write the chars to the text file. </li></ul><ul><li>// Write until user gives 'q' </li></ul><ul><li>output.write(character); </li></ul><ul><li>} while( ( (char) character ) != 'q' ); </li></ul><ul><li>// close the stream </li></ul><ul><li>output.close(); </li></ul><ul><li>} catch(IOException e) { </li></ul><ul><li>System.out.println(&quot;Some problem reading the file&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  28. 30. Reading and Writing Binary <ul><li>import java.io.*; </li></ul><ul><li>public class App </li></ul><ul><li>{ </li></ul><ul><li>public static void main(String args[]) </li></ul><ul><li>{ </li></ul><ul><li>int oneByte; </li></ul><ul><li>try </li></ul><ul><li>{ </li></ul><ul><li>// For reading </li></ul><ul><li>FileInputStream input = new FileInputStream(&quot;calculator.exe&quot;); </li></ul><ul><li>// For writing </li></ul><ul><li>FileOutputStream output = new FileOutputStream(&quot;duplicatecalculator.exe&quot;); </li></ul><ul><li>// Read and write one byte at a time </li></ul><ul><li>while( (oneByte = input.read()) != -1) { </li></ul><ul><li>output.write(oneByte); </li></ul><ul><li>} </li></ul><ul><li>// close the streams </li></ul><ul><li>input.close(); </li></ul><ul><li>output.close(); </li></ul><ul><li>} catch(IOException e) { </li></ul><ul><li>System.out.println(&quot;Some problem with reading and writing&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  29. 31. Buffered Stream: BufferedReader <ul><li>import java.io.*; </li></ul><ul><li>class App { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>try { </li></ul><ul><li>String line = &quot;something&quot;; </li></ul><ul><li>FileReader input = new FileReader(&quot;App.java&quot;); </li></ul><ul><li>BufferedReader bf = new BufferedReader(input); </li></ul><ul><li>while((line = bf.readLine()) != null) { </li></ul><ul><li>System.out.println(line); </li></ul><ul><li>} </li></ul><ul><li>// close the streams </li></ul><ul><li>bf.close(); </li></ul><ul><li>input.close(); </li></ul><ul><li>} catch(IOException e) { </li></ul><ul><li>System.out.println(&quot;Some problem reading the file&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  30. 32. Buffered Stream: BufferedWriter <ul><li>import java.io.*; </li></ul><ul><li>class App { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>try { </li></ul><ul><li>FileWriter output = new FileWriter(&quot;Test.txt&quot;); </li></ul><ul><li>BufferedWriter bf = new BufferedWriter(output); </li></ul><ul><li>bf.write(&quot;Hello&quot;); </li></ul><ul><li>bf.close(); </li></ul><ul><li>output.close(); </li></ul><ul><li>} catch(IOException e) { </li></ul><ul><li>System.out.println(&quot;Some problem reading the file&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>

×