写入指定长度的字节到文件

简介:

使用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.   
  23.         long length_tmp = length2;  
  24.         while ((readSize = ins.read(bytes)) != SystemHWUtil.NEGATIVE_ONE/*-1*/) {  
  25.             length_tmp -= readSize;  
  26.   
  27.             outs.write(bytes, 0, readSize);  
  28.             if (length_tmp == 0) {  
  29.                 break;  
  30.             }  
  31.             //非常重要,千万不能去掉!!!  
  32.              if (length_tmp < SystemHWUtil.BUFF_SIZE/*4096*/) {  
  33.              bytes = new byte[(int) length_tmp];  
  34.              }  
  35.         }  
  36.         outs.flush();  
  37.         if (isCloseOutputStream) {  
  38.             outs.close();  
  39.         }  
  40.         return outs;  
  41.     }  
  42.   
  43. /*** 
  44.      * Not responsible for closing the output and input stream 写入指定长度的字节到输出流 
  45.      *  
  46.      * @param fin 
  47.      * @param fout 
  48.      *            : The divided file 
  49.      * @param length2 
  50.      * @throws IOException 
  51.      */  
  52.     public static void writeFromFile2File(InputStream fin, OutputStream fout,  
  53.             long length2) throws IOException {  
  54.           
  55.         if (length2 == 0) {// want to write zero bytes  
  56.             // if (fout != null) {  
  57.             // fout.close();  
  58.             // }  
  59.             return;  
  60.         }  
  61.         int readSize;  
  62.         byte[] bytes = null;  
  63.         if (length2 >= SystemHWUtil.BUFF_SIZE) {  
  64.             bytes = new byte[SystemHWUtil.BUFF_SIZE];  
  65.         } else {  
  66.             bytes = new byte[(int) length2];  
  67.         }  
  68.   
  69.         long length_tmp = length2;  
  70.         while ((readSize = fin.read(bytes)) != SystemHWUtil.NEGATIVE_ONE) {  
  71.               
  72.             length_tmp -= readSize;  
  73.             fout.write(bytes, 0, readSize);  
  74.             if (length_tmp == 0) {  
  75.                 break;  
  76.             }  
  77.             //非常重要,千万不能删除  
  78.              if (length_tmp < SystemHWUtil.BUFF_SIZE) {  
  79.              bytes = new byte[(int) length_tmp];  
  80.              }  
  81.         }  
  82.   
  83.     }  
  84.   
  85.     /*** 
  86.      * Responsible for closing the output stream 
  87.      *  
  88.      * @param fin 
  89.      * @param outPutFile 
  90.      * @param length2 
  91.      *            :The number of bytes to be written 
  92.      * @param append 
  93.      *            : Whether additional 
  94.      * @throws IOException 
  95.      */  
  96.     public static void writeFromFile2File(FileInputStream fin, File outPutFile,  
  97.             long length2, boolean append) throws IOException {  
  98.           
  99.         if (length2 == 0) {// want to write zero bytes  
  100.             return;  
  101.         }  
  102.         FileOutputStream fout = null;  
  103.         try {  
  104.             fout = new FileOutputStream(outPutFile, append/* 追加 */);  
  105.         } catch (FileNotFoundException e1) {  
  106.             e1.printStackTrace();  
  107.         }  
  108.         try {  
  109.             writeFromFile2File(fin, fout, length2);  
  110.         } catch (IOException e) {  
  111.             e.printStackTrace();  
  112.         } finally {  
  113.             fout.flush();  
  114.             fout.close();// Close the stream  
  115.         }  
  116.     }  

 上述代码见附件中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.           
  14.     }  
相关文章
|
3月前
|
安全 数据安全/隐私保护
突破512字节
突破512字节
33 0
|
7月前
|
算法 前端开发 Java
在字节当了几个月的牛马,醒悟了。
以前也分享过不少实习体验,比如去年就分享了一位师弟的美团实习体验:美团实习三个月,我受益良多,今天来分享一下一位学习圈中学弟的字节实习体验。
128 0
2 字节的 UTF-8 序列的字节 2 无效 解决方法
2 字节的 UTF-8 序列的字节 2 无效 解决方法: 用记事本打开xml文件,另存为 编码 选择 UTF-8,保存替换掉之前的文件,解决问题博客内容仅代表个人观点,如发现阐述有误,麻烦指正,谢谢!
4049 0
|
C语言
文件的长度
文件的长度
161 0
文件的长度
|
存储 数据处理
位,字节与字
位、字节、字(bits, Bytes, words)是计算机数据存储的单位。位是最小的存储单位,每一个位存储一个1位的二进制码(0 or 1),一个字节由8位(8个二进制0 or 1 串)组成。而字通常为16、32或64个位组成。
681 0
|
C++
c++ 数据字节
#include using namespace std; void main() { cout
827 0