In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to use ByteArrayOutputStream string writing method, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Write the string package com.gk;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;/** * using ByteArrayOutputStream * write the string * @ author GuoKe * using ByteArrayOutputStream description: 1, not associated with source 2. Resources can not be released 3. Use toByteArray () to get data * / public class IOTest8 {public static void main (String [] args) {byte [] dest = null; ByteArrayOutputStream bs = null; try {bs = new ByteArrayOutputStream () String str = "hello"; byte [] datas = str.getBytes (); bs.write (datas,0,datas.length); bs.flush (); dest = bs.toByteArray () System.out.println (dest.length + ":" + new String (dest,0,dest.length/*bs.size () * /));} catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace () } finally {try {if (bs! = null) {/ / alt+shift+z bs.close () }} catch (Exception e) {e.printStackTrace ();}} File and binary data conversion-ByteArrayOutputStream// acquires binary data public static byte [] getFileBinary (String filePath) {FileInputStream fis = null BufferedInputStream bis = null; ByteArrayOutputStream baos = null; try {fis = new FileInputStream (filePath); bis = new BufferedInputStream (fis); baos = new ByteArrayOutputStream (); int c = bis.read (); while (c! =-1) {/ / data is stored in ByteArrayOutputStream baos.write (c); c = bis.read () } fis.close (); bis.close (); / / convert to binary return baos.toByteArray ();} catch (Exception e) {e.printStackTrace ();} finally {/ / has no meaning to close the ByteArrayOutputStream stream, empty implementation try {if (fis! = null) {fis.close () }} catch (IOException e) {e.printStackTrace ();} finally {try {if (bis! = null) {bis.close ();}} catch (IOException e) {e.printStackTrace () } return null;} / / binary data conversion file public static void binaryToFile (byte [] bytes, String filePath) {FileOutputStream fos = null; BufferedOutputStream bos = null; try {fos = new FileOutputStream (filePath); bos = new BufferedOutputStream (fos); bos.write (bytes);} catch (Exception e) {e.printStackTrace () } finally {try {if (fos! = null) {fos.close ();}} catch (IOException e) {e.printStackTrace ();} finally {try {if (bos! = null) {bos.close () }} catch (IOException e) {e.printStackTrace ();}
ByteArrayOutputStream has no meaning to execute close () because the underlying empty implementation (source code is as follows)
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.