Tuesday 23 August 2016

Java Tutorial : Java IO (PrintWriter Constructor accepts file object)


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

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

/*
 * public PrintWriter(File file) throws
 *                          FileNotFoundException
 * 
 * Parameters: 
 * ----------
 * 
 * file - The file to use as the destination
 * of this writer. If the file exists then it will be
 * truncated to zero size; otherwise, a new file will be
 * created. The output will be written to the file and
 * is buffered.
 */

public class PrintWriterDemo
{

    public static void main(String[] args) throws IOException
    {
        PrintWriter printWriter = null;
        try
        {
            File file = new File("myoutputfile.txt");
            /*
             * Creates a new PrintWriter, without automatic
             * line flushing, with the specified file.
             */
            printWriter = new PrintWriter(file);

            int intValue = 100;
            double doubleValue = 200.6;
            printWriter.printf("i = %d and k = %f", intValue, doubleValue);
            
            System.out.println("Successfully written to the file."
                    + "please check the file \'myoutputfile.txt\'");

        }
        finally
        {

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

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

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

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