简体   繁体   English

将字节数组写入文件的快速方法

[英]Fast way to write a byte array into a file

On my app I use Files.write and org.jclouds.blobstore.domain.Blob.putBlob to write a byte array into 4MB files. 在我的应用程序上,我使用Files.write和org.jclouds.blobstore.domain.Blob.putBlob将字节数组写入4MB文件。 Both in a concurrent way. 两者以并发方式进行。 The second option (jcloud) is faster. 第二个选项(jcloud)更快。

I wounder to know if there is a faster way to write a byte array int a file. 我很想知道是否有更快的方法将字节数组写入文件。 If I implement my Files.write it is better. 如果我实现我的Files.write更好。

Thanks 谢谢

I looked at the code, and (surprisingly) Files.write(Path, byte[], OpenOption ...) writes the file using a fixed sized buffer of 8192 bytes. 我看了一下代码,(令人惊讶的是) Files.write(Path, byte[], OpenOption ...)使用8192字节的固定大小的缓冲区写入文件。 (Java 7 & Java 8 versions) (Java 7和Java 8版本)

You should be able to get better performance by doing the write directly; 您应该可以通过直接执行写入来获得更好的性能。 eg 例如

    byte[] bytes = ...
    try (FileOutputStream fos = new FileOutputStream(...)) {
        fos.write(bytes);
    }

I did two programs. 我做了两个程序。 First using Files.write and second Using FileOutputStream to create 1000 files of 4MB. 首先使用Files.write,然后使用FileOutputStream创建1000个4MB的文件。 Files.write took 47 sec and FileOutputStream 53 sec. Files.write花了47秒,FileOutputStream花了53秒。

public class TestFileWrite {

    public static void main(String[] args) {
        try {
            Path path = Paths.get("/home/felipe/teste.txt");
            byte[] data = Files.readAllBytes(path);

            SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
            System.out.println("TestFileWrite");
            System.out.println("start: " + sdf.format(new Date()));
            for (int i = 0; i < 1000; i++) {
                Files.write(Paths.get("/home/felipe/Test/testFileWrite/file" + i + ".txt"), data);
            }
            System.out.println("end: " + sdf.format(new Date()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



public class TestOutputStream {

    public static void main(String[] args) {
        Path path = Paths.get("/home/felipe/teste.txt");
        byte[] data = null;
        try {
            data = Files.readAllBytes(path);
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
        System.out.println("TestOutputStream");
        System.out.println("start: " + sdf.format(new Date()));

        for (int i = 0; i < 1000; i++) {
            try (OutputStream out = new FileOutputStream("/home/felipe/Test/testOutputStream/file" + i + ".txt")) {
                out.write(data);
            } catch (IOException e) {
                e.printStackTrace();
            }
            // Files.write(Paths.get("), data);
        }
        System.out.println("end: " + sdf.format(new Date()));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM