Java byte stream (read-write file)

Source: Internet
Author: User

InputStream
This abstract class is a superclass of all classes that represent the byte input stream. Applications that need to define inputstream subclasses must always provide a way to return the next input byte.

int available ()
Returns the number of bytes that the next caller of this input stream method can read (or skip) from this input stream in a blocked manner.
void Close ()
Closes this input stream and frees all system resources associated with the stream.
void mark (int readlimit)
Marks the current position in this input stream.
Boolean marksupported ()
Tests whether this input stream supports the mark and reset methods.
abstract int Read ()
Reads the next data byte from the input stream.
int read (byte[] b)
Reads a certain number of bytes from the input stream and stores it in buffer array B.
int read (byte[] b, int off, int len)
Reads up to Len data bytes in the input stream into a byte array.
void Reset ()
Relocate this stream to the location when the mark method was last called on this input stream.
Long Skip (long N)
Skips and discards n bytes of data in this input stream.
OutputStream
This abstract class is a superclass of all classes that represent the output byte stream. The output stream accepts output bytes and sends those bytes to a sink. Applications that need to define outputstream subclasses must always provide at least one way to write an output byte.

void Close ()
Closes this output stream and frees all system resources related to this stream.
void Flush ()
Refreshes this output stream and forces all buffered output bytes to be written out.
void Write (byte[] b)
Writes a b.length byte from the specified byte array to this output stream.
void Write (byte[] b, int off, int len)
Writes Len bytes from offset off in the specified byte array to this output stream.
abstract void Write (int b)
Writes the specified bytes to this output stream.
I/O exceptions may be generated when I am doing I/OS, which are non-runtime exceptions and should be handled in the program. such as: FileNotFoundException, eofexception, IOException, etc., the following specific instructions to operate the Java byte stream method.

Read the file:
This example takes the read (buffer) method of FileInputStream, each time it reads 512 bytes from the source program file Openfile.java, stores it in buffer buffers, and then constructs the string new string with the value in buffer ( Buffer) is displayed on the screen. The program is as follows (this example is placed in the package Biz.1cn.stream, in addition, please create a TestFile.txt file in the root directory for normal operation):

1      PackageBiz.1cn.stream; 2     ImportJava.io.FileInputStream; 3     Importjava.io.IOException; 4     /** 5      * @authorchenrz (Simon)6 * @date 2006-6-297 * <p>8 * Java byte stream example-read file (www.1cn.biz)9 * </p>Ten      */   One      Public classReadFile { A          Public Static voidMain (string[] args) { -             Try {   -             //Create a file input stream object theFileInputStream is =NewFileInputStream ("TestFile.txt");  -             //set the number of bytes read -             intn = 512;  -             byteBuffer[] =New byte[n];  +             //Read input stream -              while((is.read (buffer, 0, n)! =-1) && (n > 0)) {   +System.out.print (NewString (buffer));  A             }   at System.out.println ();  -             //close the input stream - Is.close ();  -}Catch(IOException IoE) { - System.out.println (IoE);  -}Catch(Exception e) { in System.out.println (e);  -             }   to         }   +}

Write file:
This example uses System.in.read (buffer) to enter a line of characters from the keyboard, stored in buffer buffers, and then using the Fileoutstream write (buffer) method. Write the contents of the buffer into the file WriteFile.txt, the program is as follows (in this case the program is placed in the package Biz.1cn.stream, the other run will be in the root directory to establish WriteFile.txt file):

1      PackageBiz.1cn.stream; 2     ImportJava.io.FileOutputStream; 3     Importjava.io.IOException; 4     /** 5      * @authorchenrz (Simon)6 * @date 2006-6-297 * <p>8 * Java byte stream Example-write file (www.1cn.biz)9 * </p>Ten      */   One      Public classWriteFile { A          Public Static voidMain (string[] args) { -             Try {   -System.out.print ("Enter the contents of the file you want to save:");  the             intCOUNT, n = 512;  -             byteBuffer[] =New byte[n];  -             //reading the standard input stream -Count =System.in.read (buffer);  +             //creating a file output stream object -FileOutputStream OS =NewFileOutputStream ("WriteFile.txt");  +             //Write output stream AOs.write (buffer, 0, Count);  at             //turn off the output stream - Os.close ();  -System.out.println ("Saved to writefile.txt!");  -}Catch(IOException IoE) { - System.out.println (IoE);  -}Catch(Exception e) { in System.out.println (e);  -             }   to         }   +}

Reprinted from:http://ily0123456.iteye.com/blog/689568

Java byte stream (read-write file)

Related Article

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.