Java如何读取文件内容再编辑
有时候,我们需要将读取文件的内容到一个byte[] 数组中,然后对这个数组进行一些修改,这时,我们可以借助于ByteArrayOutputStream 这个类来实现。
ByteArrayOutputStream,顾名思义,同样是一个OutputStream,那么,对于它的写入操作,和其他的 OutputStream应该是没有什么两样,写入代码可以说是随手拈来的',与其他输出流的不同之处在于,ByteArrayOutputStream写入到内存中,并提供一个 toByteArray() 方法返回我们所需要的byte[]。
Java代码
//保存上传的附件信息
String filename = files[0];
String filepath = "upload/append/"+files[1];
BufferedInputStream in = new BufferedInputStream(new FileInputStream(filepath));
ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
tln("available bytes"+lable());
byte[] temp = new byte[2048];
int size =0;
while((size = (temp))!=-1){ e(temp,0,size);
}
e();
byte[] content = teArray();
String stream = new BaseCode64()deBase64(content);
tln("content" +th);
effect += insertAttachmentInfo(conn,attNo,processId,filename,stream);
这里把读取的流以base64保存,使用了类BaseCode64()的方法EncodeBase64。
代码如下:
Java代码
package ;
import ArrayOutputStream;
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 >= th)
{
break;
}
for(int n = 0; n <= 3; n++)
{
if(count >= th)
{
d[n] = 64;
} else
{
int y = xOf(x[count]);
if(y < 0)
{
y = 65;
}
d[n] = (byte)y;
}
count++;
}
e((byte)(((d[0] & 0x3f) << 2) + ((d[1] & 0x30) >> 4)));
if(d[2] != 64)
{
e((byte)(((d[1] & 0xf) << 4) + ((d[2] & 0x3c) >> 2)));
if(d[3] != 64)
{
e((byte)(((d[2] & 3) << 6) + (d[3] & 0x3f)));
}
}
} while(true);
}
catch(StringIndexOutOfBoundsException e)
{
FError = eOf(eOf(FError)) + eOf(eOf(ring()));
tln(ring());
}
return ring();
}
public String EncodeBase64(byte [] Value){
ByteArrayOutputStream o = new ByteArrayOutputStream();
byte d[] = new byte[4];
try
{
int count = 0;
for(byte x[] = Value; count < th;)
{
byte c = x[count];
count++;
d[0] = (byte)((c & 0xfc) >> 2);
d[1] = (byte)((c & 3) << 4);
if(count < th)
{
c = x[count];
count++;
d[1] = (byte)(d[1] + (byte)((c & 0xf0) >> 4));
d[2] = (byte)((c & 0xf) << 2);
if(count < th)
{
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)
{
e(At(d[n]));
n++;
}
}
}
catch(StringIndexOutOfBoundsException e)
{
FError = eOf(eOf(FError)) + eOf(eOf(ring()));
tln(ring());
}
return ring();
}
}
-
2016计算机二级试题及答案
2016计算机二级考试报名啦,正式进入备考阶段,本站小编为同学们带来最新计算机二级模拟试题及答案解析,希望对同学们复习计算机二级考试有所帮助!(1)下列关于栈和队列的描述中,正确的是()A)栈是先进先出B)队列是先进后出C)队列允许在队友删除元素D)栈在栈顶删除元素...
-
H3C虚拟园区网的解决方案
H3C虚拟园区网解决方案把整网的物理资源虚拟成多套逻辑资源,各群组用户根据预先的配置,只能使用相应的资源。下文本站为大家详细介绍H3C虚拟园区网解决方案,欢迎阅读!1虚拟园区网概述企业园区网作为企业网络的核心部分,连接了企业总部的办公、生产、研发、财务等多...
-
2017年计算机二级C语言模拟题及答案
1.1单选题1.下列变量名中,非法的是(C)。A.A25_car-str2.下列常量中,十六进制int型常量是(A)。A.0x5fB.x2aC.046D.7a3.下列常量中,不是字符常量的是(B)。A.'\n'B."y"C.'x'D.'\7'4.在函数体内定义了下述变量a,a的存储类为(D)。inta;A.寄存...
-
2017年全国计算机一级考试模拟试题及答案
全国计算机一级考试是全国计算机等级考试的一种。下面是2017年全国计算机一级考试模拟试题及答案。欢迎阅读。2017年全国计算机一级考试模拟试题及答案1.微机中1K字节表示的二进制位数是()。DA、1000B、8x1000C、1024D、8x10242.计算机硬件能直接识别和执行的...