One of the key areas of Java learning: OutputStream byte output stream usage
FileOutputStream: Sub-class, write the channel of the data
Steps:
1. get the target file
2. Create a channel (if there is no target file, it will be created automatically)
3. writing data write ()
4. Releasing Resources
Attention:
(1) if the target file does not exist, it will create a target file by itself
(2) if the target file exists, first empty the data, 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),boolean value is true then you can
(4) write data with the Write(int a) method, although it receives an int, but actually only one byte of data
(Operation is low eight bits, all the others are discarded)
// will automatically import some packages Import Java.io.File; Import Java.io.FileOutputStream; import java.io.IOException;
//Mode One
1 Public Static voidWriteData ()throwsioexception{2 //1. Find the target file3File File =NewFile ("C:\\users\\bigerf\\desktop\\ folder \\writeTest.java");4 5 //2. Create a channel6FileOutputStream OutputStream =Newfileoutputstream (file);7 8 //3. Start writing data,9 intA = 10;//int Type 4 bytesTenOutputstream.write (a);//Note that only one byte can be output at a time OneOutputstream.write (' B ');//Char type AOutputstream.write (5); - - //0000-0000 0000-0000 0000-0001 1111-1111 = = 511 the intb = 511;//greater than eight bits (9 bits) -Outputstream.write (b);//actual result 255, but not shown - - intc = 63;//less than eight bits (6 bits) +Outputstream.write (c);//garbled - + //4. Close Resources A outputstream.close (); at}
//Mode two
1 Public Static voidWRITEDATA2 ()throwsioexception{2 //1. Find the target file3File File =NewFile ("C:\\users\\bigerf\\desktop\\ folder \\writeTest2.java");4 5 //2. Create a channel (the file that will be created in this step if there is no file in the path)6 //new FileOutputStream (file,true);/true indicates that text is written on the basis of the original text (otherwise it will be emptied and then written)7FileOutputStream OutputStream =NewFileOutputStream (file,true); 8 9 //3. Create a byte array of keysTenString str = "Hello word"; One //Changing a string into a byte array A byte[] B =str.getbytes (); - - //4. Writing Data theOutputstream.write (b);//Hello Word - - //5. Close Resources - outputstream.close (); + -}
Mo Mo says: The input stream and output stream can be copied to the file, so try to implement (writes the data copy of the path file to the byte array before writing out the path path file from the byte array) |
Java IO stream output stream outputstring ()