Automation Using Selenium Webdriver

Thursday, 22 December 2016

How to read a file in java with example program



  • We can read java file using buffered reader class in java
  • We need to import java.io.BufferedReader in order to read java text file.
  • Create object of java.io.BufferedReader class by passing new FileReader("C:\\Sample.txt") object to the constructor.
  • In order to read line by line call readLine() method of BufferedReader class which returns current line. Initially first line of java text file
  •  Program #1: Write a java program to read a file line by line using BufferedReader class 

  • package com.instanceofjava.javareadfile;

  • import java.io.BufferedReader;
  • import java.io.FileReader;
  • import java.io.IOException;

  • public class ReadFileBufferedReader {

  •  /**
  •   * 
  •   * @category: How to read java read file line by line using buffered reader
  •   */

  • public static void main(String[] args) {

  •   BufferedReader breader = null;

  •  try {

  •  String CurrentLine;

  •  breader = new BufferedReader(new FileReader("E://Sample.txt"));

  •  while ((CurrentLine = breader.readLine()) != null) {
  •     System.out.println(CurrentLine);
  •  }

  • } catch (IOException e) {
  •    e.printStackTrace();
  • } finally {
  •     try {
  •  if (breader != null)
  •      breader.close();
  •      } catch (IOException ex) {
  •     ex.printStackTrace();
  •      }
  • }

  • }

  • }

  •  Output:
  •   
  • Java open and read file
  • Read file line by line in java
  • Example java program to read a file line by line
  • java read file line by line example


  • Java example program to print message without using System.out.println()

    Is it possible to print message without using system.out.println?




    Yes its possible to print message without using System.out.println("");

    System.out.write("www.corejavawithselenium.blogspot.in\n".getBytes());
     System.out.format("%s", "www.java.com \n")
     PrintStream myout =  new PrintStream(new FileOutputStream(FileDescriptor.out));
       myout.print("www.corejavawithselenium.blogspot.in \n");
    System.err.print("This is custom error message");
    System.console().writer().println("Hai");

    1.System.out.write(Byte [] arg0);

    System.out.write(Byte [] arg0) method The java.io.FilterOutputStream.write(byte[] b) method writes b.length bytes to this output stream.

    package com.instanceofjava;

    public class PrintMessage {

    public static void main(String[] args) throws IOException{

           System.out.write("Hello World".getBytes());
    }
    }

    Output:

    Hello World

    2. PrintStream  java.io.PrintStream.format(String arg0, Object... arg1)

    System.out.format("format",Object obj) by using this method we can format the text and print

    package com.instanceofjava;

    public class PrintMessage {

    public static void main(String[] args){

            System.out.format("%s", "James");
    }

    }

    Output:

    James

    3. FileDescriptor:

    Create PrintStream object by passing FileOutputStream(FileDescriptor.out) object as an argument to the constructor
    package com.instanceofjava;

    public class PrintMessage {

    public static void main(String[] args){

           PrintStream myout =  new PrintStream(new FileOutputStream(FileDescriptor.out));
              myout.print("i love Java");
    }
    }

    Output:

    i love java


    4. System.err.print(""):

    We can use System.err.print("") method to print the message actually its used to print error message
    package com.instanceofjava;

    public static void main(String[] args){

           System.err.print("This is custom error message");
    }
    }

    Output:

    This is custom error message


    5.System.console().writer().println("");

    System.console() returns null if your application is not run in a terminal System.console() provides methods for reading password without echoing characters

    package com.instanceofjava;

    public static void main(String[] args){

           System.err.print("This is custom error message");
    }
    }

    Output:

    This is custom error message Exception in thread "main" java.lang.NullPointerException
        at PrintMessage.main(PrintMessage.java:24)