Java I/O useful to process input & produce the output of the program. The java.io package contains all required classes that used for input & output operation. File handling in java can be performed by java I/O API. java.io package supports many data such as primitives, objects, characters etc.
The sequence of data known as a stream. In simple word, stream means continuous flow,
Command | Description |
---|---|
System.in |
standard input stream used to read character |
System.out |
standard output stream used to produce the result of the program |
System.err |
standard error stream used to output error data produced by the program |
Print() |
the method used to display text on the console |
Depending on the type of operation stream primarily divided into two class:
Used to read data from the source, it might be file, array, device, or socket. For example FileInputStream, BufferedInputStream, ByteArrayInputStream etc.
Used to write data to the destination, it might be file, array, or socket. For example FileOutputStream, BufferedOutputStream, ByteArrayOutputStrem etc.
Depending on type of file, stream primarily divided into two class:
ByteStream used to process data byte with 8 bits. Bytestream has many classes, FileInputStream & FileOutputStream are common out of them.
Used to read data from sources.it obtains input bytes from a file. commonly used for reading byte-oriented data such as audio, image, video, etc.
public class FileInputStream extends InputStream
Using constructor :
InputStream f = new FileInputStream ("C:/java/slightbook");
File f = new File("C:/java/slightbook");
InputStream f = new FileInputStream(f);
Used to write data to the destination. It is an output stream used for writing data to file.it allows to write primitive values into a file, use FileOutputstream class. we can write byte-oriented as well as character-oriented data through FileOutputStream class.
public class FileOutputStream extends OutputStream
Using constructor:
OutputStream f = new FileOutputStream ("C:/java/slightbook");
File f = new File("C:/java/slightbook");
OutputStream f = new FileOutputStream(f);
Let take look into FilterInputStream,
The class used to read information from the stream. Internally used to buffer mechanism to generate performance fast. when bytes from the stream are read or skipped, the internal buffer automatically refilled from the contained input stream. when BufferedInputStream is created, an internal buffer array created.
Let see declaration for java.io.BufferedInputStream:
public class BufferedInputStream extends BufferedInputStream
The class used for buffering output stream. Internally used to buffer to store information. It adds more efficiency than writing data directly into a stream. In addition, the buffer in an outputStream used the BufferedOutputStream class.
OutputStream os = new BufferedOutputStream (new FileOutputStream ("D:\\drive\\myfile.txt"));
Let’s take look on declaration for java.io.BufferedOutputStream:
public class BufferedOutputStream extends BufferedOutputStream
Character stored using Unicode convention. CharacterStream allows reading/write data character by character.it has many classes but FileReader & FIleWriter are the most common CharacterStream classes.
It is useful to read data from sources.
It is useful to write data to the destination.
File class in java represents the file & directory pathname in an abstract manner. The pathname is either absolute or relative.it is used for the creation of directories & files. file deletion, file searching, renaming directories, listing the contents of a directory, etc
The file object describes the actual file or directory on the disk.
A file Object created by passing in a string that describes the name of a file or string or file object.
File a - new File("/usr/local/bin/books");
This Constructor used to create a new file instance from parent abstract pathname & child pathname string.
This Constructor used to create a new File instance by converting the pathname string into an abstract pathname.
This Constructor used to create a new file instance from string patent & string child.
This Constructor used to create a new File instance by converting the file URI into an abstract pathname.
Modifier and Type |
Method |
Description |
---|---|---|
static File |
CreateTempFile(Sting prefix, String suffix) |
It creates an empty file in the default temporary-file dirctory ,using the given prefix and suffix to generate its name. |
boolean |
createNewFile() |
It atomically creates a new , emptyfile named by this abstract pathname if and only if a file with this name does not yet exist. |
boolean |
canWrite() |
It tests whether the application can modify the file denoted by this abstract pathname.String[] |
boolean |
canExecute() |
It tests whether the application can execute the file denoted by this abstract pathname. |
boolean |
canRead() |
It tests whether the application can read the file denoted by this abstract pathname. |
boolean |
isAbsolute() |
It tests whether this abstract pathname is absolute. |
boolean |
isDirectory() |
It tests whether the file denoted by this abstract pathname is a directory. |
boolean |
isFile() |
It tests whether the file denoted by this abstract pathname is a normal file. |
boolean |
getName() |
It returns the name of the file or directory denoted by this abstract pathname. |
boolean |
getParent() |
It returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory. |
Path |
toPath() |
It returns a java.nio.file.path object constructed from this abstract pathname. |
URI |
toURI() |
It constructs a file.URI that represents this abstract pathname. |
File[] |
listFiles() |
It returns an array of abstract pathname denoting the file in the directory denoted by this abstract pate. |
long |
getFreeSpace() |
It returns the number of unallocated bytes in the partition named by this abstract pathname. |
String[] |
list(FilenameFilter filter) |
It returns an array of string naming the files and directories in the directory by this abstract pathname that satisfy the specified filter. |
boolean |
mkdir() |
It creates the directory named by this abstract pathname. |
Scanner class located in java.util package. It provides various ways to read input. It breaks the input into tokens using delimiter which is whitespace.it provides methods to read and parse primitive values. It is widely used to parse text for string & primate types using regex class.it is the simplest way to add input.
public final class Scanner
extends Object
implements Iterator<String>
To make an instance of a scanner that reads input from the user, you need to pass the input stream in the constructor of the scanner class. e.g.
Scanner in = new Scanner(System.in);
/* by parsing string */
Scanner in = new Scanner("Slightbook");