package Collections_demo.bean;
import java.io.Serializable;
/**
* Serializable:用于给被序列化的类加入ID号。
* 用于判断类和对象是否是同一版本
*/
public class Person implements Serializable/**标记接口 */ {
private transient String name;//transient短暂的,非静态数据不想被序列化可以用这个关键字注释
private int age;
public Person(String name,int age){
this.name = name;
this.age = age;
}
public String getName(){
return this.name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
}
class Input implements Runnable{
private PipedInputStream in;
Input(PipedInputStream in){
this.in = in;
}
@Override
public void run() {
try{
byte[] buf = new byte[1024];
int len = in.read(buf);
String s = new String(buf,0,len);
System.out.println("s:"+s);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Output implements Runnable{
private PipedOutputStream out;
Output(PipedOutputStream out){
this.out = out;
}
@Override
public void run() {
try{
Thread.sleep(1000);
out.write("hi pipe".getBytes());
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
对象序列化
public static void writeObj() throws IOException{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("print.object"));
//对象序列化.被序列化的对象必须实现Serilizable
oos.writeObject(new Person("pp", 24));
oos.close();
System.out.print("end");
}
对象反序列化
//对象反序列化
public static void readObj() throws IOException,ClassNotFoundException{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/ideas/VSCode/CppCode/JavaTest/Collections_demo/print.object"));
Person p = (Person)ois.readObject();
System.out.print(p.getName()+":"+p.getAge());
ois.close();
}
//关键字:transient
使用RandomAccessFile对象写入
//使用RandomAccessFile对象写入一些人员信息,比如姓名和年龄
public static void writeFile() throws IOException{
/**
* 如果文件不存在则创建,如果文件存在,不创建
*/
RandomAccessFile raf = new RandomAccessFile("D:/ideas/VSCode/CppCode/JavaTest/demo/src/my/demo/print.txt", "rw");
raf.seek(3*8);
//往指定位置写入数据
raf.write("张三风".getBytes());
// raf.write(97);
raf.writeInt(23);
// raf.writeInt(23);
raf.close();
}
使用RandomAccessFile读
//使用RandomAccessFile读
public static void readFile() throws IOException {
RandomAccessFile raf = new RandomAccessFile("D:/ideas/VSCode/CppCode/JavaTest/demo/src/my/demo/print.txt","r");
//通过seek设置指针位置
raf.seek(3*8);
byte[] buf = new byte[9];
raf.read(buf);
int age = raf.readInt();
String name = new String(buf);
System.out.println("name:"+name);
System.out.println("age:"+age);
System.out.println("pos:"+raf.getFilePointer());
System.out.println("length:"+raf.length());
raf.close();
}
IO流(管道流-pipedStream)
/*
PipedStream
*/
public static void pipeDemo() throws IOException {
//创建管道流对象
PipedInputStream input = new PipedInputStream();
PipedOutputStream output = new PipedOutputStream();
input.connect(output);
new Thread(new Input(input)).start();
new Thread(new Output(output)).start();
}
操作基本数据类型的流对象
/**
* 操作基本数据类型的流对象
* @throws IOException
*/
public static void writeDataDemo() throws IOException {
DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
dos.writeUTF("你好");
dos.close();
}
/**
* DataInputStream
* @throws IOException
*/
public static void readData() throws IOException {
DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
String str = dis.readUTF();
System.out.println(str);
dis.close();
}
操作数组的流
/**
* 操作数组的流
* @throws IOException
*/
public static void byteArrayStreamDemo() throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream("abcdefg".getBytes());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int ch = 0;
while ((ch=bais.read())!=-1){
baos.write(ch);
}
System.out.println(baos.toString());
}
简单编码解码
/**
* 简单编码解码
* @throws UnsupportedEncodingException
*/
public static void Code() throws UnsupportedEncodingException {
String str = "你好";
//编码
byte[] buf = str.getBytes("UTF-8");
printBytes(buf);
//解码
String s1 = new String(buf,"UTF-8");
System.out.println(s1);
}
private static void printBytes(byte[] buf) {
for(byte b:buf){
System.out.print(b+" ");
}
}
//按字节截取字符串
public static String cutStringByByte(String str,int len) throws UnsupportedEncodingException {
byte[] buf = str.getBytes("GBK");
int count = 0;
for(int x=len-1;x>=0;x--){
if(buf[x]<0){
count++;
}
else
break;
}
if(count%2==0){
return new String(buf,0,len,"GBK");
}
else
return new String(buf,0,len-1,"GBK");
}