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

Java 将字节数组写入文件的快速方法,java,jclouds,java.nio.file,Java,Jclouds,Java.nio.file,在我的应用程序中,我使用Files.write和org.jclouds.blobstore.domain.Blob.putBlob将字节数组写入4MB文件。两者同时进行。第二个选项(jcloud)更快 我想知道是否有一种更快的方法在文件中写入字节数组。如果我实现我的Files.write,它会更好 谢谢我查看了代码,并且(出乎意料地)文件。write(Path,byte[],OpenOption…使用8192字节的固定大小缓冲区写入文件。(Java 7和Java 8版本) 您应该能够通过直接编写

在我的应用程序中,我使用Files.write和org.jclouds.blobstore.domain.Blob.putBlob将字节数组写入4MB文件。两者同时进行。第二个选项(jcloud)更快

我想知道是否有一种更快的方法在文件中写入字节数组。如果我实现我的Files.write,它会更好

谢谢

我查看了代码,并且(出乎意料地)
文件。write(Path,byte[],OpenOption…
使用8192字节的固定大小缓冲区写入文件。(Java 7和Java 8版本)

您应该能够通过直接编写来获得更好的性能;e、 g

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

我做了两个节目。首先使用Files.write,然后使用FileOutputStream创建1000个4MB的文件。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()));
    }
}
公共类TestFileWrite{
公共静态void main(字符串[]args){
试一试{
Path Path=Path.get(“/home/felipe/teste.txt”);
字节[]数据=文件。readAllBytes(路径);
SimpleDataFormat sdf=新的SimpleDataFormat(“YYYY-MM-DD HH:MM:ss”);
System.out.println(“TestFileWrite”);
System.out.println(“开始:+sdf.format(newdate()));
对于(int i=0;i<1000;i++){
file.write(path.get(“/home/felipe/Test/testFileWrite/file”+i+“.txt”),数据);
}
System.out.println(“结束:+sdf.format(newdate()));
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
公共类TestOutputStream{
公共静态void main(字符串[]args){
Path Path=Path.get(“/home/felipe/teste.txt”);
字节[]数据=null;
试一试{
数据=文件。readAllBytes(路径);
}捕获(IOE1异常){
e1.printStackTrace();
}
SimpleDataFormat sdf=新的SimpleDataFormat(“YYYY-MM-DD HH:MM:ss”);
System.out.println(“TestOutputStream”);
System.out.println(“开始:+sdf.format(newdate()));
对于(int i=0;i<1000;i++){
try(OutputStream out=newfileoutputstream(“/home/felipe/Test/testOutputStream/file”+i+“.txt”)){
输出。写入(数据);
}捕获(IOE异常){
e、 printStackTrace();
}
//文件.write(路径.get(“),数据);
}
System.out.println(“结束:+sdf.format(newdate()));
}
}

目前提出的问题过于情境化。您需要提供更多的上下文,使这成为一个特定的问题,最好是使用MCVE,以便我们能够有意义地回答。文件系统缓存有可能使文件写入在数据写入磁盘之前完成。您的基准测试不会考虑到这一点。