Wednesday, September 2, 2015

Character Stream

------------------------------Main.java-------------------------

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Main
{
public static void main(String[] args) throws IOException {

// Stream is used to perform input and output of 16-bit unicode.
// FileReader use the FileInputStream and FileWriter uses the FileOutputStream.
// FileReader read two byte at a time and FileWriter write two byte at a time.

FileReader in = null;
FileWriter out = null;
try {
in = new FileReader("input.txt");
out = new FileWriter("output.txt");

int readChar;
while((readChar = in.read()) != -1){
out.write(readChar);
}
System.out.println("\nSuccessfully copied/write the text of input.txt to the output.txt file");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
in.close();
out.close();
}
}
}

--------------------------OUTPUT----------------------

Byte Streams

-------------------------Main.java------------------

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main
{
public static void main(String[] args) throws IOException
{
// Stream represents the input source and output destination
// InputStream is used to read data from the source and
// OutputStream is used to write data to the destination

// Byte Stream is used to perform input and output of 8-bit byte

FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream("input.txt");
fileOutputStream = new FileOutputStream("output.txt");

int text ;
while((text = fileInputStream.read()) != -1){
fileOutputStream.write(text);
// Copying the text value of input.txt to output.txt
}
System.out.println("\nSuccessfully copied the text of input.txt to the output.txt file");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fileInputStream != null){
fileInputStream.close();
}
if(fileOutputStream != null){
fileOutputStream.close();
}
}
}
}

---------------------Output----------------------

Tuesday, June 9, 2015

HttpURLConnection


import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


public class URLCOnnectionDemo
{
public static void main(String[] args)
{
try {
URL url = new URL("http://www.javatpoint.com/java-tutorial");
URLConnection urlCon = url.openConnection();
InputStream stream = urlCon.getInputStream();
int i;
while((i = stream.read()) != -1){
System.out.print((char)i);
}

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}


Thread Methods

Thread Methods:

public void start()
public void run()
public final void setName(String name)
public final void setPriority(int priority)
public final void setDaemon(boolean on)
public final void join(long millisec)
public void interrupt()
public final boolean isAlive()


The previous methods are invoked on a particular Thread object. The following methods in the Thread class are static. Invoking one of the static methods performs the operation on the currently running thread.


Methods with Description

public static void yield()
public static void sleep(long millisec)
public static boolean holdsLock(Object x)
public static Thread currentThread()
public static void dumpStack()

Extending Thread Class


public class MainActivity
{
public static void main(String[] args)
{
ExtendingThreadClass obj = new ExtendingThreadClass("THREAD_A");
obj.run();

ExtendingThreadClass obj2 = new ExtendingThreadClass("THREAD_B");
obj2.run();
}
}

----------------------------------------------


public class ExtendingThreadClass extends Thread{

Thread t ; 
String threadName;
public ExtendingThreadClass(String threadName) 
{
this.threadName = threadName;
System.out.println("Creating thread obj: "+ threadName);
}
@Override
public void run() 
{
super.run();
System.out.println("Running thread: "+ threadName);
try {
for(int i = 4; i > 0 ; i-- ){
System.out.println("thread: "+ threadName +" "+  i);
Thread.sleep(50);
}
} catch (InterruptedException e) {
System.out.println("Thread: "+ threadName + " interrupted.");
e.printStackTrace();
}
System.out.println("Thread: "+ threadName +" is exiting");
}
public void start()
{
System.out.println("Starting: "+ threadName);
if(t == null){
t = new Thread(this, threadName);
t.start();
}
}
}

--------------------------------------------------------


Thread Runnable Interface


public class ThreadWidRunnableInterface implements Runnable{

Thread t ;
String threadName;

public ThreadWidRunnableInterface(String threadName) {
this.threadName = threadName;
System.out.println("Creating thread named : "+ threadName);
}

@Override
public void run() {
System.out.println("Running thread: "+ threadName);
try {
for (int i = 4; i > 0 ; i--) {
System.out.println("Thread "+ threadName + " "+ i);
Thread.sleep(50);
}
Thread.sleep(50);
} catch (InterruptedException e) {
System.out.println("Thread: "+ threadName+"interrupted");
e.printStackTrace();
}
System.out.println("Thread: "+threadName+"exiting");
}

public void start() {
System.out.println("Starting: "+ threadName);
if( t == null ){
t = new Thread(this, threadName);
t.start();
}
}
}



Thread Lifecycle