Sunday, July 27, 2008

PipedInput Stream and PipedOutputStream

import java.io.*;
// reads numbers from input stream and compute running average
class WriteToFile extends Thread
{
private DataInputStream in;
String msg;
private FileOutputStream out;
File f1=new File("C:\\Program Files\\Java\\jdk1.5.0_14\\test\\AJP","output.txt");



public WriteToFile(InputStream i)
{
in=new DataInputStream(i);
try
{
out = new FileOutputStream(f1);
}catch(FileNotFoundException e) { e.printStackTrace();}

}
public void run()
{
String CRLF = "\n";
try {
int count;
while (true)
{

byte buffer[] = new byte[50];

count=in.read(buffer,0,50);
if (count==-1) break;
// System.out.println("Available bytes to read : " + count);
msg = new String(buffer,0,count);


if (count==-1 ||msg.equalsIgnoreCase("EOI") ) { in.close(); break;}
if (count>0 && msg.equalsIgnoreCase("EOI")==false)
{

out.write(buffer,0,count);
if (in.available()==0)
out.write(CRLF.getBytes());
}
}
out.flush();
} catch (Exception e) { e.printStackTrace();}

}

}
class ReadFromCommandLine extends Thread {
private DataOutputStream out;
private BufferedReader buf;
String msg;
public ReadFromCommandLine(OutputStream o) {
out = new DataOutputStream(o);
buf = new BufferedReader(new InputStreamReader(System.in));

}
public void run() {


try {
System.out.println("Please enter the text in multiple lines to write to a file:");
while (true)
{
msg = buf.readLine();

byte b[]=msg.getBytes();
out.write(b);
out.flush();
// System.out.println(msg);
if (msg.equalsIgnoreCase("EOI"))
{
out.close();
break;
}

}


} catch (Exception e) { e.printStackTrace();}

}

}

class MainPP
{
public static void main(String[] args)
{

try {
PipedOutputStream inputreader =new PipedOutputStream();
PipedInputStream filewriter =new PipedInputStream(inputreader);

ReadFromCommandLine readCommandLine = new ReadFromCommandLine(inputreader);
WriteToFile writeFile = new WriteToFile(filewriter);
readCommandLine.setPriority(1);
readCommandLine.start();
writeFile.start();
} catch (IOException e) {e.printStackTrace();}
}
}

Thursday, July 24, 2008

BufferedInputStream,BufferedOutputStream,FileInputStream,FileOutputStream


import java.io.*;
class TestByteStreams{
public static void main(String args[]) throws IOException
{

File f1=new File("C:\\Program Files\\Java\\jdk1.5.0_14\\test\\AJP","BoxArray.java");

System.out.println("Path seperator=" + f1.pathSeparator);

System.out.println("seperator=" + f1.separator);

FileInputStream fInputStream = new FileInputStream(f1);
BufferedInputStream bInputStream = new BufferedInputStream(fInputStream);
byte byteInputArray [] = new byte[500];
FileOutputStream fOutputStream = new FileOutputStream (f1 + "_copy2.java");
BufferedOutputStream bOutputStream = new BufferedOutputStream(fOutputStream,1024);
int count;
while ((count = bInputStream.read(byteInputArray,0,500))>0)

{
//BufferedOutputStream data is written to file or out when the buffer size = 1024 specified in the bufferedoutputstream is reached.
bOutputStream.write(byteInputArray,0,count);

}
//Once reading is complete and byteInputArray filled size is less than 1024 buffered output stream size,
//the output will not be written unless flushed exclusively
bOutputStream.flush();

fInputStream.close();
fOutputStream.close();


/*ByteArrayOutputStream bo = new ByteArrayOutputStream();
String s1 = "Twinkle twinkle litte star";
String s2 = "How I wonder what you are";
byte b1[] = s1.getBytes();
byte b2[] = s2.getBytes();
bo.write(b1);
bo.write(b2);
bo.close();
byte b3[] = bo.toByteArray();
ByteArrayInputStream bi = new ByteArrayInputStream(b3);
int size = bi.available();
for (int i=0; i{
System.out.print((char) bi.read());
}
System.out.println();*/


}

}


Monday, July 14, 2008

Calendar Application

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

