/**
* 针对大文件存储,请依次调用beginSave、AddSave、endSave。
*
* @author CK
*
*/
public class DataUtil {
DataOutputStream BinaryOut=null;
BufferedWriter TextOut=null;
String FilePath=null;
enum SaveFileType{Text,Binary};
SaveFileType SaveFileType;
/**
* double转byte[]
*
* @param d
* @return
*/
public static byte[] double2Bytes(double d) {
long value = Double.doubleToRawLongBits(d);
byte[] byteRet = new byte[8];
for (int i = 0; i < 8; i++) {
byteRet[i] = (byte) ((value >> 8 * i) & 0xff);
}
return byteRet;
}
/**
* byte[]转double
*
* @param arr
* @return
*/
public static double bytes2Double(byte[] arr) {
long value = 0;
for (int i = 0; i < 8; i++) {
value |= ((long) (arr[i] & 0xff)) << (8 * i);
}
return Double.longBitsToDouble(value);
}
/**
* 大型数据存储之开始存储
* @param FilePath 文件路径
* @param saveFileType 保存的文件类型,文本文件、双精度所存的二进制文件
* @return
* @throws IOException
*/
public boolean BeginSave(String FilePath,SaveFileType saveFileType) throws IOException {
if (FilePath == "" || FilePath == null) {
System.out.println("the SavePath is null.");
return false;
}
this.FilePath=FilePath;
this.SaveFileType=saveFileType;
File dataFile = new File(FilePath);
if (!dataFile.getParentFile().exists()) {
dataFile.getParentFile().mkdirs();
}
if (dataFile.exists()) {
dataFile.delete();
}
dataFile.createNewFile();
switch(this.SaveFileType){
case Text:
TextOut= new BufferedWriter(new FileWriter(dataFile,true));
break;
case Binary:
BinaryOut = new DataOutputStream(new FileOutputStream(dataFile,true));
break;
default:
break;
}
return true;
}
/**
* 大型文件存储之追加存储
* @param DataStr 若是文本存储则无要求,若是双精度的二进制文件,以若干空格隔开
* @return
* @throws IOException
*/
public boolean AddSave(String DataStr) throws IOException{
switch(this.SaveFileType){
case Text:
this.TextOut.append(DataStr);
break;
case Binary:
DataStr=DataStr.trim();
String[] dataArray=DataStr.split("\\s+");
for(int i=0;i<dataArray.length;i++){
this.BinaryOut.write(double2Bytes(Double.parseDouble(dataArray[i])));
}
break;
default:
break;
}
return true;
}
/**
* 大型文件存储之结束保存,清空缓存、关闭文件。
* @return
* @throws IOException
*/
public boolean EndSave() throws IOException{
switch(this.SaveFileType){
case Text:
this.TextOut.flush();
this.TextOut.close();
break;
case Binary:
this.BinaryOut.flush();
this.BinaryOut.close();
break;
default:
break;
}
return true;
}
/**
* 将字符串保存为文本文件(一次完成)
*
* @param DataStr
* 文件内容
* @param SavePath
* 文件路径,包含文件名、后缀
* @return
* @throws IOException
*/
public boolean saveTextFile(String DataStr, String SavePath)
throws IOException {
if (DataStr == "" || DataStr == null) {
System.out.println("the dataStr is null.");
return false;
}
if (SavePath == "" || SavePath == null) {
System.out.println("the SavePath is null.");
return false;
}
File dataFile = new File(SavePath);
if (!dataFile.getParentFile().exists()) {
dataFile.getParentFile().mkdirs();
}
if (dataFile.exists()) {
dataFile.delete();
}
dataFile.createNewFile();
BufferedWriter out;
out = new BufferedWriter(new FileWriter(dataFile));
out.append(DataStr);
out.flush();
out.close();
return true;
}
/**
* 双精度存为二进制数据(一次存储)
*
* @param DataStr 双精度数据组成的字符串,以若干空格隔开
* @param OutputPath
* @return
* @throws IOException
*/
public boolean saveBinaryFile(String DataStr, String OutputPath) throws IOException {
if (DataStr == "" || DataStr == null) {
System.out.println("the dataStr is null.");
return false;
}
if (OutputPath == "" || OutputPath == null) {
System.out.println("the OutputPath is null.");
return false;
}
File dataFile = new File(OutputPath);
if (!dataFile.getParentFile().exists()) {
dataFile.getParentFile().mkdirs();
}
if (dataFile.exists()) {
dataFile.delete();
}
dataFile.createNewFile();
DataOutputStream out;
out = new DataOutputStream(new FileOutputStream(dataFile));
// 数据处理
DataStr=DataStr.trim();
String[] dataArray=DataStr.split("\\s+");
for(int i=0;i<dataArray.length;i++){
out.write(double2Bytes(Double.parseDouble(dataArray[i])));
}
out.flush();
out.close();
return true;
}
}
/**
* 测试二进制大文件读写(200M左右)
* @author ck
*
*/
public class FileTest {
static String inputFilePath=""; //输入文件路径,包含文件名后缀
static String outputFilePath=""; //输出文件名,包含文件名后缀
public static void file2file() throws IOException{
DataUtil dataUtil=new DataUtil();
DataInputStream br=new DataInputStream(
new BufferedInputStream(
new FileInputStream(inputFilePath)));
dataUtil.BeginSave(outputFilePath, SaveFileType.Text); //初始化,创建文件,采用文件追加存储的思路
byte[] oneData=new byte[8];
int i=0,count =0 ;
while(br.read(oneData, 0, 8)!=-1){
i=i+1;
dataUtil.AddSave(String.valueOf(DataUtil.bytes2Double(oneData)));
if(i/23543==0){
count++;
System.out.println(count+"\n");
}
}
dataUtil.EndSave(); //将还在缓存中的数据写入到文件中,关闭文件。
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有