What is preferred for file I/O in Java? [closed]

Of the three

  • BufferedReader/Writer
  • Binary Streams
  • Channels
  • which is more preferred in real life for file I/O in Java and why?
    I prefer channels for binary data to be put into file or read from and BufferedReader/Writer for String data

    java.io uses Streams while java.nio use Channels and buffer, The most important distinction between java.io way and java.nio way is, how data is packaged and transmitted.java.io deals with data in streams (one byte at a time), whereas java.nio deals with data in blocks.
    Processing data by the block can be much faster than processing it by the (streamed) byte but it is not as easy as streams.

    In these situation, it is better to use java.io way:

    • Files may be large but not huge. It’s possible to read an entire file into memory.
    • An application reads from or writes to only a few files or network connections at the same time, ideally using only one stream at a time.
    • The application is sequential. It won’t be able to do much until it’s finished reading or writing a file.

    In these situation, it is better to use java.nio way:

    • An application that need to service hundreds or thousands of connections simultaneously.

    You can see the following links for further information:

    http://www.skill-guru.com/blog/2010/11/14/java-nio-vs-java-io-which-one-to-use/

    http://blogs.oracle.com/slc/entry/javanio_vs_javaio

    https://www.ibm.com/developerworks/java/tutorials/j-nio/

    http://java.dzone.com/articles/java-nio-vs-io

    2

    which is more preferred in real life for file I/O in Java and why?

    It should really be a matter of function rather than preference.

    • You use Readers and Writers for character oriented data (which requires encoding and decoding), Input/OutputStreams for byte-oriented data, and Binary streams if the data encodes binary data.

    • You use the BufferedXxx classes if you need to I/O at the granularity of a character or byte. They are a simple way to avoid the serious performance problems that can occur if you do lots of byte-at-a-time read and write syscalls.

    • If you only do I/O by reading / writing large chunks, then the BufferedXxx classes might might actually reduce throughput a bit. You can either use plain readers/writers/streams (with big char[] or byte[] buffers), or Channels and XxxBuffers. The latter are potentially faster, but the APIs are more awkward.

    • You use Channels if you need low overheads, and/or do specialised things such as “selecting” from multiple streams, scatter / gather, and memory mapped file access.

    As you can see, the different APIs is designed for different purposes. In some use-cases there is overlap, in others only one API is fit for the purpose.


    The problem with developing a “preference” for one style of I/O over another is that it leads to bad software; i.e. either code that is inefficient where I/O efficiency is needed, or overly complicated where I/O efficiency should not be a concern.

    3

    The answer is “It depends” – Buffered, Streaming, Blocking and Non-blocking I/O are all used for different purposes depending on what concurrency, throughput and other performance characteristics that you’re after.

    Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
    Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
    Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật