I/O streams in java

Java IO at a Glance

Java I/O Streams – Overview File Copying Semantics of File copying – FileInputStream and FileOutputStream Skipping a few bytes in a file and reading File copying – Using FileReader & FileWriter Reading data types using DataInputStream and DataOutputStream Merging of files – SequenceInputStream Reading and writing sequentially and randomly –…

StringWriter vs StringBuilder

StringWriter vs StringBuilder given in simple terms for Beginner. Even though, some string word is common in both of them (like Java and JavaScript), both are very different in their functionalities. We cannot compare them because both are very different from different packages. 1. StringWriter StringWriter from java.io package is…

Keyboard Reading Java

To interact with a running program, that is to pass data for computation like user name, password or Employee particulars or Bank account credit and debit operations, two styles exist in a language like Java. Through Keyboard: It is very common input for practicing a language (but not in realtime).…

Java File Copying

1. Semantics of File copying – FileInputStream and FileOutputStream 2. Using FileReader & FileWriter 3. Rereading a file – Using markSupported(), mark() & reset() 4. Skipping a few bytes in a file and reading 5. Reading data types using DataInputStream and DataOutputStream 6. Reading and writing sequentially and randomly –…

InputStreamReader Java

InputStreamReader and OutputStreamWriter are two IO Streams linking byte streams with character streams. They are known as wrapper streams. 1. What is a Wrapper Stream? It is a good question not very clear to a Learner. I put it elaborately. Like we have wrapper classes which wraps primitive data types…

Java IO Special Programs

1. To find number of lines in a file 2. Adding line numbers to a file – LineNumberInputStream, LineNumberReader 3. PrintStream vs PrintWriter 4. Byte streams vs Character streams 5. Byte streams and Character streams – file coying using FileReader and FileWriter 6. Pushing out Character – PushbackReader 7. Piping…

Number of Lines in File

Sometimes, in coding, it is required to know the number of lines existing in a file programmatically. There is no direct way (a predefined method) to find out in Java. To do the job, the help of BufferedReader's readLine() method is taken. While reading each line, the counter is incremented;…

File Copying FileReader FileWriter

We have seen earlier, clearly the style and basics of file copying in Java, with byte streams FileInputStream and FileOutputStream in "Semantics of File Copying" and the it&339;s drawbacks of file copying in "Performance drawbacks of file copying". Now let us do the same job but with character streams –…

markSupported() mark() reset() Java

Rereading from the Same Position The same contents of a file can be again and again read by marking the position with the methods of StringBufferInputStream, inherited from its super class InputStream. A file can be reread (reading again) from a particular location. Here, the file position is marked; execution…

Performance drawbacks File copying

Performance drawbacks File copying Summary: By the end of this tutorial "Performance drawbacks File copying", you will come to know the reasons of slow performance in file copying. Gives links to other programs to increase speed of copying. Performance drawbacks of FileInputStream and FileOutputStream Following is the for loop used…

Read bytes ByteArrayInputStream

Read bytes ByteArrayInputStream Summary: By the end of this tutorial "Read bytes ByteArrayInputStream", you will come to know to read a byte array. The byte array can be read fully or a few elements. Two separate programs given. This class is very useful to read data from byte arrays. For…

StringBufferInputStream Example

StringBufferInputStream Example: As in the earlier program, where the source of reading is a byte array in case of ByteArrayInputStream, now the source is a string buffer in case of StringBufferInputStream. That is, StringBufferInputStream reads from a string buffer (or the other way, string). StringBufferInputStream class methods and constructors are…

ByteArrayOutputStream

The class ByteArrayOutputStream does just the opposite functionality of ByteArrayInputStream. The ByteArrayInputStream reads data from the array and now the ByteArrayOutputStream writes the data to the byte array. All the characters of the array can be written or a few also. In the following program, both functionalities are illustrated.

CharArrayWriter Java

Like StringWriter stores data in a StringBuffer, the CharArrayWriter stores data in a character array. The array size increases implicitly when more data is added. The data stored with CharArrayWriter object can be obtained using toString() or toCharArray() methods. The ealier closing of CharArrayWriter object does not have any affect…