Java IO Stream, collection class part small Knowledge point summary

Source: Internet
Author: User
Tags addall

In Java, the following three classes are often used to process data streams, and the following describes the differences between the three classes and their usage.

    • InputStream: is a superclass of all byte input streams, generally using its subclasses: FileInputStream, etc., it can output byte stream;
    • InputStreamReader: It is a bridge between the byte stream and the character stream, can output the character stream as a character stream, and can specify the character set for the byte stream, and can output one of the characters;
    • BufferedReader: Provides a common buffered text read, ReadLine reads a line of text, reads text from the character input stream, buffers individual characters, and provides efficient reading of characters, arrays, and rows.
Example:

Convert bytes to character stream
InputStreamReader reader = new InputStreamReader (new FileInputStream (targetfile));
Provides universal buffering of text reads
BufferedReader br = new BufferedReader (reader);

Iterators (Iterator)
    • (1) Use method iterator () requires the container to return a iterator. The first time you call Iterator's next () method, it returns the first element of a sequence. Note: The iterator () method is an Java.lang.Iterable interface that is inherited by collection.

    • (2) Use Next () to get the next element in the sequence.

    • (3) Use Hasnext () to check if there are elements in the sequence.

    • (4) use remove () to delete the newly returned element of the iterator.

Example:

Iterators for while Loops
Iterator iter = L.iterator ();
while (Iter.hasnext ()) {
String str = (string) iter.next ();
System.out.println (str);
}

list and Listfiles methods in the file class
    • The list () method returns the file names of all files and directories under a directory, and returns a string array
    • The Listfiles () method returns the absolute path to all files and directories in a directory, and returns a file array
List of Add () and AddAll () methods
    • Result.addall (list); //Add each element in the list to result, result.size () = = List.size ()
    • Result.add (list); //Add list as an element to result, then Result.size () is 1

Java IO Stream, collection class part small Knowledge point summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.