Java Programming class – Department of Network

advertisement
Java Programming
Mehdi Ebady Manaa
3rd class – Department of Network
College of IT- University of Babylon
Files in Java Programming
Section One
Reading data from InputStream using commandLines
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.
For example to read from console, we use the read() method as
below:-
Page 1
Monday, May 30, 2016
Java Programming
Mehdi Ebady Manaa
3rd class – Department of Network
College of IT- University of Babylon
I. System.in.Read()
Reads the next byte of data from the input stream. The value byte is
returned as an int in the range 0 to 255. If no byte is available because the
end of the stream has been reached, the value -1 is returned. This method
blocks until input data is available, the end of the stream is detected, or an
exception is thrown. A subclass must provide an implementation of this
method.
Returns:
the next byte of data, or -1 if the end of the stream is reached.
II. System.in. available ()
Returns an estimate of the number of bytes that can be read (or
skipped over) from this input stream without blocking by the next
invocation of a method for this input stream. The next invocation might
be the same thread or another thread. A single read or skip of this many
bytes will not block, but may read or skip fewer bytes.
Returns:
An estimate of the number of bytes that can be read (or skipped over)
from this input stream without blocking or 0 when it reaches the end of
the input stream.
import java.io.IOException;
public class InputStreamExample {
public static void main (String args[])
{
try {
System.out.println("Read the available data"+
System.in.available());
System.out.println("Read something from Console");
int n = System.in.read();
System.out.println("The available byte
are:"+System.in.available());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Page 2
Monday, May 30, 2016
Java Programming
Mehdi Ebady Manaa
3rd class – Department of Network
College of IT- University of Babylon
Example:
Read the available data0
Read something from Console
a
The available byte are:2
III.System.in. read (bytes): bytes is an
array of byte.
You can also choose to read a number of bytes in a byte array,
instead of reading just one byte, To do that you can use public int
read(byte[] b):
public class InputStreamExample {
public static void main(String[] args){
byte[] bytes = new byte[30];
try {
System.out.println("Available bytes :"+System.in.available());
System.out.print("Write something :");
int bytesread = System.in.read(bytes);
System.out.println("I've read :"+bytesread +" bytes from the
InputStream");
System.out.println(Arrays.toString(bytes));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}}
Example:
Write something :abcdef
I've read :8 bytes from the InputStream
[97, 98, 99, 100, 101, 102, 13, 10, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0]
V. Read read(byte[] b,int off, int len)
You can also choose to read a number of bytes and place them in
an arbitrary position in you buffer array, instead of filling up your
array. To do that you can use public int read(byte[] b, int off, int
len), where int off you specify the offset from the start of the
buffer that you want to start placing the read bytes, and len is the
number of bytes you wish to read from the stream.
Page 3
Monday, May 30, 2016
Java Programming
Mehdi Ebady Manaa
3rd class – Department of Network
College of IT- University of Babylon
import java.io.IOException;
import java.util.Arrays;
public class InputStreamExample {
public static void main(String[] args){
byte[] bytes = new byte[30];
try {
System.out.println("Available bytes
:"+System.in.available());
System.out.print("Write something :");
int bytesread = System.in.read(bytes,5,5);
System.out.println("I've read :"+bytesread +" bytes from
the InputStream");
System.out.println(Arrays.toString(bytes));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}}
Parameters:
b - the buffer into which the data is read.
off - the start offset in array b at which the data is written.
len - the maximum number of bytes to read.
Returns:
the total number of bytes read into the buffer, or -1 if there is no
more data because the end of the stream has been reached.
Throws:
IOException - If the first byte cannot be read for any reason other
than end of file, or if the input stream has been closed, or if some
other I/O error occurs.
NullPointerException - If b is null.
IndexOutOfBoundsException - If off is negative, len is negative, or
len is greater than b.length - off
Example:
Available bytes :0
Write something :javaprogramming
I've read :5 bytes from the InputStream
[0, 0, 0, 0, 0, 106, 97, 118, 97, 112, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0]
3. Reading characters form InputStream
When you’re dealing with binary data, it is usually fine to read
bytes from the input stream. But, as you might agree, reading bytes
is not always handy, especially when reading streams of characters,
like we did in the example. For that, Java offers
Page 4
Monday, May 30, 2016
Java Programming
Mehdi Ebady Manaa
3rd class – Department of Network
College of IT- University of Babylon
special Reader classes, that convert byte streams to character
streams. It does that by simply parsing the bytes and encoding
them according to character set encoding (you can do that on your
own, but don’t even bother). Such a Reader is InputStreamReader.
To create an InputStreamReader, you give it an InputStream as an
argument in its constructor, optionally along with a character set
(or else the default will be used to encode the characters).
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class InputStreamExample {
Public static void main(String[] args){
char[] characters = new char[30];
try {
InputStreamReader inputReader = new
InputStreamReader(System.in);
System.out.print("Write some characters :");
int bytesread = inputReader.read(characters);
System.out.println("I've read :"+bytesread +" characters from
the InputStreamReader");
System.out.println(Arrays.toString(characters));
} catch (IOException e) {
// TODO Auto-generated
catch block
e.printStackTrace();
}}}
So as you can see, I can now read characters instead of bytes. Of
course public int read(char[] cbuf, int offset, int length)method is
also available from the Reader that offers the same basic
functionality as we’ve described before in the case ofInpuStream.
Same goes for read(), but instead of reading one bytes, it reads one
character.
4. Using BufferedReader
You can also buffer a Reader, mainly for efficiency. But, you can
also take advantage of it when reading character streams, as you
can pack characters in Strings. Thus, you can read a text input
stream line by line.
Let’s see how :
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputStreamExample {
public static void main(String[] args){
try {
InputStreamReader inputReader = new
InputStreamReader(System.in);
BufferedReader buffReader = new
BufferedReader(inputReader);
Page 5
Monday, May 30, 2016
Java Programming
Mehdi Ebady Manaa
3rd class – Department of Network
College of IT- University of Babylon
System.out.print("Write a line :");
String line = buffReader.readLine();
System.out.println("Line read :"+line);
} catch (IOException e) {
e.printStackTrace();}}}
Page 6
Monday, May 30, 2016
Download