java programming istream

Upload: danniel-irvin-cajuban

Post on 02-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 Java Programming Istream

    1/14

    SOFTWARE ENGINEERINStandard Input

    File Handling

  • 7/27/2019 Java Programming Istream

    2/14

    READING FROM STANDARD

    Instead of getting user input from the command-line, most users prefer idata when prompted by the program while it is already in execution. Oding this is with the use of streams. A stream is an abstraction of a file or that allows a series of items to be read or written. Streams are connectedevices such as keyboards, consoles and files. There are two general kinstreams, byte streams and character streams. Byte streams are for binarcharacter streams are for Unicode characters. System.in and System.ou

    examples of predefined byte streams in java. The first one by default refkeyboard and the latter refers to console.

    To read characters from the key keyboard, you can use the System.in bwarped in a BufferedReader object. The following line shows how to do

  • 7/27/2019 Java Programming Istream

    3/14

    READING FROM STANDARD

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

    The read method of the BufferedReader object is then used to reinput device.

    ch = (int) br.read(); //read method returns an integer

  • 7/27/2019 Java Programming Istream

    4/14

    READING FROM STANDARD

    Try out this example: import java.io.*;

    Class FavoriteCharacter{ Public static void main(String args[])throws IOException{

    System.out.println(Hi, what is your favorite character? );

    Char favChar;

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in

    favChar = (char) br.read();

    System.out.println(favChar+ is a good character!);

    }

    }

  • 7/27/2019 Java Programming Istream

    5/14

  • 7/27/2019 Java Programming Istream

    6/14

    READING FROM STANDARD

    If you prefer reading an entire line instead of reading one charactime, you can use the readLine method.

    e.g. str = br.readline();

  • 7/27/2019 Java Programming Istream

    7/14

    READING FROM STANDARD

    Here is a program almost similar to the preceding example but reentire string instead of just one character:

    import java.io.*;

    Class FavoriteCharacter{ Public static void main(String args[]) throws IOException{

    System.out.println(Hi, what is your name? );

    String name;

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in

    name = br.readLine();

    System.out.println(Nice to meet you + name + !);

    }

    }

  • 7/27/2019 Java Programming Istream

    8/14

    FILE HAN

    In some cases, data inputs are stored in files. Moreover, there areinstances when we want to store the output of a certain programa computerized enrollment system, the student data that may beinput to the system is most probably stored in a particular file. Thepossible output of the system is the information about the subjectby the students. Again, the output in this case may preferably be

    file. As seen in this application, there is a need for reading from awriting to a file. You will learn about file input and output n this se

  • 7/27/2019 Java Programming Istream

    9/14

    FILE HAN

    Reading from a file

    To read from a file, you can use the FileInputStream class. Here is constructors of this class.

    FileInputStream(String filename)

    The constructor creates a connection to an actual file whose filespecified as an argument. A FileNotFoundException is thrown whdoes not exist or it cannot be opened

  • 7/27/2019 Java Programming Istream

    10/14

    FILE HAN

    After creating an input stream, you can now use to read from thefile using the read method. The read method returns an integer a-1 when the end of the file is reached.

  • 7/27/2019 Java Programming Istream

    11/14

    FILE HANimport java.io.*;public class ReadFile {

    public static void main(String args[])throws

    IOException{

    System.out.println("What is the name of the fileto read from?");

    String filename;BufferedReader br = new BufferedReader(new

    InputStreamReader(System.in));

    filename = br.readLine();

    System.out.println("Now reading from " +filename + "...");

    FileInputStream fis = null;try{

    fis = new FileInputStream(filename);

    }catch (FileNotFoundException ex){" "

    }

    try{

    char data;int temp;do{

    temp = fis.read();data = (char) temp;if (temp != 1)

    System.out.print(data);}while (temp != -1);

    }catch (IOException ex){

    System.out.println("Problefrom the file.");

    }

    }}

  • 7/27/2019 Java Programming Istream

    12/14

    FILE HAN

    Assuming test.txt exist and it contains the text temporary file, hereoutput of this program.

    What is the name of the file to read from?

    test.txt

    Now reading from temp.txt

    temporary file

  • 7/27/2019 Java Programming Istream

    13/14

    FILE HAN

    Writing to a file

    For writing to a file, you can use the FileOutputStream class. Here is one constructors you can use.

    FileOutputStream(String filename)

    The constructor links an output stream to an actual file to write to. AFileNotFoundException is thrown when the file cannot be opened for w

    Once the output stream is created, you can now use the stream to writlinked file using the write method. The method has the following signatu

    void write(int b)

  • 7/27/2019 Java Programming Istream

    14/14

    FILE HANpackage sd1a;

    import java.io.*;

    public class WriteFile {public static void main(String args[])

    throws IOException{

    System.out.println("What is the nameof the file to be written to?");

    String filename;BufferedReader br = new

    BufferedReader(newInputStreamReader(System.in));

    filename = br.readLine();System.out.println("Enter data to

    write to " + filename +"...");System.out.println("Type q$ to end.");

    FileOutputStream fos = null;

    try{fos = new

    FileOutputStream(filename);}catch(FileNotFoundException ex){

    System.out.println("Fiile cannotbe opened for writing.");

    }try{

    boolean done = false;int data;

    do{

    data = br.read();if((char) data == 'q'){

    data = br.read();if((char)data == '$'){

    done = true;}

    else{fos.write

    fos.write(}

    }else{fos.write(da

    }}while(!done);

    }catch(IOExceptio{

    System.out.printreading from the file."

    }

    }}