Posts

Showing posts with the label java

83.RandomAccessfile in java

 //randomaccessfile import java.io.*; import java.util.*; class randomaccessfile { public static void main(String[] args) { try { Scanner obj = new Scanner(System.in); int n, ch; long len; RandomAccessFile raf = new RandomAccessFile("File.txt","r"); len = raf.length(); System.out.print("Enter last character to Display : "); n = obj.nextInt(); if (n > len) { raf.seek(0); } else { raf.seek(len - 0); } raf.close(); } catch(Exception e) { System.out.println(e); } } } OUTPUT:

82.File Information in JAVA

 //fileinfo import java.io.*; class fileinfo { public static void main(String[] args) { try { File file = new File("C:\\Users\\Divyesh\\Desktop\\java\\File.txt"); System.out.println("getName() = " + file.getName()); System.out.println("getParent() = " + file.getParent()); System.out.println("getPath() = " + file.getPath()); System.out.println("isFile() = " + file.isFile()); System.out.println("isDirectory() = " + file.isDirectory()); System.out.println("canRead() = " + file.canRead()); System.out.println("canWrite() = " + file.canWrite()); System.out.println("exists() = " + file.exists()); System.out.println("isHidden() = " + file.isHidden()); System.out.println("length() = " + file.length()); } catch(Exception e) { System.out.println(e); } } } OUTPUT: getName() = File.txt getParent() = C:\Users\Divy...

81.System.Out.Println() in java

//System.Out.Println() /* System is class java.lang.System printStream is Class java.io.printStream println is method of printStream class */ class sys { static pristr out = new pristr(); } class pristr { void println(String str) { System.out.println(str); } } class SystemOut { public static void main(String[] args) { sys.out.println("Welcome"); } }  OUTPUT: Welcome

80.FileinputeStream i n JAVA

 //FileinputeStream import java.io.*; class FileInputeStream1 { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("bytes.txt"); int ch; while((ch = fis.read()) != -1) { System.out.print( ch + "\t" ); } fis.close(); } catch(Exception e) { System.out.println(e); } } } OUTPUT: 1       2       3       4       5       6       7       8       9       10

79.FileOutputStream in JAVA

 //Byte Stream //FileOutputStream import java.io.*; class FileOutputStream1 { public static void main(String[] args) { try{ FileOutputStream fos = new FileOutputStream("bytes.txt"); for (int i = 1; i <= 10; i++) { fos.write(i); //String str = "Line : " + i; //byte arr[]  = str.getBytes(); //.write(arr); } fos.close(); } catch(Exception e) { System.out.println(e); } } } OUTPUT: text file : bytes.txt 1     2     3     4     5     6     7     8     9     10

78.BufferReader In JAVA

 //Buffereader import java.io.*; class Buffereader { public static void main(String[] args) { try { FileReader fr = new FileReader("buffer.txt"); BufferedReader br = new BufferedReader(fr); int ch; while((ch = br.read()) != -1) { System.out.print((char) ch ); } br.close(); fr.close(); } catch(Exception e) { System.out.println(e); } } } OUTPUT: ☺☻♥♦♣

77.BufferWriter in JAVA

 //Bufferwriter import java.io.*; class Bufferwriter { public static void main(String[] args) { try{ FileWriter fw = new FileWriter("buffer.txt"); BufferedWriter bw = new BufferedWriter(fw); for (int i = 1; i <= 10; i++) { bw.write(i); } bw.close(); fw.close(); } catch(Exception e) { System.out.println(e); } } } OUTPUT: Test file : buffer.txt  

76.FileReader IN JAVA

 //filereader import java.io.*; class filereader { public static void main(String[] args) { try { FileReader fr = new FileReader("my.txt"); int ch; while((ch = fr.read()) != -1) { System.out.print((char) ch ); } fr.close(); } catch(Exception e) { System.out.println(e); } } } OUTPUT: Milan Bakotra

