-------android培训 、java培训、期待与您交流! ----------
SequenceInputStream:序列流:
将多个读取流合并成一个读取流
将多个读取流合并成一个读取流
package com.heima.IO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;
/**
* 串联流:合并文件
*
*/
public class SequenceInputstreamDemo {
public static void main(String[] args) {
try {
Vector<FileInputStream> v = new Vector<FileInputStream>();
for (int i = 1; i <= 5; i++) {
v.add(new FileInputStream("i" + ".part"));
}
combineFile(v, new FileOutputStream(new File("2.avi")));
} catch (Exception e) {
e.printStackTrace();
}
// splitFile(new File("1.avi"));
}
// 合并源文件
public static void combineFile(Vector<FileInputStream> v,
FileOutputStream fos) {
SequenceInputStream sis = null;
try {
Enumeration<FileInputStream> en = v.elements();
sis = new SequenceInputStream(en);
byte[] buff = new byte[1024];
int len = 0;
while ((len = sis.read(buff)) != -1) {
fos.write(buff, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
sis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 分割源文件
public static void splitFile(File file) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file);
byte[] buff = new byte[1024 * 1024];// 1M为一块
int len = 0;
int count = 1;
while ((len = fis.read(buff)) != -1) {
fos = new FileOutputStream((count++) + ".part");
fos.write(buff, 0, len);
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
PipedStream:管道流
管道读取流和管道写入流可以像管道一样对接上,管道读取流就可以读取管道写入流写入的数据
package com.heima.IO;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
/**
* 管道流
* 以前我们读写数据都是从读取流读出来放在数组或字符串里然后通过输出流写出去
* 这个管道流可以通过结合线程,读写都在管道里
*单线程也许会造成死锁
*/
public class PipedStreamDemo {
public static void main(String[] args) {
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();
try {
pis.connect(pos);
new Thread(new read(pis)).start();
new Thread(new write(pos)).start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
class read implements Runnable{
private PipedInputStream pis = null;
public read(PipedInputStream pis){
this.pis = pis;
}
@Override
public void run() {
try {
byte[] buff = new byte[1024];
System.out.println("没有数据可读,阻塞中。。。");
int len = pis.read(buff);
String s = new String(buff,0,len);
System.out.println("读到数据了,阻塞结束");
pis.close();
System.out.println(s);
} catch (Exception e) {
e.printStackTrace();
}
}
}
class write implements Runnable{
private PipedOutputStream pos = null;
public write(PipedOutputStream pos){
this.pos = pos;
}
@Override
public void run() {
try {//键盘输出到管道里?
System.out.println("5秒后写入数据。。。");
Thread.sleep(5000);
pos.write("hello,pipedStream....".getBytes());
System.out.println("数据已写入。。。");
pos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
ObjectStream对象的序列化
package com.heima.IO;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
* 把存储在堆内存中的对象持久化到硬盘
*对象的序列化
*
*/
public class ObjectStreamDemo {
public static void main(String[] args) {
try {
//writeObj();
readObj();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void readObj() {
ObjectInputStream ois = null;
List<Person> list = new ArrayList<Person>();
try {
ois = new ObjectInputStream(new FileInputStream("obj.object"));
while(true){
Person person = (Person) ois.readObject();
list.add(person);
}
} catch (Exception e) {
e.printStackTrace();
}
for(Person p :list ){
System.out.println(p.toString());
}
}
public static void writeObj() throws Exception{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.object"));
oos.writeObject(new Person("yxb", 20,"kr"));
oos.writeObject(new Person("杨显彬", 21,"cn"));
}
}
//没有实现方法的接口叫做标记接口
//每个类有一个唯一的UID,Serializable赋给被序列化类一个UID
//类改了,UID变了,持久化的对象就不能识别了
//UID是根据字段方法生成的
class Person implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
transient int age;//transient:短暂的,修饰的age不会被序列化
//只能序列化堆里面的东西,static方法属于方法区,不能被序列化
static String country = "cn";
public Person(String name,int age,String country){
this.name = name;
this.age = age;
this.country = country;
}
public String toString(){
return name+","+age+","+country;
}
}
编码(字符到字节)和解码(字节到字符)
package com.heima.IO;
import java.util.Arrays;
/**
* 编码:String -----str.getBytes------byte[]
* 解码:byte[] ------new String(byte[], charset)------String
*GBK:2字节
*UTF-8:3字节【头,标志】
*0读取一个字节后不再读取
*110两个字节
*1110三个字节
*/
public class Demo {
public static void main(String[] args) {
String s = "你好";
try {
byte[] b1 = s.getBytes("GBK");//编码
String str = new String(b1, "iso8859-1");//错误的解码
//因为UTF-8能识别中文,所以编错码了就不能再编码和解码回来
byte[] b2 = str.getBytes("iso8859-1");//再次编码
String str2 = new String(b2, "GBK");//解码
System.out.println(str2);
System.out.println(Arrays.toString(b2));
} catch (Exception e) {
e.printStackTrace();
}
}
}
------- android培训 、 java培训 、期待与您交流! ----------