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();
}
}
-
Sun认证Java程序员(SCJP)考题常见陷阱集合
(1)Twopublicclassesinthesamefile.(illegal)同一个文件里有两个public类。(非法)(2)Mainmethodcallinganon-staticmethod.(illegal)在main(String[]args)方法内调用一个非静态方法。(非法)(3)Methodswiththesamenameastheconstructor(s).(这种题常有)与Constr...
-
SUN认证考试知识点:OSCache提升J2EE系统运行性能
Cache是一种用于提高系统响应速度、改善系统运行性能的技术。尤其是在Web应用中,通过缓存页面的输出结果,可以很显着的改善系统运行性能。本文中作者给大家介绍一个实现J2EE框架中Web应用层缓存功能的开放源代码项目----OSCache。通过应用OSCache,我们不但可以实...
-
Java方法继承、重载和覆盖总结
Java平台是基于Java语言的平台。这样的平台非常流行。因此微软公司推出了与之竞争的平台以及模仿Java的C#语言。下面文章介绍了Java方法继承、重载、覆盖总结:1、方法继承:利用extends关键字一个方法继承另一个方法,而且只能直接继承一个类。*当Sub类和Base类在同...
-
关于Java的优点
我们为什么要学习java呢?java为何这么的吸引人们的关注?下面小编为大家整理了一些Java的优点。希望对你有所帮助:1.Java是目前使用最为广泛的网络编程语言之一。它具有简单,面向对象,稳定,与平台无关,解释型,多线程,动态等特点。2.简单Java语言简单是指这门语言既易学...