Wednesday 3 August 2016

Java Tutorial : Java IO (BufferedOutputputStream-Write partial data)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=PBRURy7uCPg&list=UUhwKlOVR041tngjerWxVccw

BufferedOutputStreamDemo.java
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
 * public void write(byte[] b, int off, int len) throws
 *                                             IOException
 * 
 * Parameters: 
 * ---------- 
 * b - the data. 
 * off - the start offset in the data. 
 * len - the number of bytes to write.
 */
public class BufferedOutputStreamDemo
{

    public static void main(String[] args) throws IOException
    {
        BufferedOutputStreamDemo bufferedOutputStreamDemo = new BufferedOutputStreamDemo();
        bufferedOutputStreamDemo.writeFile();
    }

    private void writeFile() throws IOException
    {

        FileOutputStream fileOutputStream = null;
        BufferedOutputStream bufferedOutputStream = null;

        try
        {
            fileOutputStream = new FileOutputStream("myoutputfile.txt");
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

            String str = "Peter is going to India.";
            byte byteArray[] = str.getBytes();

            /*
             * Writes len bytes from the specified byte
             * array starting at offset off to this buffered
             * output stream.
             */
            bufferedOutputStream.write(byteArray, 18, 5);
            /*
             * Flushes this buffered output stream. This
             * forces any buffered output bytes to be
             * written out to the underlying output stream.
             */
            bufferedOutputStream.flush();
            System.out.println("Successfully written to the file."
                    + "please check the file content.");

        }
        catch (Exception exe)
        {
            exe.printStackTrace();
        }

        finally
        {
            if (fileOutputStream != null)
            {
                fileOutputStream.close();
            }
            if (bufferedOutputStream != null)
            {
                bufferedOutputStream.close();
            }
        }
    }

}
Output
Successfully written to the file.please check the file content.
myoutputfile.txt
India
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_BufferedOutputStream_write_offset_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_BufferedOutputStream_write_offset_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/cc9ea15ba6d4c4de8bbc1e97954a85f48d342a8f/BasicJava/JavaIODemo_BufferedOutputStream_write_offset_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment