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

使用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.     }  
时间: 2024-09-17 04:32:23

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

从inputStream中读取指定长度的字节

Java 中如何从InputStream中读取指定长度的字节呢? 代码如下: Java代码   /***       * 从输入流获取字节数组,当文件很大时,报java.lang.OutOfMemoryError: Java heap space       *        * @since 2014-02-19       * @param br_right       * @param length2       * @return       * @throws IOException 

用C#截取指定长度的中英文混合字符串

字符串 我们常做的一件事情,就是在文章系统中,截取一定长度的文章标题,超过指定长度,就加"..." 如两个字符串:string str1 = "中国人要啊abc呀~";string str2 = "1中国人23456abc呀~"; 要截取后,输出: str1 = "中国人要...";str2 = "1中国人2..."; 即要把中英文混合的字符串,在截取后,长度要一致,即8个字节的长度(不包括三个点),而且不

C#编写WinForm,生成数字码并写入指定Excel(求指导哇)

问题描述 Excel文件要求:文件名固定,有两个表单且表单名固定.表单用途:(跟下面的序列号生成规则结合起来,就不太会了--)数字码写入sheet1列"A",每次写入均从第二行往下写(覆盖),写完后其他行清空:数字码写入sheet2列"D",每次写入数据不覆盖,接着之前的数字码下面写(用于记录以往数字码的生成历史).窗体功能:1.数字码生成(这个容易搞定,只是为了介绍下我要做什么)数字码=当天日期(格式:yymmdd)+序列号(格式:XXX,从001开始)2.序列号

Dedecms解决你没指定要上传的文件或文件大小超过限制的问题

今天在使用Dedecms进行本地上传缩略图的时候,突然出现你没指定要上传的文件或文件大小超过限制的故障!感觉很是奇怪呀!一直用得好好的,为何突然出现这样的问题呢? 查找原因,发现来原php.ini文件的设置引起的. 立即动手进行解决: 打开php的配置文件php.ini,通过搜索找到以下几项: ;upload_tmp_dir = 注释掉之后我们改为: upload_tmp_dir = d:\php\tmp 注意tmp文件需要有写入和修改权限 另外php.ini中还有两个参数: pose_max_

安全的截取指定长度的html或者ubb字符串

在将html代码输出到页面时,有时候会需要截断字符串保留指定长度的字符串,由于html中有些标签 必须成对出现,所以在截取html时需要特别注意,不能因为截断问题把页面搞乱掉. 在截取字符串时需要记录每一个标签是否关闭,如果截取到指定长度还有没有关闭的标签,那么我们 需要将标签关闭,或者删除掉未关闭的标签.不考虑某些不需要关闭标签的情况,html开始和结束标签总 是成对出现的,我们可以遍历输入的字符串,并在标签开始时放入堆栈中,遇到结束标签时从堆栈中弹出 一个元素,这样遍历到指定长度,堆栈中留下

编程-[求助]使用java在指定的目录下实现文件的模糊搜索

问题描述 [求助]使用java在指定的目录下实现文件的模糊搜索 一.功能要求: 用户单击"选择地址"按钮,程序自动打开文件夹选择器(如图2),用户选定用户选择目标文件夹后,输入文件名(可包含"*"表示任意连续多个字符,"?"示任意一个字符),选择文件的扩展名.程序自动获取指定目录下满足条件的所有文件,并显示在窗体中(如图1).二.关键技术: 首先获取指定目录下的文件数组,再从该数组中查询满足条件的文件.三.实现的图形用户界面如下: 解决方案 已经

PHP删除指定目录下的所有文件

/**  * 删除指定目录下的所有文件  *  * @param String $dir  要进行操作的路径  * 适合范围,只有用于文件夹内不存在子文件夹的情况下  * 来源  DZ  * 小佳(www.phpcina.cn)  整理 于 2006-06-26   */ function dir_clear($dir) {     $directory = dir($dir);                //创建一个dir类(PHP手册上这么说的),用来读取目录中的每一个文件     wh

生成指定长度的随机字符串

随机|字符串 '检测生成字符串的长度Response.Write(Len(iXuEr_Rnd_Str(100,1,1,1,1,1)) & "<br>")'以HTML编码输出到浏览器,避免含有某些特殊字符时不能正常显示Response.Write(Server.HTMLEncode(iXuEr_Rnd_Str(100,1,1,1,1,1)))'实际应用时,可以直接调用iXuEr_Rnd_Str(Length,S1,S2,S3,S4,Ln)Function iXuEr_

前后台实现精确截取指定长度字符串(考虑全角半角)

页面上很多显示新闻列表时,考虑面子问题,常常需要限制标题长度. 以前都是用 <%#Eval("title").ToString().Length > 10 ? Eval("title").ToString ().Substring(0, 8) + "..." : Eval("title").ToString()%> 这样的方式实现,这样代码即繁多又不能重用,不过这个还不是问题,因为可以在后台写个传字符串 和指