75.FileWriter in JAVA

 //filewriter import java.io.*; class filewriter { public static void main(String[] args) { try{ FileWriter fw = new FileWriter("BCA"); for (int i = 1; i <= 10; i++) { fw.write("Line : " + i); fw.write("\n"); } fw.close(); } catch(Exception e) { System.out.println(e); } } } OUTPUT: NOTEPAD FILE "BCA.TXT" Line : 1 Line : 2 Line : 3 Line : 4 Line : 5 Line : 6 Line : 7 Line : 8 Line : 9 Line : 10

74.sychronization in java

 //sychronization class  Account { static int bal; Account() { bal = 0; } synchronized void deposit(int amt) { bal = bal + amt; } } OUTPUT: Balance : 1000000 class customer extends Thread { Account act; customer (Account ac) { act = ac; } public void run() { for (int i = 1; i <= 10000; i++) { act.deposit(10); } } } class sychronization { public static void main(String[] args) { Account acc = new Account(); customer cust[] = new customer[10]; int a; for (a = 0; a < 10; a++) { cust[a] = new customer(acc); cust[a].start(); } for (a = 0; a < 10; a++) { try{ cust[a].join(); } catch(Exception e) { System.out.println("Exception : " + e); } } System.out.println("Balance : " + acc.bal); } }

73.multi_threading in java

 //multi_threading class threaA extends Thread { public void run() { try { for (int i = 1; i <= 10; i++ ) { System.out.println("\t" + i); Thread.sleep(100); } } catch(Exception e) { System.out.println(e); } } } class threaB extends Thread { public void run() { try { for (int i = 10; i >= 1; i-- ) { System.out.println("\t" + i); Thread.sleep(100); } } catch(Exception e) { System.out.println(e); } } } class multi_threading { public static void main(String[] args) { threaA objA = new threaA(); threaB objB = new threaB(); objA.start(); objB.start(); try { objA.join(); objB.join(); } catch(Exception e) { System.out.println("Main : " + e); } System.out.println("End of main() Program"); } } OUTPUT:   1         10         9         2         3     ...

72.multi - threading by implementing runable interfce in java

 //multi - threading by implementing runable interfce  class MyThread2 implements Runnable { public void run() { try { for (int i = 10; i >= 1; i-- ) { System.out.println("\t" + i); Thread.sleep(100); } } catch(Exception e) { System.out.println(e); } } } class threading2 { public static void main(String[] args) { MyThread2 obj = new MyThread2(); Thread t = new Thread(obj); t.start(); System.out.println("End..."); } } OUTPUT: End...         10         9         8         7         6         5         4         3         2         1

71. multi - threading in java

 //multi - threading by extending Thread class  class MyThread extends Thread { public void run() { try { for (int i = 1; i <= 10; i++ ) { System.out.println("\t" + i); Thread.sleep(100); } } catch(InterruptedException e) { System.out.println(e); } } } class threading1 { public static void main(String[] args) { MyThread obj = new MyThread(); obj.start(); System.out.println("End of Program"); } } OUTPUT: End of Program         1         2         3         4         5         6         7         8         9         10

70.division using throws keyword

 //Division.java file //Throws keyword public class division { public void divide(int a, int b) throws ArithmeticException { int c = a / b; System.out.println("Division : " + c); } } ================================================================ //calculate2.java //throws keyword import java.util.*; class calculate2 { public static void main(String[] args) { Scanner obj = new Scanner(System.in); int x, y; System.out.print("Enter Value X : "); x = obj.nextInt(); System.out.print("Enter Value Y : "); y = obj.nextInt(); try { division div = new division(); div.divide(x, y); } catch(ArithmeticException ae) { System.out.println("Second Argument must be Non-Zero"); } } } OUTPUT: Enter Value X : 12 Enter Value Y : 3 Division : 4 ------------------------------------------------------------------------------------------ Enter Value X : 12 Enter Value Y : 0 Second Argument must be Non-Zero

