Example of PrintWriter

package fileionavigationexample;
import java.io.*;
public class PrintWriterExample {
public static void main(String[] args){
File file = new File(“C:\Users\KUNDAN\Documents\AdvanceJavaNotes.txt”);
try{
FileWriter fw = new FileWriter(file);
PrintWriter pw = new PrintWriter(fw);
pw.println(“Advance Java Is also Termed As Enterprise Edition ie. J2EE.”);
pw.println(“It includes Folowing Topics.”);
pw.println(“1) JDBC:Java Database Conectivity.”);
pw.println(“2) Servelet”);
pw.println(“3) JSP”);
pw.println(“4) Hibernate”);
pw.println(“5) Spring”);
pw.println(“6) Struts”);

pw.close();
fw.close();
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String f = br.readLine();
while((f = br.readLine()) != null){
System.out.println(f);
}
br.close();
}catch(IOException ex){
ex.printStackTrace();
}
}
}

Example Of BufferedWriter And BufferedReader

package fileionavigationexample;
/*All Belongs to java.io package
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
*/
import java.io.*;

public class BufferedWriterAndBufferedReader extends IOException {
public static void main(String[] args) throws IOException{
File file = new File(“C:\Users\KUNDAN\Documents\JavaNotes.txt”);

FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(“Features Of Java Programming Language:”);
bw.newLine();
bw.write(“1) Java is an Object Oriented Programming Language also called OOPS Language.”);
bw.newLine();
bw.write(“2) Java language is Highly Secured. Means It provides Third party security.”);
bw.newLine();
bw.write(“3) Java language is Robust. Means 24/7 it can be accesed by the user.”);
bw.newLine();
bw.write(“4) Java language is an scale oriented programming language. Means we can restrict an application for the number of users at a time.”);

bw.close();
fw.close();

FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String s = null;

while((s = br.readLine()) != null){
System.out.println(s);
}
fr.close();
br.close();

}
}

Example Of FileWriter And FileReader

package fileionavigationexample;
import java.io.*;
public class FileWriterAndFileReader {
public static void main(String[] args){
char[] in = new char[100];
int size = 0;
try{
File file = new File(“C:\Users\KUNDAN\Documents\Firsttext.txt”);
FileWriter fw = new FileWriter(file);//this is a file writer object “fw”.
fw.write(“Hello I am writing a story.”);//it is used to write character into the text file “Firsttext.txt”.
// fw.write(“Hello I am writing a story.”);
fw.close();

FileReader fr = new FileReader(file);
size = fr.read(in);//it reads the total number of bytes in a file.
for(char c:in){
System.out.print(c);
}
System.out.println(size); //return 26(21 letters + 5 spaces b/w letters)
fr.close();

}catch(IOException ex){
ex.printStackTrace();
}
}
}

/*
Output:
————-
Hello I am writing a story 26 bytes
*/

How To Create New Text File

FileIONavigationExamplepackage fileionavigationexample;
import java.io.*;
public class ToCeateNewTextFile {
public static void main(String[] args) {
boolean f = false;
File file = new File(“C:\Users\KUNDAN\Documents\First.txt”);//Its just creating the name of file but not actual file.
//to create actual file we need the below code after the declaring name of file.
System.out.println(file.exists());//first time compiling the code gives “false” but 2nd time gives “true.”
try{
f = file.createNewFile(); //after compiling this code you will see an empty text file as “First.txt” at the specified address.
System.out.println(f); //At very first time it will return true but second time it returns false b/c once file created than
//again it will not create the same file.
System.out.println(file.exists());//all time return “true”.
}catch(IOException e){
e.printStackTrace();
}
}

}

/*initial output :
———————-
false
true
true

after initial output:
———————-
true
false
true */

Block Level Synchronization

BlockLevelSyncpackage synchronizationusingmethod;

public class BlockLevelSyncronization {

public static void main(String[] args) {
Course c = new Course();
courseDetail d1 = new courseDetail(1,c);
courseDetail d2 = new courseDetail(2,c);
d1.start();
d2.start();
}

}

class Course{
private String name;
private double cost;
public int duration;

public void Display(int id,String name,double cost, int duration){
this.name = name;
this.cost = cost;
this.duration = duration;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;//thousands of code
synchronized(this){
System.out.println(“Thread :” +id + “Running………”);
for(int i=0; i<5; i++){
System.out.println("Course Name :"+name+" "+"Course Cost :"+cost+"Rs."+" "+"Course Duration :"+duration+" months");
}
try{
Thread.sleep(2000);
}catch(InterruptedException ex){
ex.printStackTrace();
}
System.out.println("ID :"+id+"Finished");
}
}
}

class courseDetail extends Thread{
private int id;
private Course c;
courseDetail(int id,Course c){
this.id = id;
this.c = c;
}
public void run(){
c.Display(id,"Core Java",5000,2);
}

}

Class Level Synchronization

ClassLevelSyncpackage classlevelsync;

public class ClassLevelSync {

public static void main(String[] args) {
weddingWish ww = new weddingWish();
person p1 = new person(ww,”Sam”);
person p2 = new person(ww,”Amit”);
p1.start();
p2.start();
}

}

class weddingWish{
private String name;
public void wish(String name){
synchronized(weddingWish.class){//class level lock is used.
this.name = name;
System.out.println(“Friends Congratulating : “+name);
for(int i=0; i<5; i++){
System.out.print("Happy marriage Day :");
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
System.out.println(name);
}
}
}
}

class person extends Thread{
private weddingWish ww;
private String name;
person(weddingWish ww, String name){
this.ww = ww;
this.name = name;
}
public void run(){
ww.wish(name);
}
}

Method Level Synchronization

package multithreading;
public class CustomerBill {
public static void main(String[] args){
McDonald m = new McDonald();
Customer1 c1 = new Customer1(m,”kundan”,”Burger and French_Fries”,125.50f);
Customer2 c2 = new Customer2(m,”Samarjeet”,”Aloo Mc_Tikki,Mc_Vegi”,375);

c1.start();
c2.start();
}
}

class McDonald{
String itemName;
float price;

public synchronized void Menu(String name,String itemName, float price){
this.itemName = itemName;
this.price = price;
System.out.println(“Customer :”+name);

System.out.println(“Item :”+itemName+”t” + “Price :”+price);

try{
Thread.sleep(200);
}catch(InterruptedException e){
e.printStackTrace();
}

}
}

class Customer1 extends Thread{
McDonald m;
String name;
String itemName;
float price;

Customer1(McDonald m,String name,String itemName, float price){
this.m = m;
this.name = name;
this.price = price;
this.itemName = itemName;
}
public void run(){
m.Menu(name,itemName,price);
}
}

class Customer2 extends Thread{
McDonald m;
String name;
String itemName;
float price;

Customer2(McDonald m,String name,String itemName,float price){
this.m = m;
this.name = name;
this.price = price;
this.itemName = itemName;
}
public void run(){
m.Menu(name,itemName,price);

}
}

Multi threading Example

package multithreading;
public class Thread1{
public static void main(String args[]) throws InterruptedException{
myThread t1 = new myThread();
myThread2 t2 = new myThread2();

t1.start();
t2.start();
}
}

//Thread 1

class myThread extends Thread{
String s = “Hi Arjun”;
public void run(){

for(int i=0; i<s.length(); i++){
char c = s.charAt(i);
System.out.println(c);
try{
Thread.sleep(2000);
}catch(InterruptedException ex){
ex.printStackTrace();
}
}
}
}

//Thread 2

class myThread2 extends Thread{
String s2 = “Hi Karan”;
public void run(){
for(int i=0; i<s2.length(); i++){
char c2 = s2.charAt(i);
System.out.println(c2);
try{
Thread.sleep(2000);
}catch(InterruptedException ex){
ex.printStackTrace();
}
}
}
}

Download Netbeans Project of Multithreading example