Java 20-5-byte output stream some methods for writing data

Source: Internet
Author: User

First of all, review

byte output stream operation steps :
A: Create A byte output stream object
B: Call the Write () method
C: Releasing Resources

Create a byte stream output object

New FileOutputStream ("Fos.txt");

The Write method for the byte output stream is introduced first:

public void Write (int b): Write a Byte
public void Write (byte[] b): Write an array of bytes
public void Write (byte[] b,int off,int len): Write part of a byte array

1, publicvoid write (int b): Write a byte

Fos.write (97); The result is a.

97--The underlying binary data-- open with Notepad--find 97 corresponding character value--a7

At this point, if you want to output 97, only then: there will be a direct output of 97 such as the method of numbers

// 9 // 7

2, publicvoid write (byte[] b): Write an array of bytes

byte [] bys={97,98,99,100,101}; //byte [] bys={' A ', ' B ', ' C ', ' d ', ' e '};  The same result.

3, publicvoid Write (byte[] b,int off,int len): Write part of a byte array

Fos.write (bys,1,3); TXT inside the text is: ABCDEBCD

The Write method of the byte output stream is complete, freeing the resource

Fos.close ();

Byte output stream writes out data for line break and append write

Append write: Each time the program is run, it will be added to the text inside the program to write the content

Create a byte stream output object

FileOutputStream fos = new FileOutputStream ("Fos.txt");

1, how to achieve the data line-wrapping?

After the data is written, the symbol will be replaced.

Note: Different systems are not the same for different line-break symbol recognition:

windows:\r\n
linux:\n
Mac:\r
Some of the more common advanced notebooks are the ones that can recognize any line break symbol.

In this case, cross-platform error-prone, so there will be a way for the program to automatically identify the system to change the sign symbol.

1, write data, implement line wrapping

         for (int x = 0; x < × + +) {            fos.write ("Hello" + x). GetBytes ());            Fos.write ("\ r \ n". GetBytes ());        }

2, how to implement the data append write?

It is possible to use the construction method with the second argument as true, that is, to define the byte stream object when it is created

New true);

Java 20-5-byte output stream some methods for writing data

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.