On my app I use Files.write and org.jclouds.blobstore.domain.Blob.putBlob to write a byte array into 4MB files. Both in a concurrent way. The second option (jcloud) is faster.
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.
Thanks
I looked at the code, and (surprisingly) Files.write(Path, byte[], OpenOption ...)
writes the file using a fixed sized buffer of 8192 bytes. (Java 7 & Java 8 versions)
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 took 47 sec and FileOutputStream 53 sec.
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()));
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.