69.throw keyword in java

 //throw keyword class throw1 { public static void main(String[] args) { try { //ArithmeticException obj = new ArithmeticException(); //throw obj; throw new ArithmeticException(); } catch(ArithmeticException ae) { System.out.println("Exception Caught"); } catch(Exception e) { System.out.println("Exception : " + e); } } } OUTPUT: Exception Caught

68. User Define Exception or Custom Exception in JAVA

 //User Define Exception or Custom Exception class ExceptionA extends Exception { ExceptionA(String msg) { super(msg); System.out.println(msg); } } class ExceptionB extends Exception { ExceptionB(String msg) { super(msg); System.out.println(msg); } } class user_define_exc { public static void main(String[] args) throws ExceptionA, ExceptionB { int n = Integer.parseInt(args[0]); if (n % 2 == 1) { throw new ExceptionA("Problem"); } else { throw new ExceptionB("Big Problem"); } } } OUTPUT: -------------------------------------------------------------------- java user_define_exc 5 Problem Exception in thread "main" ExceptionA: Problem         at user_define_exc.main(user_define_exc.java:25) --------------------------------------------------------------------- java user_define_exc 2 Big Problem Exception in thread "main" ExceptionB: Big Problem         at user_define_exc.main(user_define...

67.Exeption Handling in java

Image
 class Exeption_handling { public static void main(String[] args) { try { int a = Integer.parseInt(args[0]); int b = Integer.parseInt(args[1]); int c = a / b; System.out.println("Division : " + c); } catch(ArrayIndexOutOfBoundsException e) { System.out.println("Enter Two Argument"); } catch(NumberFormatException ne) { System.out.println("Enter Two Numaric Argument"); } catch(ArithmeticException ae) { System.out.println("Second Argument must be Non-Zero"); } catch(Exception e) { System.out.println("Exception : " + e); } finally { System.out.println("Finally block Execute"); } } } OUTPUT: =================================== java Exeption_handling 4 2 Division : 2 Finally block Execute ---------------------------------------------------------- java Exeption_handling Enter Two Argument Finally block Execute ----------------------------...

66.Vector in JAVA

// Vector  is like the dynamic array which can grow or shrink its size. Unlike array, we can store n-number of elements in it as there is no size limit.  import java.util.*; class vector_class { public static void main(String[] args) { Vector v = new Vector(); System.out.println("Vector = " + v); System.out.println("Vector Capacity = " + v.capacity()); System.out.println("Vector Size = " + v.size()); v.add("one"); v.add("two"); v.add("three"); System.out.println("\nVector = " + v); System.out.println("Vector Capacity = " + v.capacity()); System.out.println("Vector Size = " + v.size()); for (int i = 1; i <= 10 ; i++ ) { v.add(i); } System.out.println("\nVector = " + v); System.out.println("Vector Capacity = " + v.capacity()); System.out.println("Vector Size = " + v.size()); for (int i = 11; i <= 20 ; i++ ) { ...

65. Stack class in java

 //stack_class import java.util.*; class stack_class { public static void main(String[] args) { Stack obj = new Stack(); obj.push(5); obj.push(15); obj.push(25); obj.push(35); obj.push(45); System.out.println("Stack : " + obj); System.out.println("pop(Delete) : " + obj.pop());      } } OUTPUT: Stack : [5, 15, 25, 35, 45] pop(Delete) : 45

64. Hashtable class in JAVA

 //Hashtable class  import java.util.*; class HashtableDemo { public static void main(String[] args) { Hashtable obj = new Hashtable(); obj.put("Apple", "Red"); obj.put("Banana", "Yellow"); System.out.println("The color of Apple is : " + obj.get("Apple")); Enumeration e = obj.keys(); while(e.hasMoreElements()) { String k = e.nextElement().toString(); String v = obj.get(k).toString(); System.out.println("\tkey : " + k + "\tValue : " + v); } } } OUTPUT: The color of Apple is : Red         key : Apple     Value : Red         key : Banana    Value : Yellow