How To Convert String To InputStream in Java?

How To Convert String To InputStream in Java?
DeeKay April 29, 2011 at 2:28 am · java.io

Below Java code show how to convert String to InputStream using ByteArrayInputStream class – /********************************************************************************** * Created on 04-2011 Copyright(c) http://kodehelp.com All Rights Reserved. **********************************************************************************/ package com.kodehelp.javaio; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.UnsupportedEncodingException; /** * Created by http://kodehelp.com * Date: 4/28/11 */ public class StringToInputStream { public static void main(String[] args){ String text = “Converting…
Continue Reading »


How To Read Text File in Java?

How To Read Text File in Java?
DeeKay April 29, 2011 at 1:58 am · java.io

Below code example shows how to read the text file in java. First the file is read into inputstream. /********************************************************************************** * Created on 04-2011 Copyright(c) http://kodehelp.com All Rights Reserved. **********************************************************************************/ package com.kodehelp.javaio; import java.io.*; /** * Created by http://kodehelp.com * Date: 4/28/11 */ public class ReadTextFile { public static void main(String[] args) throws Exception{ InputStream…
Continue Reading »


How To Convert InputStream to String in Java

How To Convert InputStream to String in Java
DeeKay April 29, 2011 at 12:55 am · java.io

Below Java code show how to read the file in InputStream and convert the inputStream to String. In this example the file used (“TEST.txt”) is kept in the root directory of your project. /********************************************************************************** * Created on 04-2011 Copyright(c) http://kodehelp.com All Rights Reserved. **********************************************************************************/ package com.kodehelp.javaio; import java.io.*; /** * Created by http://kodehelp.com * Date:…
Continue Reading »


How To Get Extension of a File in Java?

How To Get Extension of a File in Java?
DeeKay April 29, 2011 at 12:23 am · java.io

Below Java code example show how to retrieve the extension of File using Java File object – /********************************************************************************** * Created on 04-2011 Copyright(c) http://kodehelp.com All Rights Reserved. **********************************************************************************/ package com.kodehelp.javaio; import java.io.File; /** * Created by http://kodehelp.com * Date: 4/28/11 */ public class ExtensionOfFile { public static final String DOT_SEPARATOR = “.”; public static void…
Continue Reading »


Java Code Example for BufferedReader to read Character InputStream

Java Code Example for BufferedReader to read Character InputStream
DeeKay April 18, 2011 at 11:53 pm · java.io

BufferedReader class of Java API provides a way to read the character inputstream, by buffering the characters which provides an efficient way to read the characters, arrays and lines. The buffer size may be specified, or the default size may be used. The default is large enough for most purposes. In general, each read request…
Continue Reading »


Java Code Example for BufferedOutputStream : flush() Method

Java Code Example for BufferedOutputStream : flush() Method
DeeKay April 18, 2011 at 8:59 pm · java.io

flush() method of BufferedOutStream flushes this buffered output stream. This forces any buffered output bytes to be written out to the underlying output stream. Overrides: flush in class FilterOutputStream Throws: IOException – if an I/O error occurs. Below code example show how the output stream is flushed and writes out the output stream. /********************************************************************************** *…
Continue Reading »


Java Code Example for BufferedOutputStream : write(byte[]b, int off, int len) Method

Java Code Example for BufferedOutputStream : write(byte[]b, int off, int len) Method
DeeKay April 18, 2011 at 8:37 pm · java.io

BufferedOutputStream’s write(byte[]b,int off,int len) method writes len bytes from specified array of byte starting from offset off to the outputstream. This method stores bytes from the given array into this stream’s buffer, flushing the buffer to the underlying output stream as needed. If the requested length is at least as large as this stream’s buffer,…
Continue Reading »


Java Code Example for BufferedOutputStream : write(int b) Method

Java Code Example for BufferedOutputStream : write(int b) Method
DeeKay April 18, 2011 at 8:23 pm · java.io

BufferedOutputStream’s write(int b) method overrides write(int b) method of FilterOutputStream. This method writes the specified byte to this output stream. The write method of FilterOutputStream calls the write method of its underlying output stream, that is, it performs out.write(b). Implements the abstract write method of OutputStream. Parameters: b – the byte. Throws: IOException – if…
Continue Reading »


Java Code Example for BufferedOutputStream to Write OutputStream

Java Code Example for BufferedOutputStream to Write OutputStream
DeeKay March 30, 2011 at 12:55 am · java.io

BufferedOutputStream can be setup to write the bytes to the underlying output stream without necessarily causing call to the underlying system for each byte written. BufferedOutputStream class has below two constructor : BufferedOutputStream(OutputStream out) – Creates a new buffered output stream to write data to the specified underlying output stream. BufferedOutputStream(OutputStream out, int size) –…
Continue Reading »


Java Code Example for BufferedInputStream : markSupported() Method

Java Code Example for BufferedInputStream : markSupported() Method
DeeKay March 29, 2011 at 12:14 am · java.io

Tests if this input stream supports the mark and reset methods. The markSupported method of BufferedInputStream returns true. Below is the code example for BufferedInputStream markSupported() method –


Java Code Example for BufferedInputStream : mark() method

Java Code Example for BufferedInputStream : mark() method
DeeKay March 28, 2011 at 8:53 pm · java.io

BufferedInputStream mark() method marks the current position in this input stream. A subsequent call to the reset method repositions this stream at the last marked position so that subsequent reads re-read the same bytes. The readlimit argument tells this input stream to allow that many bytes to be read before the mark position gets invalidated….
Continue Reading »


Java Code for BufferedInputStream : reset() Method Example

Java Code for BufferedInputStream : reset() Method Example
DeeKay March 28, 2011 at 8:31 pm · java.io

BufferedInputStream reset() method repositions the current stream to the position at the time the mark method was called last time on this input stream. If markpos is -1 (no mark has been set or the mark has been invalidated), an IOException is thrown. Otherwise, pos is set equal to markpos. Overrides: reset in class FilterInputStream…
Continue Reading »


Java Code Example for BufferedInputStream: skip() method

Java Code Example for BufferedInputStream: skip() method
DeeKay March 22, 2011 at 12:34 am · java.io

skip() method of BufferedInputStream class creates a byte array and then repeatedly reads into it until n bytes have been read or the end of the stream has been reached. Skips over and discards n bytes of data from this input stream. The skip method may, for a variety of reasons, end up skipping over…
Continue Reading »


Java Code Example for BufferedInputStream: close() method

Java Code Example for BufferedInputStream: close() method
DeeKay March 21, 2011 at 11:13 pm · java.io

close() method of BufferedInputStream closes the current input stream and releases any system resources associated with the stream. Once the stream has been closed, further read(), available(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.


Java Code Example for BufferedInputStream: read(byte[] b,int off,int len) Method

Java Code Example for BufferedInputStream: read(byte[] b,int off,int len) Method
DeeKay March 21, 2011 at 6:23 pm · java.io

Reads bytes from this byte-input stream into the specified byte array, starting at the given offset. It attempts to read as many bytes as possible by repeatedly invoking the read method of the underlying stream. This iterated read continues until one of the following conditions becomes true: The specified number of bytes have been read,…
Continue Reading »


Java Code Example for BufferedInputStream: read() Method

Java Code Example for BufferedInputStream: read() Method
DeeKay March 21, 2011 at 6:11 pm · java.io

read() method of BufferedInputStream class reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the…
Continue Reading »


Java Code Example for BufferedInputStream to read file

Java Code Example for BufferedInputStream to read file
DeeKay March 21, 2011 at 4:29 pm · java.io

BufferedInputStream adds an additionally capability to InputStream by buffering the input. When instance of BufferedInputStream is created, an internal array is created to buffer the input data. As bytes from stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time. BufferedInputStream class has…
Continue Reading »


Code Example for BufferedInputStream: available() method

Code Example for BufferedInputStream: available() method
DeeKay March 21, 2011 at 3:53 pm · java.io

BufferedInputStream available() method returns the total count of bytes that can be read or skipped over from the input stream without blocking by next invocation of a method for the input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block,…
Continue Reading »