class MyAdapter implements ActionListener
{

FrameCalendar fc;

public MyAdapter(FrameCalendar fc1)
{
fc=fc1;
}
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource()==fc.btnDisplay)
{
int month,year;
GregorianCalendar c = new GregorianCalendar();
try
{
month=Integer.parseInt(fc.tMonth.getText());
year=Integer.parseInt(fc.tYear.getText());
} catch (NumberFormatException e)
{ month=c.get(Calendar.MONTH)+1;
year=c.get(Calendar.YEAR);
}
c.set(Calendar.DATE,1);
c.set(Calendar.MONTH,month-1);
c.set(Calendar.YEAR,year);
printCalendar(c);
}
}

private void printCalendar(GregorianCalendar c)
{
String DAY_OF_WEEK[] = {"Su","Mo","Tu","We","Th","Fr","Sa"};
int monthLength[]={31,28,31,30,31,30,31,31,30,31,30,31};
int MonthArray[][]=new int[6][7];
int month,year,date;
for (int i=0;i<6;i++)
{
Arrays.fill(MonthArray[i],0);
}
c.set(Calendar.DATE,1);
date=c.get(Calendar.DATE);
month=c.get(Calendar.MONTH);
year=c.get(Calendar.YEAR);

int i;

for (i=0;i{
System.out.print(DAY_OF_WEEK[i]+"\t");
fc.ta.append(DAY_OF_WEEK[i]+"\t");
}
System.out.println();
if ((month==1) && (c.isLeapYear(year)))
monthLength[1]=29;
else
monthLength[1]=28;

for (i=1;i<=monthLength[month];i++)
{
MonthArray[c.get(Calendar.WEEK_OF_MONTH)-1][c.get(Calendar.DAY_OF_WEEK)-1]=i;
c.roll(Calendar.DATE,1);
}
for (i=0;i<6;i++)
{
System.out.println();
fc.ta.append("\n");
for (int j=0;j<7;j++)
{
if (MonthArray[i][j]==0)
{
System.out.print("\t");
fc.ta.append("\t");
}
else
{
System.out.print(MonthArray[i][j] + "\t");
fc.ta.append(MonthArray[i][j] + "\t");
}
}
}
}
}
public class FrameCalendar extends Frame
{
Frame f1;
Label l1;
Button btnDisplay;
Panel p1,p2;
TextField tMonth, tYear;
TextArea ta;

public FrameCalendar(){
f1=new Frame();
f1.setSize(400,400);
l1=new Label("Please enter Month and Year");
btnDisplay=new Button("Show");
tMonth=new TextField("Enter Month");
tYear=new TextField("Enter Year");
p1=new Panel();
p1.add(l1);
p1.add(btnDisplay);
p1.add(tMonth);
p1.add(tYear);
f1.add(p1);
p2=new Panel();
ta = new TextArea("",10,50,0);
p2.add(ta);
f1.add(p2);
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p2.setLayout(new FlowLayout(FlowLayout.CENTER));
f1.setLayout(new GridLayout(2,1));
f1.setVisible(true);
p1.setVisible(true);
p2.setVisible(true);
btnDisplay.addActionListener(new MyAdapter(this));
}

public static void main(String args[])
{
FrameCalendar fc = new FrameCalendar();
}
}

Tuesday, July 8, 2008

Wrapper classes for primitive types

Constructors
Integer (int i)
Integer(String s)

Methods to Convert primitive integer type to Integer Object
public static Integer decode(String s)
public static Integer valueOf(String s) => The string format can be number, leading 0 for octal, # or 0x for Hexadecimal
public static Integer valueOf(String s, int radix)

Methods to convert Integer Object to different primitive types
public byte byteValue()
public double doubleValue()
public float floatValue()
public int intValue()
public long longValue()
public short shortValue()

Methods for converting a String to primtive type
public int parseInt(String s)
public int parseInt(String s, int radix)

Methods for converting a primitive type to String
public static String toString(int i)
public static String toBinaryString(int i)
public static String toHexString(int i)
public static String toOctalString(int i)

Methods to compare Integer Object
public boolean equals(Object x)
public int compareTo(Integer x)

Integer.MIN_VALUE and Integer.MAX_VALUE fields to compare max and min values for a particular Object type.

This is similar for Short, byte, boolean, long, double, float.