java读取文件内容再编辑

有时候,我们需要将读取文件的内容到一个byte[] 数组中,然后对这个数组进行一些修改,这时,我们可以借助于ByteArrayOutputStream 这个类来实现。
  ByteArrayOutputStream,顾名思义,同样是一个OutputStream,那么,对于它的写入操作,和其他的 OutputStream应该是没有什么两样,写入代码可以说是随手拈来的,与其他输出流的不同之处在于,ByteArrayOutputStream写入到内存中,并提供一个 toByteArray() 方法返回我们所需要的byte[]。
 

//保存上传的附件信息
String filename = files[0];
String filepath = "upload/append/"+files[1];
BufferedInputStream in = new BufferedInputStream(new FileInputStream(filepath));
ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
System.out.println("available bytes"+in.available());
byte[] temp = new byte[2048];
int size =0;
while((size = in.read(temp))!=-1){ out.write(temp,0,size);
}
in.close();
byte[] content = out.toByteArray();

String stream = new BaseCode64().EncodeBase64(content);
System.out.println("content" +content.length);
effect += insertAttachmentInfo(conn,attNo,processId,filename,stream);


这里把读取的流以base64保存,使用了类BaseCode64()的方法EncodeBase64。
代码如下:


package com.ving.xzfw.util;

import java.io.ByteArrayOutputStream;

public class BaseCode64 {
private String TableBase64;
private String FError;

public BaseCode64()
{
TableBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
FError = new String();
FError = "";
}
public String DecodeBase64(byte [] Value){

ByteArrayOutputStream o = new ByteArrayOutputStream();
byte d[] = new byte[4];
try
{
int count = 0;
byte x[] = Value;
do
{
if(count >= x.length)
{
break;
}
for(int n = 0; n <= 3; n++)
{
if(count >= x.length)
{
d[n] = 64;
} else
{
int y = TableBase64.indexOf(x[count]);
if(y < 0)
{
y = 65;
}
d[n] = (byte)y;
}
count++;
}

o.write((byte)(((d[0] & 0x3f) << 2) + ((d[1] & 0x30) >> 4)));
if(d[2] != 64)
{
o.write((byte)(((d[1] & 0xf) << 4) + ((d[2] & 0x3c) >> 2)));
if(d[3] != 64)
{
o.write((byte)(((d[2] & 3) << 6) + (d[3] & 0x3f)));
}
}
} while(true);
}
catch(StringIndexOutOfBoundsException e)
{
FError = String.valueOf(String.valueOf(FError)) + String.valueOf(String.valueOf(e.toString()));
System.out.println(e.toString());
}
return o.toString();

}

public String EncodeBase64(byte [] Value){
ByteArrayOutputStream o = new ByteArrayOutputStream();
byte d[] = new byte[4];
try
{
int count = 0;
for(byte x[] = Value; count < x.length;)
{
byte c = x[count];
count++;
d[0] = (byte)((c & 0xfc) >> 2);
d[1] = (byte)((c & 3) << 4);
if(count < x.length)
{
c = x[count];
count++;
d[1] = (byte)(d[1] + (byte)((c & 0xf0) >> 4));
d[2] = (byte)((c & 0xf) << 2);
if(count < x.length)
{
c = x[count];
count++;
d[2] = (byte)(d[2] + ((c & 0xc0) >> 6));
d[3] = (byte)(c & 0x3f);
} else
{
d[3] = 64;
}
} else
{
d[2] = 64;
d[3] = 64;
}
int n = 0;
while(n <= 3)
{
o.write(TableBase64.charAt(d[n]));
n++;
}
}

}
catch(StringIndexOutOfBoundsException e)
{
FError = String.valueOf(String.valueOf(FError)) + String.valueOf(String.valueOf(e.toString()));
System.out.println(e.toString());
}
return o.toString();
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值