写入指定长度的字节到文件
使用Java ,如何把指定长度的字节写入文件呢,或者说如何从inputStream 中读取指定长度的字节写入outputStream中呢?
- /***
- * write inputstream into file according to specified length.
- *
- * @param file
- * @param ins
- * : not closed
- * @param length2
- * @throws IOException
- */
- public static FileOutputStream writeInputStream2File(File file,
- InputStream ins, long length2, boolean isCloseOutputStream)
- throws IOException {
- String parentDir = SystemHWUtil.getParentDir(file.getAbsolutePath());
- File fatherFile = new File(parentDir);
- if (!fatherFile.exists()) {
- fatherFile.mkdirs();
- }
- FileOutputStream outs = new FileOutputStream(file);
- int readSize;
- byte[] bytes = null;
- bytes = new byte[(int) length2];
- long length_tmp = length2;
- while ((readSize = ins.read(bytes)) != SystemHWUtil.NEGATIVE_ONE/*-1*/) {
- length_tmp -= readSize;
- outs.write(bytes, 0, readSize);
- if (length_tmp == 0) {
- break;
- }
- //非常重要,千万不能去掉!!!
- if (length_tmp < SystemHWUtil.BUFF_SIZE/*4096*/) {
- bytes = new byte[(int) length_tmp];
- }
- }
- outs.flush();
- if (isCloseOutputStream) {
- outs.close();
- }
- return outs;
- }
- /***
- * Not responsible for closing the output and input stream 写入指定长度的字节到输出流
- *
- * @param fin
- * @param fout
- * : The divided file
- * @param length2
- * @throws IOException
- */
- public static void writeFromFile2File(InputStream fin, OutputStream fout,
- long length2) throws IOException {
- if (length2 == 0) {// want to write zero bytes
- // if (fout != null) {
- // fout.close();
- // }
- return;
- }
- int readSize;
- byte[] bytes = null;
- if (length2 >= SystemHWUtil.BUFF_SIZE) {
- bytes = new byte[SystemHWUtil.BUFF_SIZE];
- } else {
- bytes = new byte[(int) length2];
- }
- long length_tmp = length2;
- while ((readSize = fin.read(bytes)) != SystemHWUtil.NEGATIVE_ONE) {
- length_tmp -= readSize;
- fout.write(bytes, 0, readSize);
- if (length_tmp == 0) {
- break;
- }
- //非常重要,千万不能删除
- if (length_tmp < SystemHWUtil.BUFF_SIZE) {
- bytes = new byte[(int) length_tmp];
- }
- }
- }
- /***
- * Responsible for closing the output stream
- *
- * @param fin
- * @param outPutFile
- * @param length2
- * :The number of bytes to be written
- * @param append
- * : Whether additional
- * @throws IOException
- */
- public static void writeFromFile2File(FileInputStream fin, File outPutFile,
- long length2, boolean append) throws IOException {
- if (length2 == 0) {// want to write zero bytes
- return;
- }
- FileOutputStream fout = null;
- try {
- fout = new FileOutputStream(outPutFile, append/* 追加 */);
- } catch (FileNotFoundException e1) {
- e1.printStackTrace();
- }
- try {
- writeFromFile2File(fin, fout, length2);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- fout.flush();
- fout.close();// Close the stream
- }
- }
上述代码见附件中io0007-find_progess\src\main\java\com\io\hw\file\util\FileUtils.java
依赖的包:is_chinese-0.0.2-SNAPSHOT.jar
参考:http://hw1287789687.iteye.com/blog/2023095
写入文件:
- /***
- * 写入文件
- * @param content
- * @param charset
- * @param readAndWriteResult
- * @param file
- * @throws IOException
- */
- private static void writeStubFile(String content, String charset, File file) throws IOException {
- FileWriterWithEncoding fileW = new FileWriterWithEncoding(file, charset);
- fileW.write(content);
- fileW.close();
- }
写入指定长度的字节到文件相关推荐
- C语言fgets()函数(以指定长度读取文件中的字符,并存入字符数组变量中)
C语言fgets()函数(以指定长度读取文件中的字符,并存入字符数组变量中) 需要引入C 标准库 - <stdio.h> 文章目录 描述 声明 参数 返回值 实例 测试(确实只能读n-1个 ...
- python写入指定路径的文件_python 从shell读取指定文件以及写入指定文件
python 从shell读取指定文件以及写入指定文件 运行不同的文件每次都要修改脚本,能不能指定输入文件以及输出文件? python abstract_element.py 1.txt 2.txt ...
- Python碎片化学习教程 @19. 在指定路径下创建.txt文件,并写入内容
代码功能:在指定路径下创建.txt文件,并写入内容 代码如下: file = open("1.txt", "w", encoding="utf8&qu ...
- C语言 将整数写入内存指定的连续字节单元中
将整数数组写入0x40003000开始的连续10个字节内存单元中,注意unsigned char *指向一个字节,而int *指向1个字(4个字),但是可以把字中存储的整数放入字节单元中,只要不超过表 ...
- linux tcp阻塞socket recv接收数据 未达到指定长度返回问题
一直以为在阻塞的tcp socket上使用read/recv读取的数据长度一定和指定的读取长度一致,但是实际测试时发现往往返回的长度都比指定长度短,查找资料发现其实是一直误解了这个函数. 引用& ...
- 40、使用javassit操作运行时字节码文件
1.是什么 Javassit是一个功能包,作用类似于java的反射,用于操作运行时字节码文件,实现动态编程,但性能高于反射. 2.怎么用? 首先需要获取存放class文件的容器ClassPool,根据 ...
- 压缩文本、字节或者文件的压缩辅助类-GZipHelper 欢迎收藏
压缩文本.字节或者文件的压缩辅助类-GZipHelper 欢迎收藏 下面为大家介绍一.NET下辅助公共类GZipHelper,该工具类主要作用是对文本.字符.文件等进行压缩与解压.该类主要使用命名空间 ...
- 这一次,彻底弄懂 Java 字节码文件!
作者 | 东升的思考 责编 | Elle 不啰嗦,直接从最最简单的一段Java源代码开启Java整体字节码分析之旅. Java 源码文件 package com.dskj.jvm.bytecode; ...
- C#按指定长度分割字符串
这几天学习分析声音的波形数据,接收到的是十六进制的数据,需要将数据转换成十进制再绘图,这个过程涉及到字符串的分割,正好可以促进自己对C#相关知识的学习.说到分割字符串,我首先想到的是Split,但根据 ...
最新文章
- JVM的垃圾回收机制详解和调优
- bpython bs4用哪个解释器好_针对python爬虫bs4(BeautifulSoup)库的基础问题
- 可以输入值的下拉框(select和input的组合使用)
- 程序默认在副屏显示_聊一款性价比极高的电竞显示器
- Linux下画原理图和PCB
- 产品认知:产品经理如何锻炼产品思维?
- 计算机录音机应用程序在哪,windows7如何给电脑录音 windows7录音机在哪
- idea中git回退远程仓库版本
- DarkSide勒索病毒分析
- python开发mbus程序_一种PMBus总线电源模块的控制与实现
- 锦锐单片机开发工具_飞思卡尔单片机PE开发工具硬件及软件
- Non-static method 'save(java.long.Object)' cannot be referenced from a static context.
- 502网关错误解决_“ HTTP 502错误的网关”错误和解决方案
- linux php muma,php实现Linux服务器木马排查及加固功能
- 还在用老办法扫描?纸质文档变电子文档,手机这个功能一键搞定!
- ps2019布尔运算快捷键_ps你必须要知道的布尔运算
- 10 个快速提升技术水平的方法
- 听说这两款是最适合程序员编程的电脑
- 公民身份证校验规则最新最全最严格(包含最后一位校验码校验)
- 在阿里巴巴工作是怎样一番体验?
热门文章
- 【RAC】 RAC For W2K8R2 安装--总体规划 (一)
- Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable(转)
- 【转载】请问Silverlight 获取客户端网卡mac码
- ob 接oracle10时接不上怎么办?
- 孤陋寡闻了吧?Python 居然可以做这30件神奇好玩的事情(附教程)
- html radio 作用域,ionic 表单输入 ion-checkbox ion-radio ion-toggle ion-spinner
- jdk 1.8 java.policy,JDK1.8 导致系统报错:java.security.InvalidKeyException:illegal Key Size
- 登录方式1:MySQL自带客户端
- MyBatis 源码解读-获得Mapper 对象
- 为什么需要ORM 框架