Java IO stream byte output stream OutputString (), output stream outputstring

Source: Internet
Author: User

Java IO stream byte output stream OutputString (), output stream outputstring

One of the key points of Java learning: Use of the OutputStream byte output stream

FileOutPutStream: a subclass that specifies the channel for writing data.

Steps:

1. Get the target file

2. Create a channel (if no target file exists, a channel is automatically created)

3. write Data write ()

4. release resources

Note:

(1) If the target file does not exist, a target file will be created by yourself.

(2) If the target file exists, first clear the data and then write the data.

(3) to write data to the original data, use the constructor when creating the channel:

OutPutStream (File file, Boolean append). If the value of boolean is true, you can

(4) write data using the write (int a) method. Although the received data is int, there is actually only one byte of data.

(The operation is performed with eight lower bits, and all others are lost)

 

// Some packages will be imported automatically. import java. io. File; import java. io. FileOutputStream; import java. io. IOException;
// Method 1
1 public static void writeData () throws IOException {2 // 1. find the target File 3 file = new File ("C: \ Users \ bigerf \ Desktop \ Folder \ writeTest. java "); 4 5 // 2. create a Channel 6 FileOutputStream outputStream = new FileOutputStream (file); 7 8 // 3. start to write data, 9 int a = 10; // int type 4 bytes 10 outputStream. write (a); // note that only one byte 11 outputStream can be output at a time. write ('B'); // char type 12 outputStream. write (5); 13 14 // 0000-0000 0000-0000 0000-0001 1111-1111 = 51115 int B = 511; // greater than eight bits (9 bits) 16 outputStream. write (B); // the actual result is 255, but 17 18 int c = 63 is not displayed; // 19 outputStream with less than eight bits (6 bits. write (c); // garbled Code 20 21 // 4. disable resource 22 outputStream. close (); 23}

 

// Method 2
1 public static void writeData2 () throws IOException {2 // 1. find the target File 3 file = new File ("C: \ Users \ bigerf \ Desktop \ Folder \ writeTest2.java"); 4 5 // 2. create a channel (if there is no file in the path, the file will be created in this step) 6 // new FileOutputStream (file, true ); /true indicates writing text based on the original text (otherwise it will be cleared before writing) 7 FileOutputStream outputStream = new FileOutputStream (file, true); 8 9 // 3. create a key for a byte array 10 String str = "hello word"; 11 // convert the String to a byte array 12 byte [] B = str. getBytes (); 13 14 // 4. write Data 15 outputStream. write (B); // hello word16 17 // 5. disable resource 18 outputStream. close (); 19 20}

Momo said:

The input stream and output stream can be used to copy files.

(Copy the data in the path file to the byte array, and then write the path file from the byte array)

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.