How to create csv file in java using filewriter

  • We can create CSV file and fill some data in to it using java program.
  • We can create CSV file and write data int to CSV file using java.io.FileWriter class.
  • Creating object of java.io.FileWriter class by giving output filename.
  • After creating object of java.io.FileWriter append data by calling append("data") method of FileWriter class object.
  • Use append('\n'); for enter data in new row.
  • Lets see a java example program on how to create csv file and write data in to it using FileWriter Class.



Program #1: Java program to create CSV file using FileWriter class

  1. package com.java.createcsvfile;

  2. import java.io.FileWriter;
  3. import java.io.IOException;

  4. public class CreateCsvFile {

  5. private static void generateCsvFile(String fileName) {

  6.       FileWriter writer = null;

  7.  try {

  8.      writer = new FileWriter(fileName);
  9.      writer.append("Name");
  10.      writer.append(',');
  11.      writer.append("Number");
  12.      writer.append('\n');

  13.      writer.append("interview questions");
  14.      writer.append(',');
  15.      writer.append("001");
  16.      writer.append('\n');

  17.   writer.append("interview programs");
  18.   writer.append(',');
  19.   writer.append("002");
  20.   writer.append('\n');

  21.   System.out.println("CSV file is created...");

  22.   } catch (IOException e) {
  23.   e.printStackTrace();
  24.   } finally {
  25.         try {
  26.    writer.flush();
  27.    writer.close();
  28.         } catch (IOException e) {
  29.    e.printStackTrace();
  30. }
  31. }
  32. }

  33. public static void main(String[] args) {

  34. String location = "E:\\newCsvFile.csv";
  35. generateCsvFile(location);

  36. }

  37. }

 Output:


create csv file in java

Creating array of objects in java example program

  • Array is collection of similar data types.
  • Arrays can hold collection of data with indexes
  • We already know that we can hold group of primitive variables
  • Arrays can hold referenced variables also like Strings and objects
  • So we can say it is possible to store or create array of objects in java



Declaring arrays in java:

  •  One dimensional array can be created like
  • int[] singlearray;
  • Two dimensional array can be created like 
  • int[][] intdoubleArray;       
  • double[][] doubleArray;

Instantiation and Initialization of Arrays

Program #1: Java example program to store string object in array
  1. package arraysofobjectsinjava;
  2. public class ArrayOfObjects {
  3.  
  4.     /**
  5.      * @www.instanceofjava.com
  6.      *  creating and assigning values to arrays in java
  7.      */
  8.    public static void main(String[] args) {
  9.         
  10.         int[] a= new int[2];
  11.         a[0]=1;
  12.         a[1]=2;
  13.         
  14.         System.out.println(a[0]);
  15.         System.out.println(a[1]);
  16.         
  17.         int[] var= {1,2,3,4,5};
  18.         
  19.         
  20.         for (int i = 0; i < var.length; i++) {
  21.             System.out.println(var[i]);
  22.         }      
  23. int[][] array=new int[2][2];       
  24.         
  25.         array[0][0]=1;
  26.         array[0][1]=2;
  27.         array[1][0]=3;
  28.         array[1][1]=4;
  29.         
  30.   for(int i=0; i<array.length; i++) {
  31.  
  32.                for(int j=0; j<array[1].length; j++)
  33.                    System.out.print(array[i][j] + " ");
  34.                System.out.println();
  35. }  
  36.  
  37. }
  38.  
  39. }

 Output:


  1. 1
  2. 2
  3. 1
  4. 2
  5. 3
  6. 4
  7. 5
  8. 1 2 
  9. 3 4

Array of objects in java:

Program #2: Java example program to store string object in array


  1. package arraysofobjectsinjava;
  2. public class ArrayofStringObjects {
  3.  
  4.     /**
  5.      * @www.instanceofjava.com
  6.      * Storing String objects in String array
  7.      */
  8.     public static void main(String[] args) {
  9.         
  10.         
  11.         String a[]= new String[5];
  12.         a[0]="array of objects";
  13.         a[1]="object array in java";
  14.         a[2]="array of objects in java example program";
  15.         a[3]="array of objects in java tutorial";
  16.         a[4]="how to make array of objects in java";
  17.         
  18.         for (int i = 0; i < a.length; i++) {
  19.             System.out.println(a[i]);
  20.         }
  21.  
  22. }
  23.  
  24. }

Output:
  1. array of objects
  2. object array in java
  3. array of objects in java example program
  4. array of objects in java tutorial
  5. how to make array of objects in java

Creating custom array of objects in java
  •  We can also store custom objects in arrays .
  • Create a employee class.
  • Create multiple objects of employee class and assign employee objects to array.
  • Arrays can store objects but we need to instantiate each and every object and array can store it

Program#3: java example program to create custom objects and store in array


Employee.java

  1. package arraysofobjectsinjava;
  2. public class Employee {
  3.  
  4.     String name;
  5.     int id;
  6.     
  7.     Employee(String name, int id){
  8.         this.name=name;
  9.         this.id=id;
  10.         
  11.     }
  12.  
  13. }


array of objects in java

Tower of hanoi recursive solution using Java

  • Towers of  Hanoi is a famous game.
  • In this game there are three poles and N number of disks placed one over another in increasing in size from top to bottom.

  • Objective of this game is to move disks from first pole to last pole.
  • And the condition is we can not place bigger disk on top of smaller disk.
  • Initially all disks placed in first pole smaller disk will be on top and bigger disk will be on bottom.
  • We need to move all the disks from from first pole to last pole.

Rules of tower of  Hanoi:
  • We can move only one disk at a time.
  • At any poi of time larger disk can not be placed on smaller disk.
  • In order to solve this problem we have given a second pole so we can use second pole and move disks from  first pole to third pole.
  • We can solve this using rec recursive procedure.
Tower of  Hanoi with Single disk: N=1
  • Three poles are A , B ,C
  • And a disk is present at A we need to move from A to C
  • As it its single disk we can directly move disk A - > C 
tower of hanoi recursive solution

 Tower of  Hanoi with Two disks : N=2
  • Three poles are A , B ,C
  • And two disks are placed in pole A, Disk 1 and Disk2 top to bottom.( assume Disk 2 is smaller and Disk 1 bigger)
  • Move Disk2 from A to  B 
  • Move Disk1 From A to C.
  • Move Disk2 from B to C.
tower of hanoi in data structure program


Tower of  Hanoi with Three disks : N=3

  • Three poles are A , B ,C
  • And three disks are placed in pole A, Disk 1  top to bot, Disk2 and Disk 2 top bottom to .( assume Disk 3 is smaller and Disk 1 bigger)
  • In this firs we need to move two disk from  A to B which we already done in above procedure
  • So we need to repeat that here.
  • Move Disk1 from A to C.
  • Now Moving two disks from B to C we have already seen in above procedure so its recursive.


Tower of Hanoi Recursive Algorithm:

N = number of disks

If  N == 1
  • Move Single disk from A to C
If   N >1

  1. 1.Move n-1 disks from start A to B  TowersofHanoi(n-1,start, end , aux)
  2. Move last Disk from A to C
  3. Move n-1 disks from B to C.             TowersofHanoi(n-1,start, aux, end )
  • Step 1 and 3 are recursive procedures.
  • Lets see hoe to write  java recursive program for this towers of  Hanoi problem
  • Here B as auxiliary pole.

Program #1: Java Example program on towers of  Hanoi:

  1. package towersofhanoi;
  2. import java.util.Scanner;
  3.  
  4. public class TowersofHanoi {
  5.  
  6. public void TOH(int n, String start, String aux, String end) {
  7.  
  8.            if (n == 1) {
  9.                System.out.println(start + " -> " + end);
  10.            } else {
  11.                TOH(n - 1, start, end, aux);
  12.                System.out.println(start + " -> " + end);
  13.                TOH(n - 1, aux, start, end);
  14.            }
  15. }
  16.  
  17. public static void main(String[] args) {
  18.  
  19.            TowersofHanoi towersOfHanoi = new TowersofHanoi();
  20.  
  21.            System.out.print("Enter number of discs: ");
  22.            Scanner scanner = new Scanner(System.in);
  23.            int discs = scanner.nextInt();
  24.            towersOfHanoi.TOH(discs, "A", "B", "C");
  25. }
  26.  
  27. }

 Output:

  1. Enter number of discs: 
  2. 3
  3. A -> C
  4. A -> B
  5. C -> B
  6. A -> C
  7. B -> A
  8. B -> C
  9. A -> C

Java interface programming questions

  • We can develop interfaces by using "interface" keyword. 
  • A class will implements all the methods in an interface.
  • By default interface methods are abstract.
  • Lets see some interesting java programming interview questions on interfaces.
  • Interface programming questions in java




Java interface interview programs part 1: interface programming java

Program #1: what will happen if we define normal methods in interface

  1. package interfaceinverviewprograms.instanceofjava;
  2. public interface A{
  3.  
  4. /**
  5. * @java interface  interview programming  questions and answers for freshers and experienced
  6.  */
  7.   
  8. void show() {
  9.         
  10.         System.out.println("Hello world");
  11.  
  12.     }
  13. }





Program #2:java interview programs to practice: Non static variables in interface

  1. package interfaceinverviewprograms.instanceofjava;
  2. public interface A{
  3.  
  4. /**
  5. * @java interface  interview programming  questions and answers for freshers and experienced
  6.  */
  7.   
  8. int a,b;

  9. }





Program #3:java interview programs to practice: which modifiers interface allows

  1. package interfaceinverviewprograms.instanceofjava;
  2. public interface A{
  3.  
  4. /**
  5. * @java interface  interview programming  questions and answers for freshers and experienced
  6.  */
  7.   
  8. private int x;
  9. protected int y;

  10. }





Program #4:java interview programs to practice: interface allows constructor?

  1. package interfaceinverviewprograms.instanceofjava;
  2. public interface A{
  3.  
  4. /**
  5. * @java interface  interview programming  questions and answers for freshers and experienced
  6.  */
  7. A(){
  8.  
  9. }

  10. }





How to open notepad using java program

  • Notepad is a text editor from windows operating system. We use notepad for writing text files.
  • We can open a new notepad using java code.
  • By using the concept of running another application by Runtime class in java.
  • By creating object of runtime and calling  exec() method by passing application name.
  • Lets see how to open a notepad using java code




Program #1: Java example program to open notepad


  1. package interestingJavaprograms;
  2. import java.io.IOException;
  3.  
  4. public class NotepadJava {
  5.  
  6.     /**
  7.      * @ www.instanceofjava.com
  8.      * @ how to open a new notepad using java program
  9.      */
  10. public static void main(String[] args) {
  11.        
  12.           Runtime rt = Runtime.getRuntime();
  13.           
  14. try {
  15.       rt.exec("notepad");
  16. }
  17.  catch (IOException ex) {
  18.  
  19.  System.out.println(ex);
  20.  
  21. }  
  22.  
  23. }
  24.  
  25. }

 Output:

open notepad using java


Program #2: Java example program to open notepad and after 2 seconds close it.


  1. package interestingJavaprograms;
  2. import java.io.IOException;
  3.  
  4. public class NotepadJava {
  5.  
  6.     /**
  7.      * @ www.instanceofjava.com
  8.      * @ how to open a new notepad using java program
  9.      */
  10. public static void main(String[] args) throws InterruptedException, IOException {
  11.         
  12. Runtime runTime = Runtime.getRuntime();
  13. System.out.println("Opening notepad");
  14. Process process = runTime.exec("notepad");
  15.           
  16. try {
  17.  
  18.  
  19. Thread.sleep(200); 

  20.  process.destroy();
  21.  System.out.println("Closing notepad");
  22.  
  23. }
  24.  catch (Exception ex) {
  25.  
  26.  System.out.println(ex);
  27.  
  28. }  
  29.  
  30. }
  31.  
  32. }

  • We can open already existing notepad also for that we need to specify notepad.exe location and path of the destination file.
  • We need to pass these two parameters  to exec method of runtime class.
  • runTime.exec("C:\\Windows\\System32\\notepad.exe E:\\Samplenotepad.txt");

Program #3: Java example program to open exiting notepad txt file.


  1. package interestingJavaprograms;
  2. import java.io.IOException;
  3.  
  4. public class NotepadJava {
  5.  
  6.     /**
  7.      * @ www.instanceofjava.com
  8.      * @ how to open a new notepad using java program
  9.      */
  10. public static void main(String[] args) {
  11.        
  12.           Runtime rt = Runtime.getRuntime();
  13.           
  14. try {
  15.    runTime.exec("C:\\Windows\\System32\\notepad.exe E:\\Samplenotepad.txt");
  16. }
  17.  catch (IOException ex) {
  18.  
  19.  System.out.println(ex);
  20.  
  21. }  
  22.  
  23. }
  24.  
  25. }

Java program to reverse vowels of a string


Program : Java example program to Reverse Vowels  in a String



  1. package inheritanceInterviewPrograms;
  2. /*
  3.  * @www.instanceofjava.com
  4.  */
  5. public class ReverseVowels {
  6.     public static String reverseVowels(String string) {
  7.  
  8.         String vowelsStr = "aeiouAEIOU";
  9.  
  10.         int lo = 0;
  11.         int hi = string.length() - 1;
  12.         char[] ch = string.toCharArray();
  13.  
  14.  while (lo < hi) {
  15.  
  16.      if (!vowelsStr.contains(String.valueOf(string.charAt(lo)))) {
  17.                 lo++;
  18.                 continue;
  19.        }
  20.  
  21.     if (!vowelsStr.contains(String.valueOf(string.charAt(hi)))) {
  22.                 hi--;
  23.                 continue;
  24.        }
  25.  
  26.     // swaping variables
  27.      swap(ch, lo, hi);
  28.             lo++;
  29.             hi--;
  30.       }
  31.  
  32.         return String.valueOf(ch);
  33.     }
  34.  
  35. private static void swap(char[] ch, int lo, int hi) {
  36.  
  37.         char temparray = ch[lo];
  38.         ch[lo] = ch[hi];
  39.         ch[hi] = temparray;
  40.  
  41.  }
  42.     
  43. public static void main (String args[]) {
  44.         
  45.          
  46.  System.out.println("After reversing vowels in a string="reverseVowels("InstanceOfjava"));
  47.         
  48.          
  49. }
  50.  
  51. }
