Java basics-output stream-outputs string arrays to text files and java output streams

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.