java byte数组与文件读写

java byte数组与文件读写

分类: java 9327人阅读 评论(2) 收藏 举报

将一个文件内的所有内容读取到byte数组,也可以把一个byte[]的内容写入到文件中。可以作为复制文件的方法

  1. import java.io.ByteArrayOutputStream;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6.   
  7. public class FileHelper {  
  8. <span style="white-space:pre">  </span>//第一种获取文件内容方式  
  9.     public byte[] getContent(String filePath) throws IOException {  
  10.         File file = new File(filePath);  
  11.   
  12.         long fileSize = file.length();  
  13.         if (fileSize > Integer.MAX_VALUE) {  
  14.             System.out.println("file too big...");  
  15.             return null;  
  16.         }  
  17.   
  18.         FileInputStream fi = new FileInputStream(file);  
  19.   
  20.         byte[] buffer = new byte[(int) fileSize];  
  21.   
  22.         int offset = 0;  
  23.   
  24.         int numRead = 0;  
  25.   
  26.         while (offset < buffer.length  
  27.   
  28.         && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {  
  29.   
  30.             offset += numRead;  
  31.   
  32.         }  
  33.   
  34.         // 确保所有数据均被读取  
  35.   
  36.         if (offset != buffer.length) {  
  37.   
  38.             throw new IOException("Could not completely read file "  
  39.                     + file.getName());  
  40.   
  41.         }  
  42.   
  43.         fi.close();  
  44.   
  45.         return buffer;  
  46.     }  
  47.       
  48.     //第二种获取文件内容方式  
  49.     public byte[] getContent2(String filePath) throws IOException  
  50.     {  
  51.         FileInputStream in=new FileInputStream(filePath);  
  52.           
  53.         ByteArrayOutputStream out=new ByteArrayOutputStream(1024);  
  54.           
  55.         System.out.println("bytes available:"+in.available());  
  56.           
  57.         byte[] temp=new byte[1024];  
  58.           
  59.         int size=0;  
  60.           
  61.         while((size=in.read(temp))!=-1)  
  62.         {  
  63.             out.write(temp,0,size);  
  64.         }  
  65.           
  66.         in.close();  
  67.           
  68.         byte[] bytes=out.toByteArray();  
  69.         System.out.println("bytes size got is:"+bytes.length);  
  70.           
  71.         return bytes;  
  72.     }  
  73.         //将byte数组写入文件  
  74.     public void createFile(String path, byte[] content) throws IOException {  
  75.   
  76.         FileOutputStream fos = new FileOutputStream(path);  
  77.   
  78.         fos.write(content);  
  79.         fos.close();  
  80.     }  
  81.   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值