Output:


  1. After reversing vowels in a string=anstancOefjavI


Core java online programming test on inheritance

  • Inheritance means getting properties from one class object to another.
  • So in sub class we can use or access super class variables and method : re usability
  • There are some interesting and important points to discuss about inheritance
  • In major java interviews and java online test on core java there is more chance of getting programming questions from inheritance
  • So let us see some java test questions. java test online to practice
  • If you want explanations then visit below topic
  • Top 16 Java Inheritance Interview questions for freshers and experienced  




5 Points to know about inheritance:

Program #1: Java test on inheritance:
  • Creating object for super class and calling super class methods and accessing super class variables.

  1. package interviewprograms.instanceofjava;
  2. public class Super{
  3.  
  4.  int a, int b;
  5.  
  6. void show(){
  7.  System.out,println("Inside Show method");
  8. }
  9.  
  10. }



  1. package interviewprograms.instanceofjava;
  2. public class Sub extends Super{
  3.  int a, int b;
  4. public static void main(String[] args) {
  5.         
  6. Super obj= new Super();
  7. obj.a=10;
  8. System.out,println(obj.a);
  9.  obj.show();
  10. }
  11. }






Program #2: Core Java online test on inheritance:
  • Creating object for sub class and calling sub class methods and accessing sub class variables.

  1. package interviewprograms.instanceofjava;
  2. public class Super{
  3.  
  4.  int a, int b;
  5.  
  6. void show(){
  7.  System.out,println("Inside Show method");
  8. }
  9.  
  10. }

  1. package interviewprograms.instanceofjava;
  2.  
  3. public class Sub extends Super{
  4.  int x;
  5.  
  6.  
  7. void show(){
  8.  System.out,println("Inside Sub class Show method");
  9. }
  10. public static void main(String[] args) {
  11.         
  12. Sub obj= new Sub ();
  13. obj.x=10;
  14. System.out,println(obj.x);
  15.  obj.show();
  16. }
  17. }





Program #3: core java online test for beginners and experienced on inheritance
  • Creating object for sub class accessing super class members and sub class members.

  1. package interviewprograms.instanceofjava;
  2. public class Super{
  3.  
  4.  int a, int b;
  5.  
  6. void show(){
  7.  System.out,println("Inside Show method");
  8. }
  9.  
  10. void print(){
  11.  System.out,println("Inside super class print method");
  12. }
  13.  
  14. }

  1. package interviewprograms.instanceofjava;
  2.  
  3. public class Sub extends Super{
  4.  int x;
  5.  
  6.  
  7. void show(){
  8.  System.out,println("Inside Sub class Show method");
  9. }
  10. void msg(){
  11.  System.out,println("Inside Sub class msg method");
  12. }
  13. public static void main(String[] args) {
  14.         
  15. Sub obj= new Sub ();
  16. obj.x=10;
  17. System.out,println(obj.x);
  18.  obj.show();
  19. obj.print();
  20. }
  21. }







Program #4: core java online test for beginners and experienced on inheritance
  • Creating object for sub class and assigning to super class reference.

  1. package interviewprograms.instanceofjava;
  2. public class Super{
  3.  
  4.  int a, int b;
  5.  
  6. void show(){
  7.  System.out,println("Inside Show method");
  8. }
  9.  
  10. void print(){
  11.  System.out,println("Inside super class print method");
  12. }
  13.  
  14. }


core java online test for beginners






Program #5: core java online test for beginners and experienced on inheritance

  • Creating object for sub class and assigning to super class reference and calling sub class method.
  1. package interviewprograms.instanceofjava;
  2. public class Super{
  3.  
  4.  int a, int b;
  5.  
  6. void show(){
  7.  System.out,println("Inside Show method");
  8. }
  9.  
  10. void print(){
  11.  System.out,println("Inside super class print method");
  12. }
  13.  
  14. }


  1. package interviewprograms.instanceofjava;
  2.  
  3. public class Sub extends Super{
  4.  int x;
  5.  
  6.  
  7. void show(){
  8.  System.out,println("Inside Sub class Show method");
  9. }
  10.  
  11. void msg(){
  12.  System.out,println("Inside Sub class msg method");
  13.  
  14. public static void main(String[] args) {
  15.         
  16. Super obj= new Sub (); 
  17. obj.msg();
  18. }
  19. }





EnJoY LeArNinG  WitH Us...
Select Menu