Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.File;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Locale;
- import org.bitcoinj.core.*;
- import org.bitcoinj.params.MainNetParams;
- import org.bitcoinj.utils.BlockFileLoader;
- public class BitcoinReader{
- //This is the path where my blk*.dats can be found
- static String PATH = "d:/bitcoin_blockchain/blocks/";
- public void read() {
- // Just some initial setup
- NetworkParameters np = new MainNetParams();
- Context.getOrCreate(MainNetParams.get());
- // We create a BlockFileLoader object by passing a list of files.
- // The list of files is built with the method buildList(), see
- // below for its definition.
- BlockFileLoader loader = new BlockFileLoader(np,buildList());
- int blockCounter = 0;
- try{
- for (Block block : loader) {
- blockCounter++;
- // There you can see the progress
- System.out.println("Block #"+blockCounter);
- //i get the exception after this block #481815
- if(blockCounter == 481815){
- System.out.println("Debugging");
- }
- }
- } catch (Exception e){
- e.printStackTrace();
- }
- }
- // The method returns a list of files in a directory according to a certain
- // pattern (block files have name blkNNNNN.dat)
- private List<File> buildList() {
- List<File> list = new LinkedList<File>();
- for (int i = 0; true; i++) {
- File file = new File(PATH + String.format(Locale.US, "blk%05d.dat", i));
- if (!file.exists())
- break;
- list.add(file);
- }
- return list;
- }
- // Main method: simply invoke everything
- public static void main(String[] args) {
- BitcoinReader br = new BitcoinReader();
- br.read();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement