Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write a string using ByteArrayOutputStream

Shulou Source: shulou.com Published: 2022-06-02 07:10:43 10月05日 Update

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.

Tags: Data binary character string meaning file help way clarity content reason bottom layer article novice more source code knowledge industry resources Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Redmi MariaDB Shulou Information vpn