9 P1 - Files - in - Java
9 P1 - Files - in - Java
Section One
1. Command-Line Arguments
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 ()
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();
}
}
}
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):
} 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 Tuesday, July 14, 2020
Java Programming 3rd class – Department of Network
Mehdi Ebady Manaa 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]
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);
System.out.print("Write a line :");