Java - Files and I/O
Java - Files and I/O
The java.io bundle contains about each class you may ever need to perform information and yield (I/O) in Java. Every one of these streams speaks to an information source and a yield goal. The stream in the java.io bundle underpins numerous information, for example, natives, question, limited characters, and so forth.
Stream
A stream can be characterized as a succession of information. There are two sorts of Streams −
input stream − The InputStream is utilized to peruse information from a source.
OutPutStream − The OutputStream is utilized for composing information to a goal.
Byte Streams
Java byte streams are utilized to perform information and yield of 8-bit bytes. In spite of the fact that there are numerous classes identified with byte streams however the most every now and again utilized classes are, FileInputStream and FileOutputStream.
Character Streams
Java Byte streams are utilized to perform information and yield of 8-bit bytes, while Java Character streams are utilized to perform info and yield for 16-bit Unicode. Despite the fact that there are numerous classes identified with character streams however the most much of the time utilized classes are, FileReader and FileWriter. In spite of the fact that inside FileReader utilizes FileInputStream and FileWriter utilizes FileOutputStream yet here the real contrast is that FileReader peruses two bytes at any given moment and FileWriter composes two bytes at any given moment.
Standard Streams
All the programming dialects offer help for standard I/O where the client's program can take enter from a console and after that create a yield on the PC screen. On the off chance that you know about C or C++ programming dialects, at that point, you should know about three standard gadgets STDIN, STDOUT and STDERR. Additionally, Java gives the accompanying three standard streams −
Standard Input − This is utilized to encourage the information to client's program and ordinarily, a console is utilized as standard info stream and spoke to as System.in.
Standard Output − This is utilized to yield the information delivered by the client's program and normally a PC screen is utilized for standard yield stream and spoke to as System.out.
Standard Error − This is utilized to yield the blunder information created by the client's program and for the most part, a PC screen is utilized for standard mistake stream and spoke to as System.err.
Reading and Writing Files
As depicted before, a stream can be characterized as a succession of information. The InputStream is utilized to peruse information from a source and the OutputStream is utilized for composing information to a goal.
FileInputStream
This stream is utilized for perusing information from the documents. Items can be made utilizing the watchword new and there are a few sorts of constructors accessible.
Method & Description
1 public void close() throws IOException{}
This method closes the file output stream. Releases any system resources associated with the file. Throws an IOException.
2 protected void finalize()throws IOException {}
This method cleans up the connection to the file. Ensures that the close method of this file output stream is called when there are no more references to this stream. Throws an IOException.
3 public int read(int r)throws IOException{}
This method reads the specified byte of data from the InputStream. Returns an int. Returns the next byte of data and -1 will be returned if it's the end of the file.
4 public int read(byte[] r) throws IOException{}
This method reads r.length bytes from the input stream into an array. Returns the total number of bytes read. If it is the end of the file, -1 will be returned.
5 public int available() throws IOException{}
Gives the number of bytes that can be read from this file input stream. Returns an int.
FileOutputStream
FileOutputStream is utilized to make a record and compose information into it. The stream would make a document, on the off chance that it doesn't as of now exist, before opening it for yield.
Method & Description
1. public void close() throws IOException{}
This method closes the file output stream. Releases any system resources associated with the file. Throws an IOException.
2. protected void finalize()throws IOException {}
This method cleans up the connection to the file. Ensures that the close method of this file output stream is called when there are no more references to this stream. Throws an IOException.
3 public void write(int w)throws IOException{}
This methods write the specified byte to the output stream.
4 public void write(byte[] w)
Writes w.length bytes from the mentioned byte array to the OutputStream.
Java Training in Chennai @ Greens Technology
Learn Java Training In Chennai At Greens Technologys – No1 Java Training Institute In Chennai. Call 89399-15577 For More Details. Enroll today to learn fundamental center Java Training to go for cutting-edge J2EE Java Training in Chennai.
![]() |
| Java Training in Chennai |

Comments
Post a Comment