使用Java ,如何把指定长度的字节写入文件呢,或者说如何从inputStream 中读取指定长度的字节写入outputStream中呢?

Java代码  
  1. /***
  2. * write inputstream into file according to specified length.
  3. *
  4. * @param file
  5. * @param ins
  6. *            : not closed
  7. * @param length2
  8. * @throws IOException
  9. */
  10. public static FileOutputStream writeInputStream2File(File file,
  11. InputStream ins, long length2, boolean isCloseOutputStream)
  12. throws IOException {
  13. String parentDir = SystemHWUtil.getParentDir(file.getAbsolutePath());
  14. File fatherFile = new File(parentDir);
  15. if (!fatherFile.exists()) {
  16. fatherFile.mkdirs();
  17. }
  18. FileOutputStream outs = new FileOutputStream(file);
  19. int readSize;
  20. byte[] bytes = null;
  21. bytes = new byte[(int) length2];
  22. long length_tmp = length2;
  23. while ((readSize = ins.read(bytes)) != SystemHWUtil.NEGATIVE_ONE/*-1*/) {
  24. length_tmp -= readSize;
  25. outs.write(bytes, 0, readSize);
  26. if (length_tmp == 0) {
  27. break;
  28. }
  29. //非常重要,千万不能去掉!!!
  30. if (length_tmp < SystemHWUtil.BUFF_SIZE/*4096*/) {
  31. bytes = new byte[(int) length_tmp];
  32. }
  33. }
  34. outs.flush();
  35. if (isCloseOutputStream) {
  36. outs.close();
  37. }
  38. return outs;
  39. }
  40. /***
  41. * Not responsible for closing the output and input stream 写入指定长度的字节到输出流
  42. *
  43. * @param fin
  44. * @param fout
  45. *            : The divided file
  46. * @param length2
  47. * @throws IOException
  48. */
  49. public static void writeFromFile2File(InputStream fin, OutputStream fout,
  50. long length2) throws IOException {
  51. if (length2 == 0) {// want to write zero bytes
  52. // if (fout != null) {
  53. // fout.close();
  54. // }
  55. return;
  56. }
  57. int readSize;
  58. byte[] bytes = null;
  59. if (length2 >= SystemHWUtil.BUFF_SIZE) {
  60. bytes = new byte[SystemHWUtil.BUFF_SIZE];
  61. } else {
  62. bytes = new byte[(int) length2];
  63. }
  64. long length_tmp = length2;
  65. while ((readSize = fin.read(bytes)) != SystemHWUtil.NEGATIVE_ONE) {
  66. length_tmp -= readSize;
  67. fout.write(bytes, 0, readSize);
  68. if (length_tmp == 0) {
  69. break;
  70. }
  71. //非常重要,千万不能删除
  72. if (length_tmp < SystemHWUtil.BUFF_SIZE) {
  73. bytes = new byte[(int) length_tmp];
  74. }
  75. }
  76. }
  77. /***
  78. * Responsible for closing the output stream
  79. *
  80. * @param fin
  81. * @param outPutFile
  82. * @param length2
  83. *            :The number of bytes to be written
  84. * @param append
  85. *            : Whether additional
  86. * @throws IOException
  87. */
  88. public static void writeFromFile2File(FileInputStream fin, File outPutFile,
  89. long length2, boolean append) throws IOException {
  90. if (length2 == 0) {// want to write zero bytes
  91. return;
  92. }
  93. FileOutputStream fout = null;
  94. try {
  95. fout = new FileOutputStream(outPutFile, append/* 追加 */);
  96. } catch (FileNotFoundException e1) {
  97. e1.printStackTrace();
  98. }
  99. try {
  100. writeFromFile2File(fin, fout, length2);
  101. } catch (IOException e) {
  102. e.printStackTrace();
  103. } finally {
  104. fout.flush();
  105. fout.close();// Close the stream
  106. }
  107. }

上述代码见附件中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

写入文件:

Java代码  
  1. /***
  2. * 写入文件
  3. * @param content
  4. * @param charset
  5. * @param readAndWriteResult
  6. * @param file
  7. * @throws IOException
  8. */
  9. private static void writeStubFile(String content, String charset,  File file) throws IOException {
  10. FileWriterWithEncoding fileW = new FileWriterWithEncoding(file, charset);
  11. fileW.write(content);
  12. fileW.close();
  13. }

