Thursday, 22 December 2016
How to read a file in java with example program
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)
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");
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)
Wednesday, 21 December 2016
How to open notepad using java program
Program #1: Java example program to open notepad
- package interestingJavaprograms;
- import java.io.IOException;
- public class NotepadJava {
- /**
- * @ www.corejavawithselenium.blogspot.in
- * @ how to open a new notepad using java program
- */
- public static void main(String[] args) {
- Runtime rt = Runtime.getRuntime();
- try {
- rt.exec("notepad");
- }
- catch (IOException ex) {
- System.out.println(ex);
- }
- }
- }
Tuesday, 20 December 2016
How to sort arraylist of strings alphabetically java
1.Basic Java example program to sort arraylist of strings
OUTPUT::
- package com.javasortarraylistofobjects;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Iterator;
- public class SortArrayList{
- public static void main(String[] args) {
- //create an ArrayList object
- ArrayList<String> arrayList = new ArrayList();
- //Add elements to Arraylist
- arrayList.add("A");
- arrayList.add("C");
- arrayList.add("D");
- arrayList.add("Z");
- arrayList.add("F");
- arrayList.add("J");
- arrayList.add("K");
- arrayList.add("M");
- arrayList.add("L");
- arrayList.add("O");
- System.out.println("Before sorting ArrayList ...");
- Iterator itr=arrayList.iterator();
- while (itr.hasNext()) {
- System.out.println(itr.next());
- }
- /*
- To sort an ArrayList object, use Collection.sort method. This is a
- static method. It sorts an ArrayList object's elements into ascending order.
- */
- Collections.sort(arrayList);
- System.out.println("After sorting ArrayList ...");
- Iterator itr1=arrayList.iterator();
- while (itr1.hasNext()) {
- System.out.println(itr1.next());
- }
- }
- }
OUTPUT::
- Before sorting ArrayList ...
- A
- C
- D
- Z
- F
- J
- K
- M
- L
- O
- After sorting ArrayList ...
- A
- C
- D
- F
- J
- K
- L
- M
- O
- Z
Monday, 19 December 2016
How to find largest element in an array with index and value using array
How to find largest element in an array with index and value using array?
package com.inofjava;
public class Array {
public static void main(String[] args) {
int arr[]={1,120,56,78,87};
int largest=arr[0];
int smallest=arr[0];
int small=0;
int index=0;
for(int i=1;i<arr.length;i++){
if(arr[i]>largest){
largest=arr[i];
index=i;
}
else if(smallest>arr[i]){
smallest=arr[i];
small=i;
}
}
System.out.println(largest);
System.out.println(index);
System.out.println(smallest);
System.out.println(small);
}
}
Output:
120
1
87
4
package com.inofjava;
public class Array {
public static void main(String[] args) {
int arr[]={1,120,56,78,87};
int largest=arr[0];
int smallest=arr[0];
int small=0;
int index=0;
for(int i=1;i<arr.length;i++){
if(arr[i]>largest){
largest=arr[i];
index=i;
}
else if(smallest>arr[i]){
smallest=arr[i];
small=i;
}
}
System.out.println(largest);
System.out.println(index);
System.out.println(smallest);
System.out.println(small);
}
}
Output:
120
1
87
4
Sunday, 18 December 2016
Encapsulation in java with example program
What is meant by encapsulation in java?
- Binding the data with its related functionalities known as encapsulation
- Here data means variables and functionalities means methods.
- So keeping the variable and related methods in one place.
- That place is "class". class is the base for encapsulation.
- Lets see example program on encapsulation, how the variables and methods defined in a class
What is encapsulation in object oriented programming?
Basic java example program on data encapsulation:
- package com.java;
- public class EncapsulationDemo {
- String name;
- int rno;
- String address;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getRno() {
- return rno;
- }
- public void setRno(int rno) {
- this.rno = rno;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- public void showInfo(){
- System.out.println("Name: "+getName());
- System.out.println("R.No: "+getRno());
- System.out.println("Name: "+getAddress());
- }
- }
Class:
The above example program shows how the variables and methods will be there in a class.
So class is the base for encapsulation
class is user defined data type in java means by using class we can structure our own programs.
Lest see a example program on class.
- package com.java;
- public class ClassDemo {
- //variables of a class
- String empName;
- int empId;
- String Designation;
- //getter and setter methods for variables
- //by using these methods we can set the value to the variable and gat the value from the
- //variables
- public String getEmpName() {
- return empName;
- }
- public void setEmpName(String empName) {
- this.empName = empName;
- }
- public int getEmpId() {
- return empId;
- }
- public void setEmpId(int empId) {
- this.empId = empId;
- }
- public String getDesignation() {
- return Designation;
- }
- public void setDesignation(String designation) {
- Designation = designation;
- }
- }
Object:
Instance of class is known as object.
Instance means dynamic memory allocation. So dynamic memory allocation to class is called object.
By using "new" keyword the object will be created dynamically.
Class is a template. By using object we will get memory for all variables so that we can use them.
We can create number of objects for one class based on our requirements.
Below an example program on class and object.
- package com.java;
- public class User {
- String Name;
- int Id;
- String address;
- public String getName() {
- return Name;
- }
- public void setName(String name) {
- Name = name;
- }
- public int getId() {
- return Id;
- }
- public void setId(int id) {
- Id = id;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- public static void main(String [] args){
- User obj= new User();
- obj.setName("sai");
- obj.setId(1);
- obj.setAddress("xyz");
- System.out.println(obj.getName());
- System.out.println(obj.getId());
- System.out.println(obj.getAddress());
- }
- }
Output:
- sai
- 1
- xyz
Subscribe to:
Posts (Atom)