Code pasted, tested, and compiled properly
Package org. mark. streamRW; import java. io. file; import java. io. fileOutputStream; import java. io. outputStream;/*** byte stream output OutputStream * Note: 1. write (byte [] B) array writing; ** @ author Mark */public class OutputStream1 {/*** @ param args */public static void main (String [] args) throws Exception {// TODO Auto-generated method stub // Step 1: use the File class to find a File file File = new File ("d:" + File. separator + "test.txt"); // Step 4: Use the subclass Instantiate the parent class Object OutputStream output = null; output = new FileOutputStream (file); // instantiate the object through object polymorphism. // Step 2: write String str = "Hello World !!! "; Byte [] B = str. getBytes (); // convert to byte array output. write (B); // view the api write (byte [] B) // Step 4: Disable the output stream output. close (); // do not forget to close it. // if the file does not exist, a file is automatically created for the user ;}}
Write (int B) the specified byte to this output stream.
Package org. mark. streamRW; import java. io. file; import java. io. fileOutputStream; import java. io. outputStream; // Use write (int B) to write specified bytes to this output stream. Public class OutputStream2 {/*** @ param args */public static void main (String [] args) throws Exception {// TODO Auto-generated method stub // Step 3: use the File class to find a File file = new File ("d:" + File. separator + "test.txt"); // Step 4: instantiate the parent class Object OutputStream output = null through subclass; output = new FileOutputStream (file); // perform object polymorphism, instantiation // Step 2: Write String str = "Hello World !!! "; Byte [] B = str. getBytes (); // convert it to a byte array // output. write (B); // view api write (byte [] B) for (int I = 0; I <B. length; I ++) {output. write (B [I]); // write (int B) writes the specified byte to this output stream. } // Step 2: Close the output stream output. close (); // do not forget to close it. // if the file does not exist, a file is automatically created for the user. // The file content does not exist }}
// Append the file, line feed,
String str = "\ r \ nHello World !!! "; // \ R \ n line feed
If you have any questions, leave me a message.