Wednesday, 14 February 2018

How to do custom serialization in java.



Firstly why we need custom serialization ?.
.look😁 we we serialized the object there may the chance of loosing some information due to the transient keyword,to overcome this problem we go for custom serialization.

For custom serialization we have to override the writeOjbect() method and inside the method we have to encrypt the information.



custom.java

package Serializations;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class custom implements Serializable {

String uname="praveen";
transient String pass="India";
transient int pin=1235;


private void writeObject(ObjectOutputStream oos) throws IOException
{
oos.defaultWriteObject();
String epass="123"+pass;
int epin=2222+pin;
oos.writeObject(epass);
oos.writeInt(epin);
}

private void readObject(ObjectInputStream ois) throws Exception
{
ois.defaultReadObject();
String rrpass=(String)ois.readObject();
pass=rrpass.substring(3);
int epin=ois.readInt();
pin=epin-2222;

}
}

Demo.java


package Serializations;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Demo implements Serializable{



public static void main(String[] args) throws IOException, ClassNotFoundException {

custom d = new custom();

FileOutputStream fos = new FileOutputStream("serial.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(d);

FileInputStream fis = new FileInputStream("serial.txt");
ObjectInputStream ois = new ObjectInputStream(fis);

custom d2=(custom)ois.readObject();
System.out.println(d2.uname+"...."+ d2.pass +"..."+ d2.pin);
}

}


When you run Demo class.Output would be:

praveen....India...1235.

and serial.ser file will be create inside classpath.



count character occurrence in string java


Ans..

Here we can count the character occurrence in String in very simple way. first we have to declare String that for every character and count of that character we have to define Hashmap type of Character and for count Integer as value.then Iterate through String value and assign the value in char c by using for loop.

after that check the key if key in present than get the value of the key and store it into int count variable  and put the value key as Ch and value as count+1;
if key as ch is not available in Map than add the key and value as 1.

See below program.
for video go  click Here

import java.util.HashMap;

public class StringCount {

public static void main(String[] args) {
String str = "Programming22222";
HashMap<Character, Integer> map = new HashMap<>();
for (char ch : str.toCharArray()) {
if (map.containsKey(ch)) {
int count = map.get(ch);
map.put(ch, count + 1);
} else {
map.put(ch, 1);
}
}
System.out.println(map);
}
}

session-per-request in hibernate

The most common pattern in a multi-user client/server application is session-per-request . In this model, a request from the client is sent...