把字符串字节数组写入文件

    /**
     * Java,把字符串字节数组写入文件
     * @param byt
     * @throws Exception
     */
    private static void byte2File(byte[] byt) throws Exception {
        if (null == byt) {
            return;
        }
        String targetFile = "C:\\Users\\fileTestTarget.txt";
        File file = new File(targetFile);
        OutputStream output = new FileOutputStream(file);
        BufferedOutputStream out = new BufferedOutputStream(output);
        InputStream in = new ByteArrayInputStream(byt);
        byte[] buff = new byte[1024];
        int len = 0;
        while ((len = in.read(buff)) != -1) {
            out.write(buff, 0, len);
        }
        in.close();
        out.close();
        System.out.println("---- done ----");
    }

如果入参是字符串“HelloWorld”的字节数组,则可以吧HellowWorld写入fileTestTarget.txt。

原文地址:https://www.cnblogs.com/east7/p/12208116.html

时间: 2024-08-23 21:48:33

把字符串字节数组写入文件的相关文章

字符串,字典,数组写入本地文件和从本地文件读取

参考:http://blog.csdn.net/hakusan/article/details/39429393?utm_source=tuicool&utm_medium=referral 一.字符串,字典,数组存储到本地文件 字符串,数组,字典存储到本地文件过程一样,只是要存储的数据类型不同而已,这里以字符串存储到本地文件为例,如下:    NSString *content = @"将字符串存储到本地文件";    (1)获取Documents文件夹路径 参数:(1)指定

java _io_图片到内存(字节数组),字节数组到文件,练习文件流和字节数组流

//图片读取到字节数组中,字节数组写出到文件 public class test{ public static void main(String[]args) { String path="C:/Users/10853/eclipse-workspace/hell/linux学习路线.png"; byte[] data=toByteArray(path); //图片不能直接到字节数组中,is.read()返回的是int类型的大小,new String是解码 //需要写入字节数组(内存)

OC从文件或者URL获取字符串,以及写入文件

OC读取或写入文件 1 /** 2 * initWithContentsOfFile:从文件获取内容 3 * initWithContentsOfURL:从URL获取内容 4 * writeToFile:写入文件 5 * atomically:原子性(文件写入的时候不会中断,在执行完毕之前不会被任何其它任务或事件中断.) 6 */ 7 #import <Foundation/Foundation.h> 8 9 int main(int argc, const char * argv[]) {

java byte【】数组与文件读写(增加新功能)

今天在测试直接写的文章: java byte[]数组与文件读写 时,想调用FileHelper类对字节数组以追加的方式写文件,结果无论怎样竟然数据录入不全,重新看了下文件的追加模式,提供了两种方式: 方式一: 字节数组写入文件(不追加) //将byte数组写入文件 public void createFile(String path, byte[] content) throws IOException { FileOutputStream fos = new FileOutputStream(

字符串、字节数组、流之间的相互转换以及文件MD5的计算

1 using System; 2 using System.IO; 3 using System.Security.Cryptography; 4 using System.Text; 5 6 namespace myMethod 7 { 8 class lgs 9 { 10 static void Main() 11 { 12 Console.ReadKey(); 13 } 14 15 /// <summary> 16 /// 使用不同的编码格式将 字符串 → 字节数组 17 /// &l

C# Byte[]数组读取和写入文件

protected void ByteToString_Click(object sender, EventArgs e) { string content = this.txtContent.Text.ToString(); if (string.IsNullOrEmpty(content)) { return; } //string 转为byte数组 byte[] array = Encoding.UTF8.GetBytes(content); //将byte数组转为string strin

文件转为字节数组工具类

java将文件转为字节数组 /** * 将文件转为字节 * @param listPd * @return */ public static byte[] t3(String path){ File file = new File("F:/util02/apache-tomcat-8.5.23/driverImgs/"+path); FileInputStream stream =null; ByteArrayOutputStream bos =null; byte[] bs; try

六、字节数组流ByteArrayInputStream&amp;ByteArrayOutputStream

一.前提 经常而言我们都是针对文件的操作,然后带上缓冲的节点流进行处理,但有时候为了提升效率,我们发现频繁的读写文件并不是太好,那么于是出现了字节数组流,即存放在内存中,因此有称之为内存流: 1.ByteArrayInputStream类 ByteArrayInputStream包含一个内部缓冲区,其中包含可以从流中读取的字节. 内部计数器跟踪由read方法提供的下一个字节. 关闭一个ByteArrayInputStream没有任何效果. 该流中的方法可以在流关闭后调用,而不生成IOExcept

设计一个字节数组缓存类

转 http://blog.csdn.net/kakashi8841/article/details/42025367 版权所有,转载须注明出处! 1.为什么要 在做网络通信的时候,经常需要用到: 读:就是我们需要从网络流里面读取字节数据,并且由于分包的原因,我们需要自己缓存这些数据,而不是读完立刻丢掉. 写:我们需要把各种类型的数据变成字节写入.比如把int.string.short等变成字节数组写入流. 2.需要什么 我们需要设计一个类来实现: 支持可以不停地往这个类中添加字节 支持写入in