写入指定长度的字节到文件相关推荐

  1. C语言fgets()函数(以指定长度读取文件中的字符,并存入字符数组变量中)

    C语言fgets()函数(以指定长度读取文件中的字符,并存入字符数组变量中) 需要引入C 标准库 - <stdio.h> 文章目录 描述 声明 参数 返回值 实例 测试(确实只能读n-1个 ...

  2. python写入指定路径的文件_python 从shell读取指定文件以及写入指定文件

    python 从shell读取指定文件以及写入指定文件 运行不同的文件每次都要修改脚本,能不能指定输入文件以及输出文件? python abstract_element.py 1.txt 2.txt ...

  3. Python碎片化学习教程 @19. 在指定路径下创建.txt文件,并写入内容

    代码功能:在指定路径下创建.txt文件,并写入内容 代码如下: file = open("1.txt", "w", encoding="utf8&qu ...

  4. C语言 将整数写入内存指定的连续字节单元中

    将整数数组写入0x40003000开始的连续10个字节内存单元中,注意unsigned char *指向一个字节,而int *指向1个字(4个字),但是可以把字中存储的整数放入字节单元中,只要不超过表 ...

  5. linux tcp阻塞socket recv接收数据 未达到指定长度返回问题

    一直以为在阻塞的tcp socket上使用read/recv读取的数据长度一定和指定的读取长度一致,但是实际测试时发现往往返回的长度都比指定长度短,查找资料发现其实是一直误解了这个函数.    引用& ...

  6. 40、使用javassit操作运行时字节码文件

    1.是什么 Javassit是一个功能包,作用类似于java的反射,用于操作运行时字节码文件,实现动态编程,但性能高于反射. 2.怎么用? 首先需要获取存放class文件的容器ClassPool,根据 ...

  7. 压缩文本、字节或者文件的压缩辅助类-GZipHelper 欢迎收藏

    压缩文本.字节或者文件的压缩辅助类-GZipHelper 欢迎收藏 下面为大家介绍一.NET下辅助公共类GZipHelper,该工具类主要作用是对文本.字符.文件等进行压缩与解压.该类主要使用命名空间 ...

  8. 这一次,彻底弄懂 Java 字节码文件!

    作者 | 东升的思考 责编 | Elle 不啰嗦,直接从最最简单的一段Java源代码开启Java整体字节码分析之旅. Java 源码文件 package com.dskj.jvm.bytecode; ...

  9. C#按指定长度分割字符串

    这几天学习分析声音的波形数据,接收到的是十六进制的数据,需要将数据转换成十进制再绘图,这个过程涉及到字符串的分割,正好可以促进自己对C#相关知识的学习.说到分割字符串,我首先想到的是Split,但根据 ...

最新文章

  1. JVM的垃圾回收机制详解和调优
  2. bpython bs4用哪个解释器好_针对python爬虫bs4(BeautifulSoup)库的基础问题
  3. 可以输入值的下拉框(select和input的组合使用)
  4. 程序默认在副屏显示_聊一款性价比极高的电竞显示器
  5. Linux下画原理图和PCB
  6. 产品认知:产品经理如何锻炼产品思维?
  7. 计算机录音机应用程序在哪,windows7如何给电脑录音 windows7录音机在哪
  8. idea中git回退远程仓库版本
  9. DarkSide勒索病毒分析
  10. python开发mbus程序_一种PMBus总线电源模块的控制与实现
  11. 锦锐单片机开发工具_飞思卡尔单片机PE开发工具硬件及软件
  12. Non-static method 'save(java.long.Object)' cannot be referenced from a static context.
  13. 502网关错误解决_“ HTTP 502错误的网关”错误和解决方案
  14. linux php muma,php实现Linux服务器木马排查及加固功能
  15. 还在用老办法扫描?纸质文档变电子文档,手机这个功能一键搞定!
  16. ps2019布尔运算快捷键_ps你必须要知道的布尔运算
  17. 10 个快速提升技术水平的方法
  18. 听说这两款是最适合程序员编程的电脑
  19. 公民身份证校验规则最新最全最严格(包含最后一位校验码校验)
  20. 在阿里巴巴工作是怎样一番体验?

热门文章

  1. 【RAC】 RAC For W2K8R2 安装--总体规划 (一)
  2. Spring 注解学习手札(七) 补遗——@ResponseBody,@RequestBody,@PathVariable(转)
  3. 【转载】请问Silverlight 获取客户端网卡mac码
  4. ob 接oracle10时接不上怎么办?
  5. 孤陋寡闻了吧?Python 居然可以做这30件神奇好玩的事情(附教程)
  6. html radio 作用域,ionic 表单输入 ion-checkbox ion-radio ion-toggle ion-spinner
  7. jdk 1.8 java.policy,JDK1.8 导致系统报错:java.security.InvalidKeyException:illegal Key Size
  8. 登录方式1:MySQL自带客户端
  9. MyBatis 源码解读-获得Mapper 对象
  10. 为什么需要ORM 框架