Best way to write String to file using java nio

后端 未结 5 1923
清歌不尽
清歌不尽 2021-01-30 12:49

I need to write(append) huge string to flat file using java nio. The encoding is ISO-8859-1.

Currently we are writing as shown below. Is there any better

5条回答
  •  情话喂你
    2021-01-30 13:50

    I don't think you will be able to get a strict answer without benchmarking your software. NIO may speed up the application significantly under the right conditions, but it may also make things slower. Here are some points:

    • Do you really need strings? If you store and receive bytes from you database you can avoid string allocation and encoding costs all together.
    • Do you really need rewind and flip? Seems like you are creating a new buffer for every string and just writing it to the channel. (If you go the NIO way, benchmark strategies that reuse the buffers instead of wrapping / discarding, I think they will do better).
    • Keep in mind that wrap and allocateDirect may produce quite different buffers. Benchmark both to grasp the trade-offs. With direct allocation, be sure to reuse the same buffer in order to achieve the best performance.
    • And the most important thing is: Be sure to compare NIO with BufferedOutputStream and/or BufferedWritter approaches (use a intermediate byte[] or char[] buffer with a reasonable size as well). I've seen many, many, many people discovering that NIO is no silver bullet.

    If you fancy some bleeding edge... Back to IO Trails for some NIO2 :D.

    And here is a interesting benchmark about file copying using different strategies. I know it is a different problem, but I think most of the facts and author conclusions also apply to your problem.

    Cheers,

    UPDATE 1:

    Since @EJP tiped me that direct buffers wouldn't be efficient for this problem, I benchmark it myself and ended up with a nice NIO solution using nemory-mapped files. In my Macbook running OS X Lion this beats BufferedOutputStream by a solid margin. but keep in mind that this might be OS / Hardware / VM specific:

    public void writeToFileNIOWay2(File file) throws IOException {
        final int numberOfIterations = 1000000;
        final String messageToWrite = "This is a test üüüüüüööööö";
        final byte[] messageBytes = messageToWrite.
                getBytes(Charset.forName("ISO-8859-1"));
        final long appendSize = numberOfIterations * messageBytes.length;
        final RandomAccessFile raf = new RandomAccessFile(file, "rw");
        raf.seek(raf.length());
        final FileChannel fc = raf.getChannel();
        final MappedByteBuffer mbf = fc.map(FileChannel.MapMode.READ_WRITE, fc.
                position(), appendSize);
        fc.close();
        for (int i = 1; i < numberOfIterations; i++) {
            mbf.put(messageBytes);
        }
    } 
    

    I admit that I cheated a little by calculating the total size to append (around 26 MB) beforehand. This may not be possible for several real world scenarios. Still, you can always use a "big enough appending size for the operations and later truncate the file.

    UPDATE 2 (2019):

    To anyone looking for a modern (as in, Java 11+) solution to the problem, I would follow @DodgyCodeException's advice and use java.nio.file.Files.writeString:

    String fileName = "/xyz/test.txt";
    String messageToWrite = "My long string";
    Files.writeString(Paths.get(fileName), messageToWrite, StandardCharsets.ISO_8859_1);
    

提交回复
热议问题