日常做项目中为避免重复性工作,我们可以把常用的类包装成自己的类,放入自己的类库中,这样通过maven或者其他引入方式,可以很方便的进行复用。
本文不涉及Java平台的搭建,不涉及操作系统环境,不涉及Maven以及Junit用法。
目的:实现自己的inputstream类。
类说明:类包括一个属性path,一个构造函数,一个read方法。
属性path说明:属性为String类型
构造函数说明:传入String类型参数,为path赋值。
read方法说明:参数length,类型int,表示读取长度。返回为字节数组。如果流内数据足够,则返回长度为length的字节数组,如果流内数据不足,则返回最大可读取长度的字节数组。
实现过程1:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class InputStream {
private String path;
public InputStream(String path){
this.path=path;
}
/**
* 读取指定长度字节,并返回
* 如果流内字节数不足,返回最大可读长度字节
* @param length
* @return 字节数组
*/
public byte[] read(int length){
byte[] bt=new byte[length];
int read=0; //表示已读取长度
java.io.InputStream is=null;
try{
is=new FileInputStream(path); //创建流
int i=0;
while(read!=length&&i!=-1){
i=is.read(bt, read, length-read);
read+=i;
}
}catch(IOException e){
System.out.println(e.getMessage());
}finally{
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
}
//如果读取程度不足length,则对数组进行缩减
if(read!=length){
byte[] nbt=new byte[read];
for(int j=0;j<read;j++)
nbt[j]=bt[j];
return nbt;
}else{
return bt;
}
}
}
测试类如下:
import static org.junit.Assert.*;
import java.io.IOException;
import org.junit.Test;
public class InputStreamTest {
@Test
public void test() {
InputStream is=new InputStream("test.txt");
byte[] bt;
bt = is.read(100);
System.out.println("返回长度"+bt.length);
for(int j=0;j<bt.length;j++)
System.out.println(bt[j]+" ");
}
}
如果,文件存在,则测试正常,然而,文件不存在,则测试失败。
方法过程中会出现IO异常, 方法将不能正常完成,测试流程也将无法完成。
解决方案: 将IO异常抛出给用户,才能保证用户业务流程正确进行。
实现过程2:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class InputStream {
private String path;
public InputStream(String path){
this.path=path;
}
/**
* 读取指定长度字节,并返回
* 如果流内字节数不足,返回最大可读长度字节
* @param length
* @return 字节数组
*/
public byte[] read(int length) throws IOException{
byte[] bt=new byte[length];
int read=0; //表示已读取长度
java.io.InputStream is=null;
is=new FileInputStream(path); //创建流
int i=0;
while(read!=length&&i!=-1){
i=is.read(bt, read, length-read);
read+=i;
}
is.close();
//如果读取程度不足length,则对数组进行缩减
if(read!=length){
byte[] nbt=new byte[read];
for(int j=0;j<read;j++)
nbt[j]=bt[j];
return nbt;
}else{
return bt;
}
}
}
测试类如下:
import static org.junit.Assert.*;
import java.io.IOException;
import org.junit.Test;
public class InputStreamTest {
@Test
public void test() {
InputStream is=new InputStream("test.txt");
byte[] bt;
try {
bt = is.read(100);
System.out.println("返回长度"+bt.length);
for(int j=0;j<bt.length;j++)
System.out.println(bt[j]+" ");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
}
}
测试正常,但是仔细观察输出,则发现,输出少一个字节。
原因及解决方案,仔细检查代码,发现while体内,当文件结束时,i值为-1,此时read会加上-1。
实现过程3:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class InputStream {
private String path;
public InputStream(String path){
this.path=path;
}
/**
* 读取指定长度字节,并返回
* 如果流内字节数不足,返回最大可读长度字节
* @param length
* @return 字节数组
*/
public byte[] read(int length) throws IOException{
byte[] bt=new byte[length];
int read=0; //表示已读取长度
java.io.InputStream is=null;
is=new FileInputStream(path); //创建流
int i=0;
while(read!=length&&i!=-1){
i=is.read(bt, read, length-read);
if(i!=-1) read+=i;
}
is.close();
//如果读取程度不足length,则对数组进行缩减
if(read!=length){
byte[] nbt=new byte[read];
for(int j=0;j<read;j++)
nbt[j]=bt[j];
return nbt;
}else{
return bt;
}
}
}
测试代码:
import static org.junit.Assert.*;
import java.io.IOException;
import org.junit.Test;
public class InputStreamTest {
@Test
public void test() {
InputStream is=new InputStream("test.txt");
byte[] bt;
try {
bt = is.read(100);
System.out.println("返回长度"+bt.length);
for(int j=0;j<bt.length;j++)
System.out.println(bt[j]+" ");
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
}
}
}
结果:测试正常,结果也正常。
当然,笔者的测试类不能算是标准意义上的测试类,因为测试不仅仅是代码逻辑,还有代码功能,正规测试类,则可以测试出结果的正确与错误。
基于以上简单测试,给出笔者总结:
1.认真阅读API,尤其搞清楚输入,输出
2.异常的处理一定要恰当。一般情况下,最好把异常抛出给最终用户。如果捕获了,就要保证有意义的返回。
3.保证程序逻辑的严谨。而这种严谨往往还建立在对API的深入理解。比如,如何保证一定读取到指定字节。
仅抛砖引玉,望高手指点。