Monday, March 30, 2015

Input & output in java

I don't write code that needs to read from files/streams very often, so I am not quite familiar with input/output methods in Java. Especially when I try to review the readN questions on LeetCode, I realize I need to brush up some of the knowledge in this area.

Scanner
The Scanner class can parse primitive types and strings using regular expressions. A Scanner breaks its input into tokens using a delimiter pattern, which by default matches white space. The resulting tokens may then be converted into values of different types using various next methods.

A Scanner can read text from any object which implements the Readable interface. If an invocation of the underlying readable's Readable.read() method throws an IOException then the Scanner assumes the end of the input has been reached. The most recent IOException thrown by the underlying readable can be retrieved via ioException() method.

ByteArrayInputStream
This class reads an input byte array (when initialized).
The read() method reads the next byte of data from this input stream.


Source: http://www.codatlas.com/github.com/openjdk-mirror/jdk7u-jdk/master/src/share/classes/java/io/ByteArrayInputStream.java?keyword=ByteArrayInputStream&line=143
read(byte b[], int off, int len) method, on the other hand, read byte array b to the stream.

Source: http://www.codatlas.com/github.com/openjdk-mirror/jdk7u-jdk/master/src/share/classes/java/io/ByteArrayInputStream.java?keyword=ByteArrayInputStream&line=176


FileReader & FileInputStream
FileReader: reading character files.
read() method uses StreamDecoder's read() method, which reads bytes from the file, and then convert them to chars.
FileInputStream: reading streams of raw bytes.
Both can be used to instantiate a Scanner class.



BufferedReader
Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders.

BufferedReader in = new BufferedReader(new FileReader(fileName));

The above example will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from file, converted into characters, and then returned, which can be very inefficient. 

read() method will read a single character, as an integer in the unicode range. The private method fill() will fill the buff array from input stream if necessary.
Source: http://www.codatlas.com/github.com/openjdk-mirror/jdk7u-jdk/master/src/share/classes/java/io/BufferedReader.java?keyword=bufferedreader&line=170

Private method read1() is the actual implementation of read4() in the LeetCode problem. char[] cbuf is the destination buffer. It will fill the inner buffer array from the input stream, copy it to the destination array, and output the number of characters read.

Source: http://www.codatlas.com/github.com/openjdk-mirror/jdk7u-jdk/master/src/share/classes/java/io/BufferedReader.java?keyword=bufferedreader&line=195

Ok, here comes the actual read(char cbuf[], int off, int len) method which, if you throw away the int off (the offset of the starting point), is pretty much the readN() implementation.
This method iteratively calling the read1() method, until len character is read. 

Source: http://www.codatlas.com/github.com/openjdk-mirror/jdk7u-jdk/master/src/share/classes/java/io/BufferedReader.java?keyword=bufferedreader&line=269

readLine(), which is the most common method used. Fill the buffer array (char[] cb), and return a String. Reading is terminated by a line feed('\n') or a carriage return ('\r').





1 comment:


  1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. IEEE final year projects on machine learning In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete