Wednesday, August 14, 2013

Reading file using OneByte in java

import java.io.*;

public class OneByteTest4 {
public static void main(String[] args) throws Exception{
File f = new File("c:\\FileTest\\bbb.txt");
FileInputStream fis1 = new FileInputStream(f);
BufferedInputStream bis1 = new BufferedInputStream(fis1);
DataInputStream dis1 = new DataInputStream(bis1);

/*
int a = dis1.readInt();
double b = dis1.readDouble();
char c = dis1.readChar();
byte d = dis1.readByte();
byte e = dis1.readByte();

System.out.println("a = "+a);
System.out.println("b = "+b);
System.out.println("c = "+c);
System.out.println("d = "+d);
System.out.println("e = "+e);
*/
while(true){
int x = dis1.read();
if(x==-1) break;  //end of file

System.out.println("x = "+x);
}



}

}

Read file using OneByte in java

import java.io.*;

public class OneByteTest3 {
public static void main(String[] args) throws Exception{
FileInputStream fis1 = new FileInputStream(FileDescriptor.in);
FileInputStream fis2 = new FileInputStream(new File("c:\\FileTest\\aaa.txt"));

/*
System.out.println("Char = ");
char ch = (char)fis1.read();
System.in.read();

System.out.println("String = ");
byte[] by = new byte[1024];
fis1.read(by);

System.out.println("ch = "+ch);
System.out.println("String = "+new String(by).trim());
*/
while(true){
int x = fis2.read();
if(x==-1) break;  //end of file
System.out.print((char)x);
}



}

}

Write File using OneByte in java

import java.io.*;

public class OneByteTest2 {
public static void main(String[] args) throws Exception{
FileOutputStream fos1 = new FileOutputStream(FileDescriptor.out);
BufferedOutputStream bos1 = new BufferedOutputStream(fos1,2048);
DataOutputStream dos1 = new DataOutputStream(bos1);

dos1.writeInt(20);
dos1.writeDouble(12.34);
dos1.flush();
dos1.writeChar('A');
dos1.writeByte('\n');
dos1.writeChar('B');
dos1.close();

FileOutputStream fos2 = new FileOutputStream(new File("C:\\FileTest\\bbb.txt"));
BufferedOutputStream bos2 = new BufferedOutputStream(fos2,2048);
DataOutputStream dos2 = new DataOutputStream(bos2);

dos2.writeInt(20);
dos2.writeDouble(12.34);
dos2.writeChar('A');
dos2.writeByte('\n');
dos2.writeChar('B');
dos2.close();







}

}

Write file using OneByte in java

import java.io.*;

/*
 * FileOutputStream fos;
 * BufferedOutputStream bos;
 * DataOutputStream dos;
 */


public class OneByteTest1 {
public static void main(String[] args) throws Exception {
FileOutputStream fos1 = new FileOutputStream(FileDescriptor.out); //stdout
File f = new File("C:\\FileTest\\bbb.txt");

FileOutputStream fos2 = new FileOutputStream(f,true); //file writing

byte[] by = new byte[]{'H','E','L','L','O',' ','J','A','V','A'};
fos1.write(by);
fos1.write(10);
fos1.write(by,6,4);
fos1.write(10);
fos1.write(65);

fos2.write(by);

fos1.close();
fos2.close();



}

}

Reading File using TwoByte in java

import java.io.*;

public class TwoByteTest2 {
public static void main(String[] args) throws Exception{

/*Standard Input*/
//InputStreamReader isr = new InputStreamReader(System.in);
/*
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("String = ");
String str = br.readLine();

System.out.print("Number = ");
int x = Integer.parseInt(br.readLine());

System.out.println("String = "+str);
System.out.println("Number = "+x);
br.close();
*/

/*File Input*/
File f = new File("C:\\FileTest\\kkk.txt");
FileReader fr = new FileReader(f);
BufferedReader br2 = new BufferedReader(fr,2048);
while(true){
String s = br2.readLine();
if(s==null) break;
System.out.println(s);
}
br2.close();
fr.close();

}
}

Write data into file using TwoByte in java

import java.io.*;

public class TwoByteTest1 {

public static void main(String[] args) throws Exception{
/*Standard output*/
OutputStreamWriter osw1 = new OutputStreamWriter(System.out);
BufferedWriter bw1 = new BufferedWriter(osw1);
PrintWriter pw1 = new PrintWriter(bw1);

pw1.println(10);
pw1.println("Hello! World!");
pw1.println(12.23);

   //pw1.flush();
pw1.close();
bw1.close();
osw1.close();

/*File Output*/
File f = new File("C:\\FileTest\\kkk.txt");
FileWriter fw1 = new FileWriter(f);
BufferedWriter bw2 = new BufferedWriter(fw1,1024);
PrintWriter pw2  = new PrintWriter(bw2);

//writing work....
pw2.println(10);
pw2.println("Hello! World!");
pw2.println(12.23);


pw2.close();
bw2.close();
fw1.close();

}

}

