//把F:/test/distribute.sql这个文件读入到byte数组中,返回这个数组
package com.cus.systemFunction.disCom;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import org.apache.log4j.Logger;
/**
* 说明:把文件读到byte数组
* @author xxx
* 创建时间:2010-8-5 下午01:21:33
*/
public class FileToByteArray {
static Logger logger = Logger.getLogger(FileToByteArray.class.getName());
String outDir = "";
//getByteArray() 方法返回一个byte数组
public byte[] getByteArray() throws Exception {
// String path= getClass().getResource("/").getPath();
// outDir = path.trim().substring(1,path.length()-5)+"outputDir/generator/sqlScript/";
//BufferedInputStream in = new BufferedInputStream(new FileInputStream(outDir+"distribute.sql"));
BufferedInputStream in = new BufferedInputStream(new FileInputStream("F://test//distribute.sql"));
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
System.out.println("Available bytes:" + in.available());
byte[] temp = new byte[1024];
int size = 0;
while ((size = in.read(temp)) != -1) {
out.write(temp, 0, size);
}
in.close();
byte[] content = out.toByteArray();
System.out.println("Readed bytes count:" + content.length);
return content;
}
}
//把上面返回的这个byte数组的内容,读入到C盘目录下的distributeCopy.sql文件中
package com.cus.systemFunction.disCom;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
public class TestMain {
public static void main(String args[]) throws Exception{
FileToByteArray fileToByteArray = new FileToByteArray();
byte[] buffer = fileToByteArray.getByteArray();
FileOutputStream outf=new FileOutputStream("C://distributeCopy.sql");
BufferedOutputStream bufferout= new BufferedOutputStream(outf);
bufferout.write(buffer);
bufferout.flush();
bufferout.close();
}
}