package Security;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.io.Reader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.nio.file.*;
public class Filer {
long bits=0;//文件流需要跳过的字节数
long bits1=0;//每次readLine()读取行的字节数
long begin=0;//第一个读取的交易时间的分钟差
long end=0;//最后读取的交易时间的分钟差
//字符串样式的时间转化成时间戳,并计算分钟差
static String adress=null;
public Filer(String adress){
this.adress =adress;
}
public Filer(){
}
public static long Datestr_int(String dateStr) {
String format="HH:mm:ss";
try {
SimpleDateFormat sdf = new SimpleDateFormat(format);
return (sdf.parse(dateStr).getTime() / 60000);
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
public ArrayList<Listr> readTransactions(){
if(bits==-1){
// JOptionPane.showMessageDialog(null, "区块已创建完。");
//break;
return null;
}
File fRead=new File(adress);
int n=1;//记录位置,如交易时间为1,转出人为2,收款人为3
try {
Reader out=new FileReader(fRead);
BufferedReader bufferedReader=new BufferedReader(out);
bufferedReader.skip(bits);//System.out.println("N"+bits);
String str=null;//存储每次读取的行
ArrayList<Listr> temp = new ArrayList<Listr>();//存储交易记录
StringTokenizer fenxi;
while((str=bufferedReader.readLine())!=null){
Listr t = new Listr();
String s="";//存储对str剪、拼过后的字符串
//System.out.println(str);//
bits1=str.length()+2;
bits=bits+bits1;
//System.out.println(bits);
if(str.startsWith("交易时间")){
str=bufferedReader.readLine();//去掉第一行(数据类别名)
//System.out.println("no");
bits1=str.length()+2;
bits=bits+bits1;
//System.out.println(bits);System.out.println(bits1);
}
fenxi=new StringTokenizer(str);
while(fenxi.hasMoreTokens()){
s = fenxi.nextToken();
//提取交易时间
switch(n){
case 1:t.time = s;
if(begin==0) begin=Datestr_int(s);
else end=Datestr_int(s);
break;
case 2:t.outman = s;break;
case 3:t.inman = s;break;
case 4:t.bitcoin = s;break;
}
n++;
}
n=1;
//判断是否超过10分钟,超过返回tempTxList,注意最后一行未添加进列表
/*if(end!=0&&end-begin>10){
begin=end;
//System.out.println(s);
//System.out.println(bits);System.out.println(bits1);
bits=bits-bits1;
bufferedReader.close();
return temp;
//break;
}*/
temp.add(t);
}
bufferedReader.close();
out.close();
//文件读完,为了避免界面不响应操作,使读取从头开始
//改
if(str==null){
// //tempTxList.add("文件已经读完,从头开始");
bits=-1;
// bits1=0;
// begin=0;
// end=0;
}
return temp;
} catch (IOException e) {
e.printStackTrace();
System.out.println(e.toString());
}
return null;
}
public static void writeObjectFormFile(Block block ,String hash){
File file = new File("D:\\区块文件\\"+hash+".txt");
//File file = new File("D:\\区块文件\\"+hash+".dat");
FileOutputStream out;
try {
out = new FileOutputStream(file);
ObjectOutputStream objOut=new ObjectOutputStream(out);
objOut.writeObject(block);
objOut.close();
} catch (IOException e) {
System.out.println("Write object failed!");
e.printStackTrace();
}
}
public static Block readObjectFormFile(String hash){
Block temp=null;
File file = new File("D:\\区块文件\\"+hash+".txt");
//File file = new File("D:\\区块文件\\"+hash+".dat");
FileInputStream in;
try {
in = new FileInputStream(file);
ObjectInputStream objIn=new ObjectInputStream(in);
temp=(Block) objIn.readObject();
objIn.close();
} catch (IOException e) {
System.out.println("read object failed!");
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return temp;
}
public static void writeLastHash(String hash){
File file = new File("D:\\区块文件\\LastHash.txt");
FileOutputStream out;
try {
out = new FileOutputStream(file);
PrintStream ps = new PrintStream(out);
ps.print(hash);// 往文件里写入字符串
} catch (IOException e) {
System.out.println("Write LastHash failed!");
e.printStackTrace();
}
}
public static String readLastHash(){
String temp="";
File file = new File("D:\\区块文件\\LastHash.txt");
FileInputStream in;
try {
in = new FileInputStream(file);
byte[] buffer=new byte[64];
in.read(buffer);
in.close();
temp=new String(buffer,"GB2312");
} catch (IOException e) {
System.out.println("read LastHash failed!");
e.printStackTrace();
}
return temp;
}
public static void writeSize(int size){
File file = new File("D:\\区块文件\\Size.txt");
FileOutputStream out;
try {
out = new FileOutputStream(file);
PrintStream ps = new PrintStream(out);
ps.print(size+"#");// 往文件里写入字符串
} catch (IOException e) {
System.out.println("Write Size failed!");
e.printStackTrace();
}
}
public static int readSize(){
String temp="";
int i = 0;
File file = new File("D:\\区块文件\\Size.txt");
FileInputStream in;
try {
in = new FileInputStream(file);
int size=in.available();
byte[] buffer=new byte[size];//size为字串的长度 ,这里一次性读完
in.read(buffer);
in.close();
temp=new String(buffer,"GB2312");
temp=(temp.split("#"))[0];
} catch (IOException e) {
System.out.println("Read Size failed!");
e.printStackTrace();
}
try{
i = Integer.parseInt(temp);
}catch(NumberFormatException e1){
System.out.println("wrong");
}
return i;
}
}
部分区块链底层核心技术的初步实现
需积分: 14 135 浏览量
2017-07-12
08:53:14
上传
评论
收藏 17KB ZIP 举报
xiaoxingdi
- 粉丝: 8
- 资源: 3
最新资源
- NetOps-py通过sftp替换网络设备启动文件
- STM32单片机FPGA毕设电路原理论文报告任务驱动教学法在单片机课程教学中的应用
- STM32单片机FPGA毕设电路原理论文报告任务驱动法在单片机教学中的应用
- STM32单片机FPGA毕设电路原理论文报告人造金刚石压机智能化压力测控系统设计
- 以某列为依据匹配多项(Excel版)
- STM32单片机FPGA毕设电路原理论文报告人体短臂离心机实验台的显示控制系统
- STM32单片机FPGA毕设电路原理论文报告人工气候室监控系统的环境控制器研究
- STM32单片机FPGA毕设电路原理论文报告染整自动线张力控制系统的设计
- 数据挖掘与机器学习-实验
- 基于Linux系统Nginx的动态网站的LNMP环境源码包
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