The use of java IO stream output stream OutputString of
- 2020-05-19 04:50:35
- OfStack
FileOutPutStream: subclass that writes the data channel
Steps:
1. Get the target file
2. Create a channel (if there is no target file, one will be created automatically)
3. Write data write ()
Release resources
Note:
(1) if the target file does not exist, a target file will be created
(2) if the target file exists, empty the data inside first, and then write the data
(3) if you want to write data on the original data, use the constructor method when creating the channel:
OutPutStream (File file, Boolean append) and boolean (true) are fine
(4) the data is written with write (int a) method. Although it receives int, it actually only has 1 byte of data
(the operation is low 8 bits, the rest is lost)
// It will automatically import 1 Some packages
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
// way 1
public static void writeData() throws IOException{
//1. Find the target file
File file = new File("C:\\Users\\bigerf\\Desktop\\ folder \\writeTest.java");
//2. create 1 A channel
FileOutputStream outputStream = new FileOutputStream(file);
//3. Start writing data,
int a = 10; // int type 4 bytes
outputStream.write(a); // Notice that you can only output at a time 1 bytes
outputStream.write('b'); // char type
outputStream.write(5);
// 0000-0000 0000-0000 0000-0001 1111-1111 == 511
int b = 511 ; // Is greater than 8 A ( 9 A)
outputStream.write(b); // The actual results 255 , but not shown
int c = 63; // Less than 8 A ( 6 A)
outputStream.write(c); // The statement
//4. Close the resource
outputStream.close();
}
// way 2
public static void writeData2() throws IOException{
//1. Find the target file
File file = new File("C:\\Users\\bigerf\\Desktop\\ folder \\writeTest2.java");
//2. create 1 If there is no file in the path, it will be here 1 Step to create the file)
//new FileOutputStream(file,true); /true Means to write text on top of the original text (otherwise it will be cleared before writing)
FileOutputStream outputStream = new FileOutputStream(file,true);
//3. A key 1 Byte array
String str = "hello word";
// Turns a string into a byte array
byte[] b = str.getBytes();
//4. Write data
outputStream.write(b); //hello word
//5. Close the resource
outputStream.close();
}
Momo said.
Input stream and output stream can achieve the copy of the file, you might as well try to achieve
(first, write the data copy of the path file to the byte array, and then write the outgoing path file from the byte array)