Java basics-output stream-outputs string arrays to text files and java output streams
Introduction: Example:
Import java. io. fileInputStream; import java. io. fileOutputStream;/*** @ author czchina **/public class TestStream {public static void main (String [] args) {// TODO Auto-generated method stub // declare the reference FileInputStream fls = null for the input stream; // declare the reference FileOutputStream fos = null for the output stream; try {// 1. Generate the object fls = new FileInputStream ("E:/Android/AndroidStudioProjects/text.txt") representing the input stream "); // generate a byte array byte [] buffer = new byte [100 ]; // Call the read method of the input stream object to read data (the read length is <buffer. length-5> data is placed in the buffer array, and 5 is the location where the data is stored. // note that the length of the read data should not exceed the length of the array. Fls. read (buffer, 5, buffer. length-5); // 2. Generate the object fos representing the output stream = new FileOutputStream ("E:/Android/AndroidStudioProjects/store.txt"); fos. write (buffer, 0, buffer. length); // String s = new String (buffer); // System. out. println (s); // call the trim method of a String object and remove the leading and trailing spaces of the String. The test is as follows: // s = s. trim (); // System. out. println (s);} catch (Exception e) {System. out. println (e. toString ());}}}
Text.txt
Store.txt
Buffer is like a bucket in a program:
Data flows take java programs as a reference: