meljun cortes jedi slides intro2 chapter05 text based applications

Upload: meljun-cortes-mbampa

Post on 05-Apr-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    1/32

    Introduction to Programming 2 1

    5 Text-Based Applications

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    2/32

    Introduction to Programming 2 2

    Topics

    Command-Line Arguments and System Properties

    Reading from Standard Input

    File Handling

    Reading from a File

    Writing to a File

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    3/32

    Introduction to Programming 2 3

    Command-Line Arguments

    Java allows user to input data from the command line

    Purpose of declaring String args[]as a parameter in the mainmethod

    When using thejava command, specifying data after the class nameindicates you are passing data via the args parameter

    Example:

    java Calculate 1 2

    args[0]has the value 1

    args[1]has the value 2

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    4/32

    Introduction to Programming 2 4

    Command-Line Arguments:Using NetBeans

    Right click on project icon/name on Projects panel Select Properties from the drop-down menu

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    5/32

    Introduction to Programming 2 5

    Command-Line Arguments:Using NetBeans

    Click on Run from Categories panel

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    6/32

    Introduction to Programming 2 6

    Command-Line Arguments:Using NetBeans

    Enter arguments in the Arguments field & click on the OKbutton

    Compile and run program as usual

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    7/32

    Introduction to Programming 2 7

    System Properties

    Java also also allows you to manipulate system propertiesfrom the command line

    System property Quite similar to environment variables

    But is not platform-dependent

    Property Mapping between the property name to its corresponding value

    Represented in Java with the Properties class.

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    8/32

    Introduction to Programming 2 8

    System Properties

    System class

    Provides a methods for determining the current system properties,the getProperties method that returns a Properties object

    Also provides the overloaded getPropertymethod

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    9/32

    Introduction to Programming 2 9

    System Properties

    Including a new property

    Use the -D option with thejava command

    java -D=value

    Example:java -Duser.home=philippines

    Display the list of system properties

    Use the getProperties methodSystem.getProperties().list(System.out);

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    10/32

    Introduction to Programming 2 10

    System Properties

    Truncated sample list of system properties

    -- listing properties --

    java.runtime.name=Java(TM) 2 Runtime Environment,Stand...

    sun.boot.library.path=C:\ProgramFiles\Java\jdk1.5.0_06\jre...

    java.vm.version=1.5.0_06-b05

    java.vm.vendor=Sun Microsystems Inc.

    java.vendor.url=http://java.sun.com/path.separator=;

    java.vm.name=Java HotSpot(TM) Client VM

    file.encoding.pkg=sun.io

    user.country=US

    ...

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    11/32

    Introduction to Programming 2 11

    Reading from Standard Input:

    Streams Can use streams to read from standard input

    Stream

    Abstraction of a file or a device that allows a series of items to beread or written

    Connected to physical devices

    Two general kinds of streams:

    Character streams

    Byte streams

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    12/32

    Introduction to Programming 2 12

    Reading from Standard Input:

    Streams Character Streams For Unicode characters

    Byte Streams For binary data

    Predefined examples

    System.in (keyboard by default)

    System.out(console by default)

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    13/32

    Introduction to Programming 2 13

    Reading from Standard Input:BufferedReader

    Reading characters from the keyboard

    Use the System.in byte stream warped in a BufferedReaderobject

    BufferedReader br = new BufferedReader(newInputStreamReader(System.in));

    Use readmethod of the BufferedReaderobject

    ch = (int) br.read();

    //read method returns an integer

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    14/32

    Introduction to Programming 2 14

    Reading from Standard Input:BufferedReaderExample

    1 import java.io.*;2 class FavoriteCharacter {

    3 public static void main(String args[])

    4 throws IOException {

    5

    System.out.println("Hi, what's your favorite6 character?");

    7 char favChar;

    8 BufferedReader br = new BufferedReader(new

    9 InputStreamReader(System.in));

    10 favChar = (char) br.read();

    11 System.out.println(favChar +

    12 " is a good choice!");

    13 }

    14 }

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    15/32

    Introduction to Programming 2 15

    Reading from Standard Input:BufferedReaderExample

    Output:

    Hi, what's your favorite character?

    a

    a is a good choice!

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    16/32

    Introduction to Programming 2 16

    Reading from Standard Input:BufferedReader

    Reading an entire line

    Use the System.in byte stream warped in a BufferedReaderobject

    BufferedReader br = new BufferedReader(newInputStreamReader(System.in));

    Use the readLine method

    str = br.readLine();

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    17/32

    Introduction to Programming 2 17

    Reading from Standard Input:BufferedReaderExample

    1 import java.io.*;

    2 class GreetUser {

    3 public static void main(String args[])

    4 throws IOException {

    5 System.out.println("Hi, what's your name?");

    6 String name;

    7 BufferedReader br = new BufferedReader(new

    8 InputStreamReader(System.in));

    9 name = br.readLine();

    1 System.out.println("Nice to meet you, " +

    2 name + "! :)");

    3 }

    4 }

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    18/32

    Introduction to Programming 2 18

    Reading from Standard Input:BufferedReaderExample

    Output:

    Hi, what's your name?rebecca

    Nice to meet you, rebecca! :)

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    19/32

    Introduction to Programming 2 19

    Reading from Standard Input:

    Reminders Don't forget to import thejava.io package as shown below:import java.io.*;

    Reading from streams may cause checked exceptions tooccur

    Handle these exceptions using try-catch statements

    Or handle by indicating the exception in the throws clause of themethod

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    20/32

    Introduction to Programming 2 20

    File Handling:

    Reading from a File Can use the FileInputStream class One of the constructors of this class

    FileInputStream(String filename)

    Creates a connection to an actual file whose filename is specified asan argument

    A FileNotFoundException is thrown when the file does not exist or itcannot be opened for reading

    Using the read method Returns an integer representation of data read

    Returns -1 when the end of the file is reached

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    21/32

    Introduction to Programming 2 21

    File Handling:Reading from a File

    1 import java.io.*;

    2 class ReadFile {

    3 public static void main(String args[])

    4 throws IOException {

    5 System.out.println("What is the name of the6 file to read from?");

    7 String filename;

    8 BufferedReader br = new BufferedReader(new

    9

    InputStreamReader(System.in));10 filename = br.readLine();

    11 System.out.println("Now reading from " +

    12 filename + "...");

    13 //continued...

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    22/32

    Introduction to Programming 2 22

    File Handling:Reading from a File

    14 FileInputStream fis = null;

    15 try {

    16 fis = new FileInputStream(filename);

    17 } catch (FileNotFoundException ex) {

    18 System.out.println("File not found.");19 }

    20 try {

    21 char data;

    22

    int temp;23 //continued...

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    23/32

    Introduction to Programming 2 23

    File Handling:Reading from a File

    Output:

    What is the name of the file to read from?

    temp.txt

    Now reading from temp.txt...

    temporary file

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    24/32

    Introduction to Programming 2 24

    File Handling:

    Writing to a File Can use the FileOutputStream class One of the constructors of this class

    FileOutputStream(String filename)

    Links an output stream to an actual file to write to A FileNotFoundException is thrown when the file cannot be opened

    for writing

    Using the write method

    void write(int b)

    where,

    b refers to the data to be written to the actual file

    File Handling:

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    25/32

    Introduction to Programming 2 25

    File Handling:Writing to a File

    1 import java.io.*;2 class WriteFile {

    3 public static void main(String args[])

    4 throws IOException {

    5 System.out.println("What is the name of the6 file to be written to?");

    7 String filename;

    8 BufferedReader br = new BufferedReader(new

    9 InputStreamReader(System.in));

    10 filename = br.readLine();

    11 System.out.println("Enter data to write to " +

    12 filename + "...");

    13 //continued...

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    26/32

    Introduction to Programming 2 26

    File Handling:Writing to a File

    14 System.out.println("Type q$ to end.");

    15 FileOutputStream fos = null;

    16 try {

    17 fos = new FileOutputStream(filename);

    18 } catch (FileNotFoundException ex) {

    19 System.out.println("File cannot be opened

    20 for writing.");

    21 }

    22 try {

    23 boolean done = false;

    24 int data;

    25 //continued...

    File Handling:

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    27/32

    Introduction to Programming 2 27

    File Handling:Writing to a File

    26 do {

    27 data = br.read();

    28 if ((char)data == 'q') {

    29 data = br.read();

    30 if ((char)data == '$') {31 done = true;

    32 } else {

    33 fos.write('q');

    34 fos.write(data);

    35 }

    36 } else {

    37 fos.write(data);

    38 //continued...

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    28/32

    Introduction to Programming 2 28

    File Handling:Writing to a File

    39 }

    40 } while (!done);

    41 } catch (IOException ex) {

    42 System.out.println("Problem in reading from

    43 the file.");

    44 }

    45 }

    46 }

    Fil H dli

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    29/32

    Introduction to Programming 2 29

    File Handling:Writing to a File

    Output:

    What is the name of the file to be writtento?

    temp.txt

    Enter data to write to temp.txt...

    Type q$ to end.

    what a wonderful world1, 2, step

    q$

    Fil H dli

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    30/32

    Introduction to Programming 2 30

    File Handling:Writing to a File

    Content oftemp.txtafter sample run

    what a wonderful world

    1, 2, step

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    31/32

    Introduction to Programming 2 31

    Summary

    Command-Line Arguments and System Properties

    Getting input from the command line

    Manipulating system properties

    java -D=

    Reading from Standard Input

    Use System.in

    Use BufferedReader Use readmethod

  • 7/31/2019 MELJUN CORTES JEDI Slides Intro2 Chapter05 Text Based Applications

    32/32

    Introduction to Programming 2 32

    Summary

    File Handling

    Reading from a File

    Use FileInputStream

    Use readmethod

    Writing to a File

    Use FileOutputStream

    Use write method