Tuesday 23 August 2016

Java Tutorial : Java IO (PrintWriter Constructor accepts filename)


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

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

/*
 * public PrintWriter(String fileName) throws
 *                                  FileNotFoundException
 * 
 * Parameters: 
 * ----------
 * 
 * fileName - The name of 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
        {
            String fileName = "myoutputfile.txt";
            /*
             * Creates a new PrintWriter, without automatic
             * line flushing, with the specified file name
             */
            printWriter = new PrintWriter(fileName);

            int intValue = 10;
            double doubleValue = 50.87;
            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 = 10 and k = 50.870000
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_PrintWriter_Cons_FileName_App.zip?attredirects=0&d=1

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

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