In this we will discuss about the Java I/O Scanning and Formatting.
Scanning and Formatting in Java
In this we will discuss about the Java I/O Scanning and Formatting.
In the perspective of programming in Java I/O we are ( i.e. human being) able to read neatly formatted data to work with. When a programmer does programming of I/O the input data undergoes with the translation and retrieves a human readable neatly formatted data to work with. For this Java provides the two APIs namely scanner and the formatting. The scanner API is responsible for breaking the input data into the particular tokens which are associated with the bits of data whereas, formatting API is responsible for assembling the individual tokens associated with the bits of data to a human readable form.
Scanning
Java provides a class java.util.Scanner object of which are used to break the formatted input data to the particular tokens according to their data type. Scanner separates the tokens using a delimiter, the default delimiter that the Scanner uses is the white space except this to use a delimiter one can use the useDelimiter() method by giving a specified pattern.
Methods of Scanner
There are various of methods of Scanner class some of mostly used methods are as follows :
- close() : This method is used to close the scanner.
public void close()
- delimiter() : This method is used to find out the pattern
which is currently used by the Scanner to match delimiters.
public Pattern delimiter()
- next() : This method is used to find out the scanner's next
completed token and it returns that token.
public String next()
- hasNext() : This method specifies that whether the scanner's
input has another token or not. It returns 'true' if the another token is available.
public boolean hasNext()
- next(Pattern pattern) : This method is used to find out the
next token if it matches with the specified pattern.
public String next(Pattern pattern)
- hasNext(Pattern pattern) : This method specifies that
whether
the scanner's input has another token which matches the given pattern or not. It returns
'true' if the token with the matched pattern is available.
public boolean hasNext(Pattern pattern)
- next(String pattern) : This method is used to find out the
next token which matches the pattern made up with the specified string.
public String next(String pattern)
- hasNext(String pattern) : This method specifies that
whether the scanner's input has another token which matches the pattern made up
with the specified string or not.
public boolean hasNext(String pattern)
- nextBigDecimal() : This method is used to scan the next
token of scanner's input as of BigDecimal type.
public BigDecimal nextBigDecimal()
- hasNextBigDecimal() : This method specifies that whether
the scanner's input has another completed token is of type BigDecimal or not.
It returns true if the input token is of BigDecimal type.
public boolean hasNextBigDecimal()
- nextBigInteger() : This method is used to scan the next
token of scanner's input as of BigInteger type.
public BigInteger nextBigInteger()
- hasNextBigInteger() : This method specifies whether the
scanner's input has another completed token is of type BigInteger or not. It
returns true if the input token is of BigInteger type.
public boolean hasNextBigInteger()
- nextBigInteger(int radix) : This method is used to scan the
next token from scanner's input as of BigInteger type with the specified radix.
public BigInteger nextBigInteger(int radix)
- hasNextBigInteger(int radix) : This method specifies
whether the scanner's input has another completed token is of BigInteger
type
with the specified radix or not. It returns true if the input token is of type BigInteger with the specified radix.
public boolean hasNextBigInteger(int radix)
- nextBoolean() : This method is used to scan the next token
from scanner's input as of boolean type value. It returns that value.
public boolean nextBoolean()
- hasNextBoolean() : This method specifies whether the
scanner's input has another completed token consisted the boolean value or not.
public boolean hasNextBoolean()
- nextByte() : This method is used to scan the next token
from scanner's input as of byte value.
public byte nextByte()
- hasNextByte() : This method specifies whether the scanner's
input has another completed token is interpreted as a byte value or not. It
returns 'true' if the value is interpreted by the byte value.
public boolean hasNextByte()
- nextByte(int radix) : This method is used to scan the next
token from the scanner's input as of byte value with the specified radix.
public byte nextByte(int radix)
- hasNextByte(int radix) : This method specifies whether the
scanner's input has another completed token is interpreted as a byte value with
the specified radix or not. It returns true if the value is interpreted by the
byte value with the specified radix.
public boolean hasNextByte(int radix)
- nextDouble() : This method is used to scan the next token
from the scanner's input as of double value.
public double nextDouble()
- hasNextDouble() : This method specifies whether the
scanner's input has another completed token is interpreted as a double value or
not. It returns 'true' if the value is interpreted as a double value.
public boolean hasNextDouble()
- nextFloat() : This method is used to scan the next token
from the scanner's input as of float value.
public float nextFloat()
- hasNextFloat() : This method specifies whether the
scanner's input has another completed token which is interpreted as a float
value or not. It returns 'true' if the value is interpreted as a float value.
public boolean hasNextFloat()
- nextInt() : This method is used to scan the next token from
the scanner's input as of integer value.
public int nextInt()
- hasNextInt() : This method specifies whether the scanner's
input has another completed token which is interpreted as an int value or not.
It returns 'true' if the value is interpreted as an int value.
public boolean hasNextInt()
- nextInt(int radix) : This method is used to scan the next
token from the scanner's input as of integer value with specified radix.
public int nextInt(int radix)
- hasNextInt(int radix) : This method specifies whether the
scanner's input has another completed token which is interpreted as an integer
value with the specified radix. It returns 'true' if the value is interpreted as
an integer value with the specified radix.
public boolean hasNextInt(int radix)
- nextLine() : This method is used to scan the next line from
the scanner's input.
public String nextLine()
- hasNextLine() : This method specifies whether the scanner's
input has another line or not. It returns 'true' if the scanner has next line.
public boolean hasNextLine()
- nextLong() : This method is used to scan the next token
from the scanner's input as of long value type.
public long nextLong()
- hasNextLong() : This method specifies whether the scanner's
input has another completed token which is interpreted as long value or not. It
returns 'true' if the value is interpreted as long value.
public boolean hasNextLong()
- nextLong(int radix) : This method is used to scan the next
token from the scanner's input as of long value type with the specified radix.
public long nextLong(int radix)
- hasNextLong(int radix) : This method specifies whether the
scanner's input has another completed token which is interpreted as long value
with the specified radix or not. It returns 'true' if the value is interpreted
as long value with the specified radix.
public boolean hasNextLong(int radix)
- nextShort() : This method is used to scan the next token
from the scanner's input as of short value type.
public short nextShort()
- hasNextShort() : This method specifies whether the
scanner's input has another completed token which is interpreted as short value
or not. It returns 'true' if the value is interpreted as short value.
public boolean hasNextShort()
- nextShort(int radix) : This method is used to scan the next
token from the scanner's input as of short value type with the specified radix.
public short nextShort(int radix)
- hasNextShort(int radix) : This method specifies whether the
scanner's input has another completed token which is interpreted as short value
with the specified radix or not. It returns 'true' if the value is interpreted
as short value with the specified radix.
public boolean hasNextShort(int radix)
- locale() : This method returns the locale of the current
scanner.
public Locale locale()
- reset() : This method is used to reset the current scanner.
public Scanner reset()
- skip(Pattern pattern) : This method is used to skip the
input which matches the specified pattern in this case use of delimiters is
ignored.
public Scanner skip(Pattern pattern)
- skip(String pattern) : This method is used to skip the
input which matches the pattern made up with the specified string.
public Scanner skip(String pattern)
- useDelimiter(Pattern pattern) : This method is used to
change scanner's current delimiter pattern to a specific pattern.
public Scanner useDelimiter(Pattern pattern)
- useDelimiter(String pattern) : This method is used to
change the scanner's current delimiter pattern to a specific pattern which is
made up of the specified string.
public Scanner useDelimiter(String pattern)
- useLocale(Locale locale) : This method is used to change
the scanner's current locale to the specified locale.
public Scanner useLocale(Locale locale)
Formatting
In Java to create a formatted output stream there are two classes PrintWriter and PrintStream.
PrintStream is a byte stream class whereas, the PrintWriter is a character stream class. Both of the classes have same methods for writing and formatting the input data.
- print() and println() methods are used to format the particular type of
values.
- print(boolean b) : This method is used to print a boolean value.
- print(char c) : This method is used to print a character.
- print(char[] s) : This method is used to print an array of characters.
- print(double d) : This method is used to print a double precision floating point number.
- print(float f) : This method is used to print a floating point number.
- print(int i) : This method is used to print an integer value.
- print(long l) : This method is used to print the long integer value.
- print(Object obj) : This method is used to print an object.
- print(String s) : This method is used to print a string.
- println() : This method is used to terminate the current line.
- println(boolean x) : Use of this method is to terminate the current line after printing the boolean value.
- println(char x) : Use of this method is to terminate the current line after printing the char value.
- println(char[] x) : Use of this method is to terminate the current line after printing an array of characters.
- println(double x) : Use of this method is to terminate the current line after printing a double precision floating point number.
- println(float x) : Use of this method is to terminate the current line after printing a floating point number.
- println(int x) : Use of this method is to terminate the current line after printing an integer value.
- println(long x) : Use of this method is to terminatethe current line after printing a long integer value.
- println(Object x) : Use of this method is to terminate the current line after printing the object.
- println(String x) : Use of this method is to terminate the current line
after printing string.
- format() methods are used to format the multiple arguments on the basis of a
format string.
- format(Locale l, String format, Object... args) : This method is used to print the formatted output on the basis of different arguments.
- format(String format, Object... args) : This method is used to print the formatted output on the basis of different arguments.