Inputstream, string, and file conversion

Source: Internet
Author: User

1. String --> inputstream
Inputstream string2inputstream (string Str ){
Bytearrayinputstream stream = new
Bytearrayinputstream (Str. getbytes ());
Return stream;
}

2. inputstream --> string
String inputstream2string (inputstream is ){
Bufferedreader in = new bufferedreader (New
Inputstreamreader (is ));
Stringbuffer buffer = new stringbuffer ();
String line = "";
While (line = in. Readline ())! = NULL ){
Buffer. append (line );
}
Return buffer. tostring ();
}


3. file --> inputstream
Inputstream in = new inputstream (New fileinputstream (File ));

The above line reports an error, and the new inputstream reports an error.

Write it as follows:

New fileinputstream (file)

4. inputstream --> File
Public void inputstreamtofile (inputstream INS, file ){
Outputstream OS = new fileoutputstream (File );
Int bytesread = 0;
Byte [] buffer = new byte [8192];
While (bytesread = ins. Read (buffer, 0, 8192 ))! =-1 ){
OS. Write (buffer, 0, bytesread );
}
OS. Close ();
INS. Close ();
}

From: http://blog.sina.com.cn/s/blog_3f7027fa0100mx1v.html

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.