The use of the output stream outputstring () of the Java IO stream _java

Source: Internet
Author: User

FileOutputStream: Subclass, write out the channel of the data

Steps:

1. Get target file

2. Create channel (if there is no target file, then automatically create one)

3. Writing data Write ()

4. Release of resources

  Attention:

(1) If the target file does not exist, it creates a target file of its own

(2) If the target file exists, first empty the data inside, then write the data

(3) To write data on the original data, use the construction method when creating the channel:

OutputStream (File File,boolean append), a Boolean value of true to

(4) writes the data with the write (int a) method, although receives an int, but actually only one byte of data

(The operation is low eight bits, the rest of the total throw away)

Automatically imports some packages import
java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
//mode one public static void WriteData () throws ioexception{. Find the target file, filename = new file (
"C:\\users\\bigerf\\desktop\\ folder \\writeTest.java");
2. Create a channel FileOutputStream outputstream = new FileOutputStream (file); 3. Start writing data, int a = 10; int Type 4 bytes outputstream.write (a); Note that only one byte outputstream.write (' B ') can be exported at a time. 
Char type Outputstream.write (5); 0000-0000 0000-0000 0000-0001 1111-1111 = = 511 int b = 511; Greater than eight-bit (9-bit) outputstream.write (b); The actual result is 255, but the int c = 63 is not shown; Less than eight bits (6 bits) Outputstream.write (c);
Garbled//4. Close resource Outputstream.close (); }
Mode two
the public static void WriteData2 () throws ioexception{
//1. Find the target file
= filename = new file ("C:\\users\\bigerf \\Desktop\\ folder \\writeTest2.java ");
2. Create a channel (if the file in the path is not present, the file that is created in this step)
//new fileoutputstream (file,true);/true indicates that the text is written on the basis of the original text (otherwise it will be emptied and written)
FileOutputStream outputstream = new FileOutputStream (file,true); 
3. Create a key byte array
String str = "Hello word";
Converts a string into a byte array
byte[] b = str.getbytes ();
4. Write Data
outputstream.write (b);//hello word
//5. Close Resource
outputstream.close ();
}

Mo Mo said:

The input stream and the output stream can be used to copy the file, so you might try to implement

(First write the data copy of 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.