欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

使用ByteArrayOutputStream写入字符串方式

时间:2022-06-12 09:34:44|栏目:JAVA代码|点击:

使用ByteArrayOutputStream写入字符串

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;
/**
 * 使用ByteArrayOutputStream写入字符串
 * @author GuoKe
 *说明:1,不关联源	2.可以不释放资源	3.使用toByteArray()获取数据
 */
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();
			}
		}
	}
}

文件与二进制数据互转-ByteArrayOutputStream

// 获取二进制数据
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) {
            // 数据存储到ByteArrayOutputStream中
            baos.write(c);
            c = bis.read();
        }
        fis.close();
        bis.close();
        // 转换成二进制
        return baos.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // 没有关闭ByteArrayOutputStream流的意义,空实现
        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;
}
 
// 二进制数据转成文件
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没有执行close()的意义,原因:底层空实现(源码如下)

上一篇:Java使用list集合remove需要注意的事项(使用示例)

栏    目:JAVA代码

下一篇:使用maven创建普通项目命令行程序详解

本文标题:使用ByteArrayOutputStream写入字符串方式

本文地址:http://www.codeinn.net/misctech/204524.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有