Tuesday, 23 August 2016

Java Tutorial : Java IO (PrintWriter Constructor accepts OutputStream)


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

PrintWriterDemo.java
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

/*
 * public PrintWriter(OutputStream out)
 * 
 * Parameters: 
 * ----------
 * 
 * out - An output stream
 */
public class PrintWriterDemo
{

    public static void main(String[] args) throws IOException
    {
        FileOutputStream fileOutputStream = null;
        PrintWriter printWriter = null;
        try
        {
            fileOutputStream = new FileOutputStream("myoutputfile.txt");
            /*
             * Creates a new PrintWriter, without automatic
             * line flushing, from an existing OutputStream.
             */
            printWriter = new PrintWriter(fileOutputStream);

            int intValue = 13;
            double doubleValue = 67.8;
            printWriter.printf("i = %d and k = %f", intValue, doubleValue);
            /*
             * Flushes the stream.
             */
            printWriter.flush();

            System.out.println("Successfully written to the file."
                    + "please check the file \'myoutputfile.txt\'");

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

    }
}
Output
Successfully written to the file.please check the file 'myoutputfile.txt'
myoutputfile.txt
i = 13 and k = 67.800000
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_PrintWriter_Cons_OutputStream_App.zip?attredirects=0&d=1

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

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/6677706d119a4693a72878c56e3b8b5e20670e29/BasicJava/JavaIODemo_PrintWriter_Cons_OutputStream_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