FileOutPutStream: Subclass, channel for writing data
step:
1. Obtain the target file
2. Create a channel (if there is no target file in the original form, one will be created automatically)
3. Write data to write()
4. Free up resources
Notice:
(1) If the target file does not exist, then you will create a target file yourself
(2) If the target file exists, first clear the data inside and then write the data
(3) If you want to write data on the original data, use the construction method when creating the channel:
OutPutStream (File file, Boolean append), if the boolean value is true, it is OK
(4) Write data using write(int a) method. Although it receives int, it actually only has one byte of data.
(The operation is in the lower eight digits, and the others are thrown away)
//Some packages will be automatically imported: import java.io.File;import java.io.FileOutputStream;import java.io.IOException;
//Method one public static void writeData() throws IOException{//1. Find the target file File file = 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 can be output at a time outputStream.write('b'); // char Type outputStream.write(5); // 0000-0000 0000-0000 0000-0001 1111-1111 == 511int b = 511 ; //Greater than eight (9 bits) outputStream.write(b); //The actual result is 255, but no int c = 63; //Small than eight (6 bits) outputStream.write(c); //Garbage code//4. Close the resource outputStream.close();} //Method 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 a channel (if there is no file in the path, the file created in this step) //new FileOutputStream(file,true); /true means writing text on the original text (or it will be cleared first and then written) FileOutputStream outputStream = new FileOutputStream(file,true); //3. Create a byte array String str = "hello word";//Change the string into a byte array byte[] b = str.getBytes();//4. Write the data outputStream.write(b); //hello word//5. Close the resource outputStream.close();}Momo said:
The input stream and the output stream can copy files, so try to implement them.
(First copy the data of the path file and write it to the byte array, and then write the path file from the byte array)