当前位置:编程学习 > JAVA >>

有用过Java工具类 Skwish 的人不?

Skwish 是一个轻量级的Java类库,用来将二进制数据保存到文件系统中。允许对文件进行并发的读写,该类库设计的目的是为了防止异常错误、关键、程序退出导致的文件损坏。

 

 

 如何使用!!!!

答案:package com.faunos.skwish.eg;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.List;

import com.faunos.skwish.SegmentStore;
import com.faunos.skwish.TxnSegment;
import com.faunos.skwish.ext.http.SkwishHttpMountPoint;
import com.faunos.skwish.ext.http.SkwishHttpServer;
import com.faunos.util.io.BufferUtil;
import com.faunos.util.io.file.DirectoryOrdering;
import com.faunos.util.io.file.FileSystemTraverser;
import com.faunos.util.main.Arguments;
import com.faunos.util.test.AbbreviatedFilepath;
import com.faunos.util.tree.TraverseListener;


public class FileStore {


    public final static String HELP_OPT = "h";
   
    public final static String CREATE_OPT = "c";
   
    public final static String WEB_OPT = "w";

 

    public static void main(String[] args) throws Exception {
        try {
            mainImpl(args);
        } catch (Exception e) {
            printUsage(System.err);
            printCurrentDir(System.err);
            throw e;
        }
    }
   
   
   

    private static void mainImpl(String[] args) throws Exception {
        Arguments arguments = new Arguments();
        arguments.getOptions().getIdentifiers().add(HELP_OPT);
        arguments.getOptions().getIdentifiers().add(CREATE_OPT);
        arguments.getOptions().getIdentifiers().add(WEB_OPT);
        arguments.parse(args);
        if (arguments.getOptionList().contains(HELP_OPT)) {
            printUsage(System.out);
            return;
        }
        List<String> filepaths = arguments.getArgumentList();
        if (filepaths.size() < 1)
            throw new IllegalArgumentException(filepaths.toString());
       
        File storageDir = new File(filepaths.get(0));
        final boolean create = arguments.getOptionList().contains(CREATE_OPT);
        if (create)
            storageDir.mkdirs();
        if (!storageDir.isDirectory())
            throw new IllegalArgumentException(
                    storageDir + " is not a directory");
       
        final SegmentStore cache, meta;
        {
            File cacheDir = new File(storageDir, "cache");
            File metaDir  = new File(storageDir, "meta");
            if (create) {
                cache = SegmentStore.writeNewInstance(cacheDir.getPath());
                meta = SegmentStore.writeNewInstance(metaDir.getPath());
            } else {
                cache = SegmentStore.loadInstance(cacheDir.getPath());
                meta = SegmentStore.loadInstance(metaDir.getPath());
            }
        }
        SkwishHttpServer http;
        if (arguments.getOptionList().contains(WEB_OPT)) {
            http = SkwishHttpServer.newInstance(8880);
            SkwishHttpMountPoint mountPoint = http.newSkwishMountPoint("/");
            mountPoint.mapRelativeUriToSkwish("cache", cache);
            mountPoint.mapRelativeUriToSkwish("meta", meta);
            http.start();
        } else
            http = null;
       
        for (int i = 1; i < filepaths.size(); ++i) {
            FileImporter importer
                = new FileImporter(cache, meta, new File(filepaths.get(i)));
            new Thread(importer).start();
        }
       
        try { Thread.sleep(120000); } catch (InterruptedException ix) { }
    }
   
    private static void printUsage(PrintStream ps) {
    }
   
    private static void printCurrentDir(PrintStream ps) throws IOException {
        File currentDir = new File(".").getCanonicalFile();
        ps.println("current dir: " + new AbbreviatedFilepath(currentDir));
        ps.println("    " + currentDir);
        ps.println();
    }
   
   
   
    static class FileImporter implements Runnable {
       
        private final TxnSegment cacheTxn;
        private final long cacheTxnId;
        private final TxnSegment metaTxn;
        private final File root;
        private final String parentFilepath;
        private final boolean escapeBackslash;
       
        private int count;
       
&nb

上一个:java 的JVM虚拟内存是如何管理的啊。
下一个:用JAVA或C#编一个在线测试的程序

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,