Example of using Scanner class in java

import java.util.*;

/*
 * java ScannerTest2 10 20 30 40
 * Count = 4
 * Total = 100
 * Avg = 25
 */
public class ScannerTest2 {
public static void main(String[] args) {
if(args.length<1){
System.exit(0);
}

String str ="";
for(int i=0;i<args.length;i++){
str = str+args[i]+ " ";
}

Scanner in = new Scanner(str);
int i=0, tot=0;
while(in.hasNextInt()){
i++;
tot += in.nextInt();
}

System.out.println("Count = "+i);
System.out.println("Total = "+tot);
System.out.println("Avg = "+tot/i);

}

}

A simple example of using Scanner in java

/*
 * scanf(c)  == Scanner(java)
 *
 * int num;
 * char c1[100];
 * scanf("number? %d",&num);
 * scanf("string? %s",c1);
 *
 *
 *
 * char tmp[100];
 * int num;
 *
 * File *fp;
 * fp = fopen("aaa.txt","rb");
 * fgets(tmp,100,stdin);  //fgets(tmp,100,fp);
 * num = atoi(tmp);
 *
 */

import java.util.*;

public class ScannerTest {
public static void main(String[] args) throws Exception{
Boolean bool=true;
Scanner in = new Scanner(System.in);
System.out.print("String = ");
String str = in.next();

System.out.print("Number = ");
int x = in.nextInt();

System.out.print("Double = ");
double y = in.nextDouble();

System.out.println("str = "+str);
System.out.println("x = "+x);
System.out.println("y = "+y);

String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
//StringTokenizer
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
}
}


How to read data from file using Serialize in java

This is an example of reading data from file.

import java.io.*;

public class ObjectTest2 {

public static void main(String[] args) throws Exception{
File f = new File("C:\\FileTest\\Obj.txt");
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis,1024);
ObjectInputStream ois = new ObjectInputStream(bis);

Object obj=null;

try{
obj = ois.readObject();
}catch(ClassNotFoundException e){}

AAA ap = (AAA)obj;

System.out.println("ap.x = "+ap.x);
System.out.println("ap.y = "+ap.y);
System.out.println("ap.z = "+ap.z);

ap.disp();


}

}

How to write data into file using Serializable in java

This is simple example if writing data into file:

import java.io.*;

class AAA implements Serializable{
int x = 100;
int y = 200;
int z = 300;
public void disp(){
System.out.println("x = "+x);
System.out.println("y = "+y);
System.out.println("z = "+z);
}

}

public class ObjectTest1 {
public static void main(String[] args) throws Exception {
AAA ap = new AAA();

File f = new File("C:\\FileTest\\Obj.txt");
FileOutputStream fos = new FileOutputStream(f);
BufferedOutputStream bos = new BufferedOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(bos);

oos.writeObject(ap);
oos.close();
bos.close();
fos.close();

}

}

Java Bean

JavaBeans is an object-oriented programming interface from Sun Microsystems that lets you build re-useable applications or program building blocks called components that can be deployed in a network on any major operating system platform. Like Java applets, JavaBeans components (or "Beans") can be used to give World Wide Web pages (or other applications) interactive capabilities such as computing interest rates or varying page content based on user or browser characteristics.
From a user's point-of-view, a component can be a button that you interact with or a small calculating program that gets initiated when you press the button. From a developer's point-of-view, the button component and the calculator component are created separately and can then be used together or in different combinations with other components in different applications or situations.
When the components or Beans are in use, the properties of a Bean (for example, the background color of a window) are visible to other Beans and Beans that haven't "met" before can learn each other's properties dynamically and interact accordingly.
Beans are developed with a Beans Development Kit (BDK) from Sun and can be run on any major operating system platform inside a number of application environments (known ascontainers), including browsers, word processors, and other applications.
To build a component with JavaBeans, you write language statements using Sun's Javaprogramming language and include JavaBeans statements that describe componentproperties such as user interface characteristics and events that trigger a bean to communicate with other beans in the same container or elsewhere in the network.
Beans also have persistence, which is a mechanism for storing the state of a component in a safe place. This would allow, for example, a component (bean) to "remember" data that a particular user had already entered in an earlier user session.
JavaBeans gives Java applications the compound document capability that the OpenDoc andActiveX interfaces already provide.