Published with Blogger-droid v2.0.2
Wednesday, January 4, 2012
Wednesday, December 22, 2010
Read & Write file using java I/O Stream.
/**
* Created by IntelliJ IDEA.
* User : Rajesh.Muralidharen - therajguru06@gmail.com
* Date : Dec 22, 2010
* Time : 6:09:46 PM
* Delimit : Read & Write file using java I/O Stream.
* This example shows how to read and write a file using Java I/O Stream package
* FileInputStream is used to read binary content of the file and
* return bytes of data.
* FileOutputStream is used to write bytes content into a file in the declared location.
*/
import java.io.*;
public class FileInputOutputStreamExample {
public void readFile(){
int ch;
File filePath = new File("D:\\Inputfile.txt");
StringBuffer strContent = new StringBuffer("");
FileInputStream fin = null;
try {
fin = new FileInputStream(filePath);
while( (ch = fin.read()) != -1)
strContent.append((char)ch);
fin.close();
System.out.println("Read successfully completed...");
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println(filePath.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
//command-out the below code to get the read contents!
//System.out.println(strContent);
writeFile(strContent); //invoking Write file.
}
public void writeFile(StringBuffer strContent){
String getName = (String) strContent.toString();
try {
FileOutputStream fou = new FileOutputStream("D:\\Outputfile.txt");
fou.write(getName.getBytes());
fou.flush();
fou.close();
System.out.println("Write successfully completed...");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
FileInputOutputStreamExample fiose = new FileInputOutputStreamExample();
fiose.readFile(); //invoking Read File.
}
}
* Created by IntelliJ IDEA.
* User : Rajesh.Muralidharen - therajguru06@gmail.com
* Date : Dec 22, 2010
* Time : 6:09:46 PM
* Delimit : Read & Write file using java I/O Stream.
* This example shows how to read and write a file using Java I/O Stream package
* FileInputStream is used to read binary content of the file and
* return bytes of data.
* FileOutputStream is used to write bytes content into a file in the declared location.
*/
import java.io.*;
public class FileInputOutputStreamExample {
public void readFile(){
int ch;
File filePath = new File("D:\\Inputfile.txt");
StringBuffer strContent = new StringBuffer("");
FileInputStream fin = null;
try {
fin = new FileInputStream(filePath);
while( (ch = fin.read()) != -1)
strContent.append((char)ch);
fin.close();
System.out.println("Read successfully completed...");
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println(filePath.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
//command-out the below code to get the read contents!
//System.out.println(strContent);
writeFile(strContent); //invoking Write file.
}
public void writeFile(StringBuffer strContent){
String getName = (String) strContent.toString();
try {
FileOutputStream fou = new FileOutputStream("D:\\Outputfile.txt");
fou.write(getName.getBytes());
fou.flush();
fou.close();
System.out.println("Write successfully completed...");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
FileInputOutputStreamExample fiose = new FileInputOutputStreamExample();
fiose.readFile(); //invoking Read File.
}
}
Monday, May 24, 2010
Setting up a Servlet development environment in Servlet Tutorials
This tutorial provides step by step instructions to setup the development environment required to test various servlet examples given throughout all the tutorials. This tutorial will also be helpful when you start practicing servlets yourself.
List of required softwares:
First of all you need the Java software development kit 1.5 or 1.6 (JAVA SDK) installed.
Checking if JAVA is already installed in your system
Follow this step to check if Java software development kit (JDK) is already installed in your system.
To determine if JAVA SDK is already installed in your system, run the following command from command prompt.
Java –version
If JAVA platform is already installed, it will display the version of the JAVA SDK. Below screen shows JAVA version 1.6 running on a windows system. If command executes successfully and shows JAVA version 1.6, you can skip the next step.otherwise download and install the Java SDK as explained in next step.
First thing you need to install is Java software development kit (Java SDK) .Download the Java development kit 6 (JDK 6) from Sun Java SE Download site. Current latest version is JDK 6 update 6.
Set up the JAVA_HOME environment variable
Once the JAVA SDK is installed follow this step to set the JAVA_HOME environment variable. If JAVA_HOME variable is already set, you can skip this step.
Right click on My Computer icon on your desktop it will open a popup menu, click on properties, it will open the system properties window, then click on advanced tab.
Click on environment variables button, it will open the environment variables window as shown in figure below. Initially there will be no JAVA_HOME environment variable set as shown in figure.
Click on new button to add new system variable JAVA_HOME.
In the variable name filed, enter JAVA_HOME. Specify the path to root directory of JAVA installation in variable value field and click OK button. Now JAVA_HOME will appear under user variables.
Next you need to add bin directory under the root directory of JAVA installation in PATH environment variable.Select the PATH variable from System variables and click on Edit button.
Add: ;%JAVA_HOME%\bin; at the end of variable value field and click OK button.
Now verify that everything is correct by following the step: Checking if JAVA is already installed in your system. It should show the version of the installed JAVA SDK.
Installing Tomcat
Tomcat is an opensource web container. it is also web container reference implementation. You will need tomcat 5.5.16 installed on your system to test various servlet and JSP examples given in other tutorials.
Download the latest version of tomcat from this URL . Download the jakarta-tomcat-5.0.28.tar.gz and extract it to the directory of your choice.
That’s all, tomcat is installed. It’s very easy, isn’t it?
Starting and shutting down Tomcat
To start the tomcat server, open the command prompt, change the directory to TOMCAT HOME/bin and run the startup.bat file. It will start the server.
>startup
To shut down the tomcat server, run the shutdown.bat file. It will stop the server.
>shutdown
Verifying Tomcat installation
To verify that tomcat is installed properly, start the server as explained above, open the web browser and access the following URL.
It should show the tomcat welcome page, if tomcat is installed properly and server is running.
Setting up the CLASSPATH
Now you need to create a new environment variable CLASSPATH if it is not already set. We need to add the servlet-api.jar into the CLASSPATH to compile the Servlets. Follow the same steps as you did to create the JAVA_HOME variable. Create a new variable CLASSPATH under system variables. Add TOMCAT_HOME/lib/servlet-api.jar in variable value field.
List of required softwares:
JAVA 1.5 or 1.6
Tomcat 5.5.16
eclipse 3.3
Note: this tutorial explains how to install required software on your windows based PC only.
First of all you need the Java software development kit 1.5 or 1.6 (JAVA SDK) installed.
Checking if JAVA is already installed in your system
Follow this step to check if Java software development kit (JDK) is already installed in your system.
To determine if JAVA SDK is already installed in your system, run the following command from command prompt.
Java –version
If JAVA platform is already installed, it will display the version of the JAVA SDK. Below screen shows JAVA version 1.6 running on a windows system. If command executes successfully and shows JAVA version 1.6, you can skip the next step.otherwise download and install the Java SDK as explained in next step.
Install JAVA SDK
First thing you need to install is Java software development kit (Java SDK) .Download the Java development kit 6 (JDK 6) from Sun Java SE Download site. Current latest version is JDK 6 update 6.
Set up the JAVA_HOME environment variable
Once the JAVA SDK is installed follow this step to set the JAVA_HOME environment variable. If JAVA_HOME variable is already set, you can skip this step.
Right click on My Computer icon on your desktop it will open a popup menu, click on properties, it will open the system properties window, then click on advanced tab.
Click on environment variables button, it will open the environment variables window as shown in figure below. Initially there will be no JAVA_HOME environment variable set as shown in figure.
Click on new button to add new system variable JAVA_HOME.
In the variable name filed, enter JAVA_HOME. Specify the path to root directory of JAVA installation in variable value field and click OK button. Now JAVA_HOME will appear under user variables.
Next you need to add bin directory under the root directory of JAVA installation in PATH environment variable.Select the PATH variable from System variables and click on Edit button.
Add: ;%JAVA_HOME%\bin; at the end of variable value field and click OK button.
Now verify that everything is correct by following the step: Checking if JAVA is already installed in your system. It should show the version of the installed JAVA SDK.
Installing Tomcat
Tomcat is an opensource web container. it is also web container reference implementation. You will need tomcat 5.5.16 installed on your system to test various servlet and JSP examples given in other tutorials.
Download the latest version of tomcat from this URL . Download the jakarta-tomcat-5.0.28.tar.gz and extract it to the directory of your choice.
Note: This directory is referred as TOMCAT_HOME in other tutorials.
That’s all, tomcat is installed. It’s very easy, isn’t it?
Starting and shutting down Tomcat
To start the tomcat server, open the command prompt, change the directory to TOMCAT HOME/bin and run the startup.bat file. It will start the server.
>startup
To shut down the tomcat server, run the shutdown.bat file. It will stop the server.
>shutdown
Verifying Tomcat installation
To verify that tomcat is installed properly, start the server as explained above, open the web browser and access the following URL.
http://localhost:8080/index.jsp
It should show the tomcat welcome page, if tomcat is installed properly and server is running.
Setting up the CLASSPATH
Now you need to create a new environment variable CLASSPATH if it is not already set. We need to add the servlet-api.jar into the CLASSPATH to compile the Servlets. Follow the same steps as you did to create the JAVA_HOME variable. Create a new variable CLASSPATH under system variables. Add TOMCAT_HOME/lib/servlet-api.jar in variable value field.
Note: here TOMCAT_HOME refers to the tomcat installation directory.
Inheritance *entends
/*Java Inheritance*/
class tuttu{
void tuttu_display(){
System.out.print("Shapes\n");
}
}
class Rectangle extends tuttu{
private int l = 2;
private int b = 3;
void display(){
System.out.print("\nArea of Rectangle" + (l*b)+"then\n");
}
}
class Circle extends Rectangle {
private int r = 4;
void display(){
System.out.print("\nArea of Circle" + (3.14f*r*r)+"then\n");
}
}
class Square{
private int a = 5;
void display(){
System.out.print("\nSquare" + (a*a)+"then\n");
}
}
class demo{
public static void main(String args[]){
Rectangle rect = new Rectangle();
rect.tuttu_display();
rect.display();
Circle cri = new Circle();
cri.display();
cri.tuttu_display();
Square squ = new Square();
squ.display();
}
}
Thread?
/*Simple thread pgm*/
class ParentThread{
public static void main(String args[]) throws InterruptedException{
try{
for(int i=0;i<=5;i++){
System.out.print("\n Parent thread "+i);
Thread.sleep(1000);
}
}catch(InterruptedException ie){
System.out.print("Sleep Method Invoked");
}
}
}
output:
Parent thread 0
Parent thread 1
Parent thread 2
Parent thread 3
Parent thread 4
Parent thread 5
class ParentThread{
public static void main(String args[]) throws InterruptedException{
try{
for(int i=0;i<=5;i++){
System.out.print("\n Parent thread "+i);
Thread.sleep(1000);
}
}catch(InterruptedException ie){
System.out.print("Sleep Method Invoked");
}
}
}
output:
Parent thread 0
Parent thread 1
Parent thread 2
Parent thread 3
Parent thread 4
Parent thread 5
Naming a Thread?
/*Give name to Thread*/
output:
Thread[main,5,main]
MyThread
Thread[MyThread,5,main]
class ParentThread{
public static void main(String args[]) throws InterruptedException{
Thread t = Thread.currentThread();
System.out.print(t+"\n");
t.setName("MyThread");
System.out.print(t.getName()+"\n");
System.out.print(t);
}
}
output:
Thread[main,5,main]
MyThread
Thread[MyThread,5,main]
Thread Concept!!!
/*Multithreading with extends Thread*/
class ChildThread extends Thread{
public void run(){
System.out.print("Child thread started...\n");
for(int i=11;i<=15;i++){
System.out.print("\n Child thread "+i);
try{
Thread.sleep(2000);
}catch(InterruptedException ie){
System.out.print(ie);
}
}//for ends
}//Method ends.
}//Class ends.
class ParentThread{
public static void main(String args[]) throws InterruptedException{
ChildThread ct = new ChildThread();
ct.start();
System.out.print("Parent thread started...\n");
for(int i=1;i<=5;i++){
System.out.print("\n Parent Thread "+i);
Thread.sleep(2000);
}
}
}
output:
Parent thread started...
Parent Thread 1Child thread started...
Child thread 11
Parent Thread 2
Child thread 12
Parent Thread 3
Child thread 13
Child thread 14
Parent Thread 4
Child thread 15
Parent Thread 5
class ChildThread extends Thread{
public void run(){
System.out.print("Child thread started...\n");
for(int i=11;i<=15;i++){
System.out.print("\n Child thread "+i);
try{
Thread.sleep(2000);
}catch(InterruptedException ie){
System.out.print(ie);
}
}//for ends
}//Method ends.
}//Class ends.
class ParentThread{
public static void main(String args[]) throws InterruptedException{
ChildThread ct = new ChildThread();
ct.start();
System.out.print("Parent thread started...\n");
for(int i=1;i<=5;i++){
System.out.print("\n Parent Thread "+i);
Thread.sleep(2000);
}
}
}
output:
Parent thread started...
Parent Thread 1Child thread started...
Child thread 11
Parent Thread 2
Child thread 12
Parent Thread 3
Child thread 13
Child thread 14
Parent Thread 4
Child thread 15
Parent Thread 5
Subscribe to:
Posts (Atom)

