Java Programming Mehdi Ebady Manaa 3rd class – Department of Network College of IT- University of Babylon 1. Command-Line Arguments Any Java technology application can use command-line arguments. • These string arguments are placed on the command line to launch the Java interpreter after the class name: public class TestArgs { public static void main (String [] args){ for (int i=0; i< args.length; i++) System.out.println("args ["+i+"]is :" +args[i]); } } • Each command-line argument is placed in the args array that is passed to the static main method as below : public class CallTest { public static void main(String[] args) { String args1[]={"seeyou","ali","mohammed"}; TestArgs xx= new TestArgs(); xx.main(args1); } } 2. I/O Stream Fundamentals A stream is a flow of data from a source or to a sink A source stream initiates the flow of data, also called an input stream. A sink stream terminates the flow of data, also called an output stream. Sources and sinks are also called Input streams and output streams Types of node streams are files, memory, and pipes between threads or processes. Table 1: Fundamental Stream Classes Page 1 Date: Sunday, December 01, 2013 Java Programming Mehdi Ebady Manaa 3rd class – Department of Network College of IT- University of Babylon 3. Data Within Streams Java technology supports two types of streams: character and byte. Input and output of character data is handled (implemented) by subclass readers and writers. Input and output of byte data is handled (implemented) by subclass inputStream and OutputStream 3.1. Byte Streams The following sections describe the fundamental byte stream 3.1.1 The InputStream Methods The following three methods provide access to the data from the input stream: The three basic read methods are: int read() // return an int from input stream, which contains either a byte read from the stream or a -1 int read(byte[] buffer) // read the stream into a byte array and return the number of bytes read int read(byte[] buffer, int offset, int length) // read the stream into a byte array and return the number of bytes read , the two int arguments in the third method indicate a sub range in the target array that needs to be filled Other methods include: void close() //it closes the stream long skip(long n) //this method discards the specified number of bytes from the stream. 3.1.2 The OutputStream Methods These methods write to the output of stream: The three basic write methods are: void write(int c)// void write(byte[] buffer) void write(byte[] buffer, int offset, int length) • Other methods include: void close() Page 2 Date: Sunday, December 01, 2013 Java Programming Mehdi Ebady Manaa 3rd class – Department of Network College of IT- University of Babylon void flush()// output stream uses it to force write when the write accumulate before committing Example1: create inputstream and write it to file test.txt import java.io.FileInputStream; import java.io.InputStream; public class InputStreamDemo { public static void main(String[] args) throws Exception { InputStream x = null; int i; char c; try{ // new input stream created x = new FileInputStream("C:\\test.txt"); System.out.println("Characters printed:"); // reads till the end of the stream while((i=x.read())!=-1) { // converts integer to character c=(char)i; // prints character System.out.print(c); } }catch(Exception e){ // if any I/O error occurs e.printStackTrace(); }finally{ // releases system resources associated with this stream if(x!=null) x.close(); } } } Example 2: create buffer of byte to read from input file stream import import import import Page 3 java.io.BufferedReader; java.io.FileInputStream; java.io.InputStream; java.io.InputStreamReader; Date: Sunday, December 01, 2013 Java Programming Mehdi Ebady Manaa 3rd class – Department of Network College of IT- University of Babylon public class BufferedReaderDemo { public static void main(String[] args) throws Exception { InputStream is = null; InputStreamReader isr = null; BufferedReader br = null; try{ // open input stream test.txt for reading purpose. is = new FileInputStream("c:/test.txt"); // create new input stream reader isr = new InputStreamReader(is); // create new buffered reader br = new BufferedReader(isr); // creates buffer char[] cbuf = new char[is.available()];// is.available to check the next invoking of byte // reads characters to buffer. br.read(cbuf); // for each character in the buffer for (char c:cbuf) { // if char is empty if(c == (char)0) { c='&'; } // prints characters System.out.print(c); } }catch(Exception e){ e.printStackTrace(); }finally{ // releases resources associated with the streams if(is!=null) is.close(); if(isr!=null) isr.close(); if(br!=null) br.close(); } } } Page 4 Date: Sunday, December 01, 2013 Java Programming Mehdi Ebady Manaa 3rd class – Department of Network College of IT- University of Babylon Example 3: read and write in input and output stream import java.io.*; public class OutputStreamDemo { public static void main(String[] args) { byte[] b = {'h', 'e', 'l', 'l', 'o'}; try { // create a new output stream OutputStream os = new FileOutputStream("c:/test.txt"); // craete a new input stream InputStream is = new FileInputStream("c:/test.txt"); // write something os.write(b); // read what we wrote for (int i = 0; i < b.length; i++) { System.out.print("" + (char) is.read()); } } catch (Exception ex) { ex.printStackTrace(); } } } Page 5 Date: Sunday, December 01, 2013