1.序列化:把内存中正在运行的对象写到输出流中
反序列化:从输入流中读取对象
2.需要被序列化操作的类必须实现:java.io.Serializable接口
3.通过transient关键字限制对属性的序列化
private transient String passWord;
4.重写readObject()和writeObject()来控制序列化方式
调用defaultReadObject()和defaultWriteObject()执行默认序列化操作
5.序列化前加密属性
示例:
//加密属性
private byte[] change(byte[] buff){
for(int i=0;i<buff.length;i++){
int b = 0;
for(int j=0;j<8;j++){
int bit = (buff[i]>>j & 1)==0? 1 : 0;
b+=(1<<j)*bit;
}
buff[i]=(byte)b;
}
return buff;
}
private void writeObject(ObjectOutputStream o)throw IOException{
o.defaultWriteObject();//按默认方式序列化
o.writeObject(change(passWord.getByte()));
}
private void readObject(ObjectOutputStream o)throw IOException{
o.defaultReadObject();//按默认方式反序列化
byte[] buff = (byte[])o.readObject();
passWord = new String(change(buff));
}
-------------------------------------------------------
public static void getSerializable() throws IOException,
ClassNotFoundException {
// 序列化
FileOutputStream fs = new FileOutputStream("D:\\a.serializable");
ObjectOutputStream ops = new ObjectOutputStream(fs);
String a = "Object-str";
Date date = new Date();
ops.writeObject(a);
ops.writeObject(date);
ops.close();
// 反序列化
FileInputStream fss = new FileInputStream("D:\\a.serializable");
ObjectInputStream is = new ObjectInputStream(fss);
String as = (String) is.readObject();
Date dates = (Date) is.readObject();
System.out.println(as);
System.out.println(dates);
}
public static void getInputStream() throws IOException {
// 输入流
InputStream is = new FileInputStream("D:\\in.txt");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String data;
while ((data = br.readLine()) != null) {
System.out.println(data);
}
br.close();
// 输出流
OutputStream os = new FileOutputStream("D:\\out.txt");
OutputStreamWriter oss = new OutputStreamWriter(os);
PrintWriter pw = new PrintWriter(oss);
pw.println(data);
pw.close();
}
--------------------------------------------------------------
注册登录示例
public static boolean insertUser(UserInfo userInfo) throws IOException
{
boolean osBoolean = false;
if (userInfo == null || userInfo.getUserName().isEmpty()
|| userInfo.getPassWord().isEmpty())
{
return osBoolean;
}
OutputStream os = new FileOutputStream("F:\\userInfo.xbliuc");
ObjectOutputStream oos = new ObjectOutputStream(os);
List<Map<String, String>> listUsers = new ArrayList<Map<String, String>>();
Map<String, String> maps = new HashMap<String, String>();
maps.put(userInfo.getUserName(), userInfo.getPassWord());
listUsers.add(maps);
if (listUsers.size() > 0)
{
oos.writeObject(listUsers);
osBoolean = true;
}
oos.close();
return osBoolean;
}
public static boolean enterUser(UserInfo userInfo) throws IOException,
ClassNotFoundException
{
boolean isBoolean = false;
if (userInfo == null || userInfo.getUserName().isEmpty()
|| userInfo.getPassWord().isEmpty())
{
return isBoolean;
}
InputStream is = new FileInputStream("F:\\userInfo.xbliuc");
ObjectInputStream ois = new ObjectInputStream(is);
List<Map<String, String>> lists = new ArrayList<Map<String, String>>();
lists = (List<Map<String, String>>)ois.readObject();
if (lists.size() > 0)
{
Map<String, String> maps = lists.get(0);
if (userInfo.getPassWord().equals(maps.get(userInfo.getUserName())))
{
isBoolean = true;
}
}
is.close();
return isBoolean;
}
public static void Test() throws IOException, ClassNotFoundException
{
System.out.print(":");
BufferedReader bra = new BufferedReader(
new InputStreamReader(System.in));
String a = bra.readLine();
int b = Integer.parseInt(a);
System.out.print(": ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String userName = br.readLine();
System.out.print(":");
BufferedReader br2 = new BufferedReader(
new InputStreamReader(System.in));
String passWord = br2.readLine();
UserInfo userInfo = new UserInfo(userName, passWord);
insertUser(userInfo);
enterUser(userInfo);
}
-----------------------------
/*******************************************************************************
* Copyright(c) 2005-2009 Huawei Tech. Co., Ltd.
* All rights reserved.
*
* Author: xbliuc
* Date : 2010-12-19
*******************************************************************************/
package com.rt.io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Date;
/**
* File InputStream
*/
public class InputStreamTest
{
/**
* TODO 添加方法注释
* @param args
* @throws IOException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws IOException,
ClassNotFoundException
{
//getFileText();//字节输入流
//getStringBufferStream();//字符输入流
//copyFile();
//copy();
getSerializable();
}
/**
* 对象序列化和反序列化
* @throws IOException
* @throws ClassNotFoundException
* @throws FileNotFoundException
* @throws ClassNotFoundException
*/
public static void getSerializable() throws IOException,
ClassNotFoundException
{
//创建保存序列化对象的文件
ObjectOutputStream oOutput = new ObjectOutputStream(
new FileOutputStream("D:\\ObjectFile.obj"));
String oStr = "Object";
Date odata = new Date();
oOutput.writeObject(oStr);
oOutput.writeObject(odata);
oOutput.close();
//反序列对象
ObjectInputStream oInput = new ObjectInputStream(new FileInputStream(
"D:\\ObjectFile.obj"));
String str = (String)oInput.readObject();
System.out.println("String : " + str);
Date date = (Date)oInput.readObject();
System.out.println("Date : " + date);
}
//把键盘输入的数据输出到控制台
public static void copy() throws IOException
{
InputStreamReader a = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(a);
String data;
while ((data = br.readLine()) != null && data.length() != 0)
{
System.out.println(data);
System.err.println(data);
}
}
/**
* 输入流种类
* InputStream :字节输入流
* OutputStream : 字节输出流
* ByteArrayInputStream:字节数组输入流
* FileInputStream : 文件输入流
* StringBufferInputStream : 字符串输入流(StringReader)
* SequenceInputStream : 顺序输入流
* FilterInputStream : 过滤输入流
* @throws IOException
*/
public static void getFileText() throws IOException
{
final int SIZE = 1024;
FileInputStream fs = null;
FileOutputStream os = null;
try
{
fs = new FileInputStream("D:\\a.txt");//输出字节流
os = new FileOutputStream("D:\\b.txt");//输入字节流
byte[] bytes = new byte[SIZE];//创建字节缓冲区
int len = fs.read(bytes);//把a.txt文件中的数据读到bytes中
while (len != -1)
{
os.write(bytes, 0, len);//把bytes中的数据写到b.txt中
len = fs.read(bytes);//此时缓冲区bytes数组为空
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
finally
{
fs.close();
os.close();
}
}
/**
* 字符串输入流示例
* TODO 添加方法注释
* @throws IOException
*/
public static void getStringBufferStream() throws IOException
{
final String FORMAT = "GBK";
InputStream input = new FileInputStream("D:\\aa.txt");
InputStreamReader reader = new InputStreamReader(input);
BufferedReader br = new BufferedReader(reader, 1024);
String data = "";
while ((data = br.readLine()) != null)
{
System.out.println(data);
}
br.close();
}
public static void copyFile() throws IOException
{
//编码格式,不添加此参数则按照本地平台的编码读取输入流中的字符
final String FORMAT = "GBK";//UTF-8,
//文件输入流
InputStream in = new FileInputStream("D:\\aa.txt");
//字符输入流
InputStreamReader reader = new InputStreamReader(in, FORMAT);
//带有缓冲区的字符输入流,提高文件读的效率
BufferedReader br = new BufferedReader(reader);
//文件输出流
OutputStream output = new FileOutputStream("D:\\bb.txt");
//字符输出流
OutputStreamWriter write = new OutputStreamWriter(output, FORMAT);
BufferedWriter bw = new BufferedWriter(write);
//输入格式化数据
PrintWriter pw = new PrintWriter(bw, true);
String data;
while ((data = br.readLine()) != null)
{
pw.println(data);//向目标逐行写数据
}
br.close();
pw.close();
}
}
反序列化:从输入流中读取对象
2.需要被序列化操作的类必须实现:java.io.Serializable接口
3.通过transient关键字限制对属性的序列化
private transient String passWord;
4.重写readObject()和writeObject()来控制序列化方式
调用defaultReadObject()和defaultWriteObject()执行默认序列化操作
5.序列化前加密属性
示例:
//加密属性
private byte[] change(byte[] buff){
for(int i=0;i<buff.length;i++){
int b = 0;
for(int j=0;j<8;j++){
int bit = (buff[i]>>j & 1)==0? 1 : 0;
b+=(1<<j)*bit;
}
buff[i]=(byte)b;
}
return buff;
}
private void writeObject(ObjectOutputStream o)throw IOException{
o.defaultWriteObject();//按默认方式序列化
o.writeObject(change(passWord.getByte()));
}
private void readObject(ObjectOutputStream o)throw IOException{
o.defaultReadObject();//按默认方式反序列化
byte[] buff = (byte[])o.readObject();
passWord = new String(change(buff));
}
-------------------------------------------------------
public static void getSerializable() throws IOException,
ClassNotFoundException {
// 序列化
FileOutputStream fs = new FileOutputStream("D:\\a.serializable");
ObjectOutputStream ops = new ObjectOutputStream(fs);
String a = "Object-str";
Date date = new Date();
ops.writeObject(a);
ops.writeObject(date);
ops.close();
// 反序列化
FileInputStream fss = new FileInputStream("D:\\a.serializable");
ObjectInputStream is = new ObjectInputStream(fss);
String as = (String) is.readObject();
Date dates = (Date) is.readObject();
System.out.println(as);
System.out.println(dates);
}
public static void getInputStream() throws IOException {
// 输入流
InputStream is = new FileInputStream("D:\\in.txt");
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String data;
while ((data = br.readLine()) != null) {
System.out.println(data);
}
br.close();
// 输出流
OutputStream os = new FileOutputStream("D:\\out.txt");
OutputStreamWriter oss = new OutputStreamWriter(os);
PrintWriter pw = new PrintWriter(oss);
pw.println(data);
pw.close();
}
--------------------------------------------------------------
注册登录示例
public static boolean insertUser(UserInfo userInfo) throws IOException
{
boolean osBoolean = false;
if (userInfo == null || userInfo.getUserName().isEmpty()
|| userInfo.getPassWord().isEmpty())
{
return osBoolean;
}
OutputStream os = new FileOutputStream("F:\\userInfo.xbliuc");
ObjectOutputStream oos = new ObjectOutputStream(os);
List<Map<String, String>> listUsers = new ArrayList<Map<String, String>>();
Map<String, String> maps = new HashMap<String, String>();
maps.put(userInfo.getUserName(), userInfo.getPassWord());
listUsers.add(maps);
if (listUsers.size() > 0)
{
oos.writeObject(listUsers);
osBoolean = true;
}
oos.close();
return osBoolean;
}
public static boolean enterUser(UserInfo userInfo) throws IOException,
ClassNotFoundException
{
boolean isBoolean = false;
if (userInfo == null || userInfo.getUserName().isEmpty()
|| userInfo.getPassWord().isEmpty())
{
return isBoolean;
}
InputStream is = new FileInputStream("F:\\userInfo.xbliuc");
ObjectInputStream ois = new ObjectInputStream(is);
List<Map<String, String>> lists = new ArrayList<Map<String, String>>();
lists = (List<Map<String, String>>)ois.readObject();
if (lists.size() > 0)
{
Map<String, String> maps = lists.get(0);
if (userInfo.getPassWord().equals(maps.get(userInfo.getUserName())))
{
isBoolean = true;
}
}
is.close();
return isBoolean;
}
public static void Test() throws IOException, ClassNotFoundException
{
System.out.print(":");
BufferedReader bra = new BufferedReader(
new InputStreamReader(System.in));
String a = bra.readLine();
int b = Integer.parseInt(a);
System.out.print(": ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String userName = br.readLine();
System.out.print(":");
BufferedReader br2 = new BufferedReader(
new InputStreamReader(System.in));
String passWord = br2.readLine();
UserInfo userInfo = new UserInfo(userName, passWord);
insertUser(userInfo);
enterUser(userInfo);
}
-----------------------------
/*******************************************************************************
* Copyright(c) 2005-2009 Huawei Tech. Co., Ltd.
* All rights reserved.
*
* Author: xbliuc
* Date : 2010-12-19
*******************************************************************************/
package com.rt.io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Date;
/**
* File InputStream
*/
public class InputStreamTest
{
/**
* TODO 添加方法注释
* @param args
* @throws IOException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws IOException,
ClassNotFoundException
{
//getFileText();//字节输入流
//getStringBufferStream();//字符输入流
//copyFile();
//copy();
getSerializable();
}
/**
* 对象序列化和反序列化
* @throws IOException
* @throws ClassNotFoundException
* @throws FileNotFoundException
* @throws ClassNotFoundException
*/
public static void getSerializable() throws IOException,
ClassNotFoundException
{
//创建保存序列化对象的文件
ObjectOutputStream oOutput = new ObjectOutputStream(
new FileOutputStream("D:\\ObjectFile.obj"));
String oStr = "Object";
Date odata = new Date();
oOutput.writeObject(oStr);
oOutput.writeObject(odata);
oOutput.close();
//反序列对象
ObjectInputStream oInput = new ObjectInputStream(new FileInputStream(
"D:\\ObjectFile.obj"));
String str = (String)oInput.readObject();
System.out.println("String : " + str);
Date date = (Date)oInput.readObject();
System.out.println("Date : " + date);
}
//把键盘输入的数据输出到控制台
public static void copy() throws IOException
{
InputStreamReader a = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(a);
String data;
while ((data = br.readLine()) != null && data.length() != 0)
{
System.out.println(data);
System.err.println(data);
}
}
/**
* 输入流种类
* InputStream :字节输入流
* OutputStream : 字节输出流
* ByteArrayInputStream:字节数组输入流
* FileInputStream : 文件输入流
* StringBufferInputStream : 字符串输入流(StringReader)
* SequenceInputStream : 顺序输入流
* FilterInputStream : 过滤输入流
* @throws IOException
*/
public static void getFileText() throws IOException
{
final int SIZE = 1024;
FileInputStream fs = null;
FileOutputStream os = null;
try
{
fs = new FileInputStream("D:\\a.txt");//输出字节流
os = new FileOutputStream("D:\\b.txt");//输入字节流
byte[] bytes = new byte[SIZE];//创建字节缓冲区
int len = fs.read(bytes);//把a.txt文件中的数据读到bytes中
while (len != -1)
{
os.write(bytes, 0, len);//把bytes中的数据写到b.txt中
len = fs.read(bytes);//此时缓冲区bytes数组为空
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
finally
{
fs.close();
os.close();
}
}
/**
* 字符串输入流示例
* TODO 添加方法注释
* @throws IOException
*/
public static void getStringBufferStream() throws IOException
{
final String FORMAT = "GBK";
InputStream input = new FileInputStream("D:\\aa.txt");
InputStreamReader reader = new InputStreamReader(input);
BufferedReader br = new BufferedReader(reader, 1024);
String data = "";
while ((data = br.readLine()) != null)
{
System.out.println(data);
}
br.close();
}
public static void copyFile() throws IOException
{
//编码格式,不添加此参数则按照本地平台的编码读取输入流中的字符
final String FORMAT = "GBK";//UTF-8,
//文件输入流
InputStream in = new FileInputStream("D:\\aa.txt");
//字符输入流
InputStreamReader reader = new InputStreamReader(in, FORMAT);
//带有缓冲区的字符输入流,提高文件读的效率
BufferedReader br = new BufferedReader(reader);
//文件输出流
OutputStream output = new FileOutputStream("D:\\bb.txt");
//字符输出流
OutputStreamWriter write = new OutputStreamWriter(output, FORMAT);
BufferedWriter bw = new BufferedWriter(write);
//输入格式化数据
PrintWriter pw = new PrintWriter(bw, true);
String data;
while ((data = br.readLine()) != null)
{
pw.println(data);//向目标逐行写数据
}
br.close();
pw.close();
}
}