Google search

Basic and advanced computer skills like Excel with macros, How to speed up your PC, C, CPP, Java programming, HTML, JavaScript, PHP, Wordpress, all web tools, Android tutorials, MySQL Tutorials, WAMP server installation. etc.

Java Programs - 2

1. Java program to find number of lines.
import java.io.*;

public class NOL
{
public static void main(String[] args) 
{
try
{
System.out.println("Getting line number of a paritcular file example!");
 BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
 System.out.println("Please enter file name with extension:");
   String str = bf.readLine();
   File file = new File(str);
   if (file.exists())
{
    FileReader fr = new FileReader(file);
   LineNumberReader ln = new LineNumberReader(fr);
  int count = 0;
  while (ln.readLine() != null){
 count++;
  }
  System.out.println("Total line no: " + count);
 ln.close();
  }
 else{
  System.out.println("File does not exists!");
  }
 }
 catch(IOException e){
 e.printStackTrace();
 }
 }
}

2. Java Program on method override.
class Base
{
void show()
{
System.out.println("inside the Base class method");
}
}

class Derive extends Base
{
void show()
{
System.out.println("inside the Dervied class method");
}
}

class Override
{
public static void main(String args[])
{
Derive d=new Derive();
d.show();
}
}

3. Java Program to print system time.
import java.util.*;
class Systime
{
public static void main(String args[])
{
Date d=new Date();
System.out.println(d.getHours() + ":"+ d.getMinutes() + ":" + d.getSeconds());
}
}

4. Java Program to demonstrate thread example.
class Ta extends Thread
{
public void run()
{
for(int i=1; i<=5; i++)
{
System.out.println("A thread=" + i);
}
System.out.println("Ta is over");
}
}

class Tb extends Thread
{
public void run()
{
for(int j=1; j<=5; j++)
{
System.out.println("B thread=" + j);
}
System.out.println("Tb is over");
}
}

class thread
{
public static void main(String args[])
{
Ta ta=new Ta();
ta.start();

Tb tb=new Tb();
tb.start();
}
}

5. Java Program without main().
class WithoutMain
{
static{
System.out.println("Hello world");
System.exit(0);
}
}

for more java program click here