Java byte stream (read/write files) [Post]

Source: Internet
Author: User

Inputstream
This abstract class is a super class that represents all classes of the byte input stream. Applications that need to define the subclass of inputstream must always provide the method to return the next input byte.

Int available ()
Returns the number of bytes that the next caller of the input stream method can read (or skip) from the input stream without blocking.
Void close ()
Close the input stream and release all system resources associated with the stream.
Void mark (INT readlimit)
Mark the current position in the input stream.
Boolean marksupported ()
Test whether the input stream supports the mark and reset methods.
Abstract int read ()
Reads the next Data byte from the input stream.
Int read (byte [] B)
Read a certain number of bytes from the input stream and store them in the buffer array B.
Int read (byte [] B, int off, int Len)
Reads a maximum of Len data bytes from the input stream into the byte array.
Void reset ()
Locate the stream again when the mark method is called for the input stream.
Long SKIP (long N)
Skip and discard n data bytes in the input stream.
Outputstream
This abstract class is a super class that represents all the classes of the output byte stream. The output stream accepts the output bytes and sends them to a receiver. Applications that need to define the outputstream subclass must always provide at least one method that can write one output byte.

Void close ()
Close the output stream and release all system resources related to the stream.
Void flush ()
Refresh the output stream and forcibly write all buffered output bytes.
Void write (byte [] B)
Write B. length bytes from the specified byte array to this output stream.
Void write (byte [] B, int off, int Len)
Write the len bytes starting from offset off in the specified byte array to this output stream.
Abstract void write (int B)
Write the specified byte to the output stream.
I/O exceptions may be generated during I/O operations, which are non-runtime exceptions and should be processed in the program. For example: filenotfoundexception, eofexception, ioexception, etc. The following describes how to operate a java byte stream.

Read files:
In this example, the read (buffer) method of fileinputstream is used to extract the openfile from the source program file every time. java reads 512 bytes, stores them in the buffer, and then displays the new string (buffer) constructed with the buffer value on the screen. The program is as follows (this example program is stored in biz.1cn.stream, and the testfile.txt file is created in the root directory for normal operation ):

Package biz.1cn. stream;
Import java. Io. fileinputstream;
Import java. Io. ioexception;
/**
* @ Author chenrz (Simon)
* @ Date 2006-29
* <P>
* Example of a Java byte stream-reading a file (www.1cn. Biz)
* </P>
*/
Public class readfile {
Public static void main (string [] ARGs ){
Try {
// Create a file input stream object
Fileinputstream is = new fileinputstream ("testfile.txt ");
// Set the number of bytes to read
Int n = 512;
Byte buffer [] = new byte [N];
// Read the input stream
While (is. Read (buffer, 0, n )! =-1) & (n> 0 )){
System. Out. Print (new string (buffer ));
}
System. Out. println ();
// Close the input stream
Is. Close ();
} Catch (ioexception IOE ){
System. Out. println (IOE );
} Catch (exception e ){
System. Out. println (E );
}
}
}

 

 

Write File:
Hosts file ):

 

Package biz.1cn. stream;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
/**
* @ Author chenrz (Simon)
* @ Date 2006-29
* <P>
* JAVA byte stream example-write a file (www.1cn. Biz)
* </P>
*/
Public class writefile {
Public static void main (string [] ARGs ){
Try {
System. Out. Print ("Enter the content of the file to be saved :");
Int count, n = 512;
Byte buffer [] = new byte [N];
// Read the standard input stream
Count = system. In. Read (buffer );
// Create a file output stream object
Fileoutputstream OS = new fileoutputstream ("writefile.txt ");
// Write the output stream
OS. Write (buffer, 0, count );
// Close the output stream
OS. Close ();
System. Out. println ("saved to writefile.txt! ");
} Catch (ioexception IOE ){
System. Out. println (IOE );
} Catch (exception e ){
System. Out. println (E );
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.