3, about the mutual conversion between string,file,inputstream

Source: Internet
Author: User

1, introduced the mutual conversion between the String,file,inputstream

1.1 String2inputstream

        /** * String2inputstream (String str) Tool method * *  @param str *            required to convert the string str * @return returned is the result of the string Str converted to InputStream */PU Blic static InputStream String2inputstream (String str) {Bytearrayinputstream stream = new Bytearrayinputstream ( Str.getbytes ()); return stream;}
1.2 inputstream2string (InputStream is); String Inputstream2string (InputStream is, string charset)
        /** * inputstream2string (InputStream is) * * @param is * input stream inputstream is * @return input stream InputStream is Convert to a string */public static string inputstream2string (InputStream is) {StringBuffer buffer = null; BufferedReader in = null;try {in = new BufferedReader (new inputstreamreader); buffer = new StringBuffer (); String line = "", while (line = In.readline ())! = null) {buffer.append (line);}} catch (Exception e) {throw new RuntimeException ("an exception occurred in the call to Utils.inputstream2string (InputStream is)!!!" ");} return buffer.tostring ();}            /** * Inputstream2string (InputStream is, String charset) * * @param is * input stream InputStream is * @param charset * Character Set string charset * @return input stream InputStream is, character set string CharSet converted to a string */public static string inputstream2st Ring (InputStream is, String charset) {Bytearrayoutputstream BAOs = null;try {BAOs = new Bytearrayoutputstream (); byte data[ ] = new Byte[1024];int len = -1;while ((len = is.read (data))! =-1) {baos.write (data, 0, Len);} StRing A = baos.tostring (charset); return baos.tostring (CharSet);} catch (IOException e) {throw new RuntimeException ("an exception occurred in calling Utils.inputstream2string (InputStream is,string charset)!!!" ");} finally {if (null! = BAOs) {try {baos.close ()} catch (IOException e) {throw new RuntimeException ("in the call utils.inputstream2st The ring (InputStream is, String CharSet) has an exception!!! ");} BAOs = null;}}}

1.3 File2inputstream

/** * File2inputstream (file file) *  * * * * * * * * * * @param file *            files * filename * @return file ' inputstream */public static I Nputstream file2inputstream (file file) {InputStream in = null;try {in = new FileInputStream (File), return in;} catch (Filen Otfoundexception e) {throw new RuntimeException ("an exception occurred in calling Utils.file2inputstream (file file)!!!" ");}} /** * File2inputstream (String filenpath) *  * @param filenpath *            File specific path * @return file converted to InputStream */pub Lic static InputStream File2inputstream (String filenpath) {inputstream in = null; File File = null;try {file = new file (Filenpath), in = new FileInputStream (file), return in;} catch (FileNotFoundException E {throw new RuntimeException ("an exception occurred in calling Utils.file2inputstream (String filenpath)!!!" ");}}


1.4 Inputstreamtofile

        /** * Inputstreamtofile (inputstream ins, file file) * * @param ins * input stream inputstream ins * @param File  * Input stream InputStream ins converted file */public static void Inputstreamtofile (InputStream ins, file file) {OutputStream OS = null;try {os = new FileOutputStream (file), int bytesread = 0;byte[] buffer = new Byte[1024];while ((bytesread = Ins.read (b Uffer))! =-1) {os.write (buffer, 0, bytesread);}} catch (Exception e) {throw new RuntimeException ("an exception occurred in calling Utils.inputstreamtofile (InputStream ins, file file)!!! ");} Finally {try {if (OS! = null) Os.close (), if (INS! = null) {Ins.close ();}} catch (IOException e) {throw new runtimeexception ("An exception occurred in calling Utils.inputstreamtofile (InputStream ins, file file)!!!" ");}}} public static void Inputstreamtofile (InputStream ins, String filePath) {outputstream OS = null; File File = null;try {file = new file (filePath); os = new FileOutputStream (file); int bytesread = 0;byte[] buffer = new byte [1024];while ((bytesread = ins.read (buffer))! =-1) {Os.write(buffer, 0, bytesread);}} catch (Exception e) {throw new RuntimeException ("an exception occurred in calling Utils.inputstreamtofile (InputStream ins, file file)!!! ");} Finally {try {if (OS! = null) Os.close (), if (INS! = null) {Ins.close ();}} catch (IOException e) {throw new runtimeexception ("An exception occurred in calling Utils.inputstreamtofile (InputStream ins, file file)!!!" ");}}}







3, about the mutual conversion between string,file,inputstream

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.