buffer缓冲之写文件(不用Output可能会出问题)

本文介绍了一种使用BufferedInputStream将数据写入到临时文件的方法。此方法首先检查并创建临时文件所在的目录,然后通过循环读取输入流中的数据,并将其写入到临时文件中,直至所有数据都被正确保存。
 public String getFilieByBufferInputStreamWriteToTemp(String srcFileNameTempPath,BufferedInputStream bufferedInputStream)  throws Exception{
        File newFile = new File(srcFileNameTempPath);
        String path =  newFile.getAbsolutePath();
        byte[] buffer = new byte[1024];
        int len = 0;
        java.io.File file = new File(path);
        File fileParent = file.getParentFile();
        if (!fileParent.exists()) {
            fileParent.mkdirs();
        }
        OutputStream out = new FileOutputStream(path);
        while ((len = bufferedInputStream.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
        out.close();
        return path;
    }

 

### 使用不同编程语言将缓冲区数据文件 #### Node.js 中将 Buffer 数据文件 在 Node.js 中,可以利用 `fs` 模块提供的异步方法轻松地将 Buffer 对象中的数据入到文件中。下面是一段简单的代码示例: ```javascript const fs = require('fs'); // 创建一个包含一些初始数据的 Buffer 实例 let buf = Buffer.from('这是一个测试字符串', 'utf8'); // 将 Buffer 的内容入到指定路径下的新文件中 fs.writeFile('./output.txt', buf, (err) => { if (err) throw err; console.log('The file has been saved!'); }); ``` 这段代码展示了如何创建一个 Buffer 并将其内容保存至磁盘上的文本文件[^1]。 #### Java 中通过 NIO API 将 Buffer 文件 对于 Java 开发者来说,在现代版本的 JDK 中推荐使用更高效的 NIO.2 文件 I/O 库来进行此类操作。这里给一段基于 FileChannel 和 ByteBuffer 类的例子: ```java import java.io.*; import java.nio.channels.FileChannel; import java.nio.ByteBuffer; public class WriteToFileExample { public static void main(String[] args) throws IOException { try (FileOutputStream fos = new FileOutputStream("output.dat"); FileChannel channel = fos.getChannel()) { String contentToWrite = "这是要存入文件的信息"; byte[] bytes = contentToWrite.getBytes(); ByteBuffer buffer = ByteBuffer.allocate(bytes.length); buffer.put(bytes); // 切换为读模式准备 buffer.flip(); // 把缓冲区的数据文件 channel.write(buffer); } } } ``` 此程序片段说明了怎样构建一个字节序列放入 ByteBuffer 后再经由关联的 Channel 发送到目标位置[^2]。 #### C 语言下直接调用系统函数完成相同任务 如果是在较低级别的 C 或类 Unix 系统上工作,则可以直接运用 POSIX 标准定义的操作系统接口来达成目的。如下所示即为一种可能的方式: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <unistd.h> int main(void){ const char *data_to_write = "这里是想要记录下来的文字串"; size_t data_length = strlen(data_to_write)+1; // 加一为了包括终止符 '\0' int fd = open("./output_c.txt", O_WRONLY | O_CREAT | O_TRUNC , S_IRUSR | S_IWUSR ); if(fd == -1){ perror("Failed to create/open the output file."); exit(EXIT_FAILURE); } ssize_t written_bytes = write(fd, data_to_write, data_length); if(written_bytes != (ssize_t)data_length){ fprintf(stderr,"Error writing all data.\nOnly %zd out of %zu bytes were successfully written.",written_bytes,data_length); close(fd); unlink("./output_c.txt"); /* 清除部分入失败后的残留文件 */ exit(EXIT_FAILURE); } close(fd); printf("Data was successfully written into './output_c.txt'\n"); return EXIT_SUCCESS; } ``` 该实例解释了如何分配内存空间给待的字符数组,并借助于低级 I/O 函数 `write()` 来执行实际的数据传输过程[^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值