Java io stream-byte stream

Source: Internet
Author: User

2017-11-05 17:48:17

IO streams in Java are categorized into two types, one is byte stream and the other is character stream. The character stream appears to simplify the read and write operations of text data.

If the file you are manipulating is a text file, using a character stream can greatly simplify the operation, but if you do not know anything, use a byte stream.

    • BYTE stream

The two abstract base classes for a byte stream are:inputstream

OutputStream

Abstract classes are obviously not instantiated, so you need to look for the appropriate subclass to operate on. Two important byte input and output stream subclasses are discussed below.

* FileOutputStream

FileOutputStreamA stream used to write raw bytes such as data. To write a character stream, consider using the FileWriter .

* * Construction method

As you can see from the API, you can initialize either using the file class or directly using an address string.

If the file does not exist, the file is created automatically.

* * Common methods

Import Java.io.fileoutputstream;import Java.io.ioexception;public class Demo1 {public    static void Main (string[] args) throws IOException {        FileOutputStream file = new FileOutputStream ("E:\\text.txt");        Byte[] B = "Hello spring.\r\n". GetBytes ();        File.write (b);        File.write ("Hello World". GetBytes ());        File.close ();    }}

line break problem: one thing to note here is the problem of line breaks. Different systems have different recognition for line breaks.

First, explain \ r: Indicates carriage return

      \ n: Indicates line break

inside the Windows system, each line ends with a carriage return + line break (CR+LF), i.e. "\ r \ n";
Unix System, the end of each line only a newline CR, that is, "\ n";mac System, the end of each line is the carriage return CR is ' \ r '.

Advanced Notepad or editor such as Sublime will automatically recognize the different line characters, if written \ n, open in Notepad does not show line-wrapping, but in sublime will be normal line wrapping.

Import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;public class Demo1 { Public    static void Main (string[] args) {        fileoutputstream file = null;        try{        file=new fileoutputstream ("E:\\text.txt");        Byte[] B = "Hello spring.\n". GetBytes ();        File.write (b);        File.write ("Hello World". GetBytes ());}        catch (FileNotFoundException e)        {            e.printstacktrace ();        }        catch (IOException e)        {            e.printstacktrace ();        }        Finally {            if (file!=null) {                try {                    file.close ();                } catch (IOException e) {                    e.printstacktrace () ;                }            }        }    }}

* FileInputStream

FileInputStreamUsed to read raw byte streams such as data. To read a character stream, consider using the FileReader .

* * Construction method

* * Common methods

Returns if there is no more data because the end of the file has been reached -1 .

Import Java.io.fileinputstream;import Java.io.fileoutputstream;import Java.io.ioexception;public class Demo1 {    public static void Main (string[] args) throws IOException {        fileinputstream fin = new FileInputStream ("E:\\text.txt") ;        FileOutputStream fout = new FileOutputStream ("E:\\text2.txt");        Byte[] B = new byte[5];        int len = 0;        while (len = Fin.read (b))! =-1) {            fout.write (b, 0, Len);        }        Fin.close ();        Fout.close ();    }}

The return value is the number of bytes that are read at a time, and returns 1 if the end of the file is reached. When writing a file, you must use the offset method to write, otherwise there will be coverage issues.

The average time to read 1024 or 1024 of the integer multiples, is to read 1kb or more kilobytes at a time.

==> Obviously, if you use an array when reading and writing, it will be much more efficient, so Java is designed with this in mind and provides

BYTE buffer stream: Bufferedoutputstream,bufferedinputstream.

* Bufferedoutputstream

Bufferedoutputstream: This class implements a buffered output stream. By setting this output stream, the application can write individual bytes to the underlying output stream without having to call the underlying system for each byte write.

* * Construction method

The construction method can specify the size of the buffer, which is generally sufficient to use the default buffer size.

* * Common methods

Of course, you can also write byte[] B, which refers to writing a byte array of length Len.

* Bufferedinputstream

BufferedInputStreamAdds some functionality to another input stream, which is the ability to buffer input and support mark and reset methods. When created BufferedInputStream , an array of internal buffers is created. When you read or skip bytes in a stream, you can populate the internal buffer again from the included input stream as needed, filling in more than one byte at a time. The mark action records a point in the input stream, which reset causes mark all bytes read after the last operation to be read again before the new byte is fetched from the included input stream.

* * Construction method

* * Common methods

There is no difference between using the buffered buffer stream and the basic file stream at the time of use, but it is optimized at the bottom, resulting in a significant increase in efficiency.

Java io stream-byte stream

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.