字节流的操作(3+1)

来源:互联网 发布:java开发百度云播放器 编辑:程序博客网 时间:2024/06/16 04:04

流介绍:  低级流(节点流):必须有,真实读写数据,数据源明确
  高级流(处理流):不一定有,不能独立存在,简化数据的操作

<1>java.io.InputStream:输入流的抽象父类,用于读取
(1)int read();
(2)int read(byte[] d):返回时实际读取到的字节量
<2>java.io.OutputStream:输出流的抽象父类,用于写出
(1)void write(int d)
(2)void write(byte[] d)



字节流的相关操作(4):

1.FileOutputStream(默认覆盖,2参是true追加,没有自创)+FileInputStream

   PS:字节流其实就是一个字节一个字节的读写一句话

2.BufferedOutputStream(flush操作)+BufferedInputStream(略)

   PS:缓冲流其实就是多字节的读写一句话

3.ObjectOutputStream+ObjectOutputStream

   PS:对象流其实就是就是读写一个对象,字节和对象的转换。

同时当一个类需要被对象流读写,那么该类必须实现java.io.Serializable接口。

接口分类:

1.签名接口,编译器加方法(本次是这个)
2.规范行为的接口,要求重写实现方法(大部分是这个)

4.复制:

   PS:(File单字节复制(略)+File多字节复制+Buffered复制)

**********************************************************************************************

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

FileOutputStream fos=new FileOutputStream("demo.txt");
 //String-->byte[]
 //1.byte getBytes();系统指定的字符集
 //2.byte getBytes(String csn);给定的字符集转化为一组字节

fos.write("我爱北京天安门".getBytes("utf-8"));
System.out.println("写出完毕!");
fos.close();
}
}

public class A_FileInputStream {
public static void main(String[] args) throws Exception {
FileInputStream fis=new FileInputStream("demo.txt");
byte[] data=new byte[100];
int len=fis.read(data);
String str=new String(data,0,len,"utf-8").trim();
System.out.println(str);
}
}

public class B_BufferedOutoutStream {
public static void main(String[] args) throws Exception {
FileOutputStream fos=new FileOutputStream("demo.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos);
bos.write("呵呵".getBytes("utf-8"));
bos.flush();//强制一次写出
System.out.println("写出完毕!");
bos.close();
}
}

//用于测试对象流
public class Person implements Serializable {
private static final long serialVersionUID = 1L;//保护反序列化,兼容
private String name;
private int age;
private String gender;
private transient List<String> otherInfo;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public List<String> getOtherInfo() {
return otherInfo;
}
public void setOtherInfo(List<String> otherInfo) {
this.otherInfo = otherInfo;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", gender=" + gender + ", otherInfo=" + otherInfo + "]";
}
public Person(String name, int age, String gender, List<String> otherInfo) {
super();
this.name = name;
this.age = age;
this.gender = gender;
this.otherInfo = otherInfo;
}
}

public class C_ObjectOutputStream {
public static void main(String[] args) throws Exception {
List<String> otherInfo=new ArrayList<String>();
otherInfo.add("是演员");
otherInfo.add("爱好写大字");
Person p=new Person("苍老师", 20, "女", otherInfo);
FileOutputStream fos=new FileOutputStream("person.obj");
ObjectOutputStream oos=new ObjectOutputStream(fos);
 // 该方法将对象转化为字节,里面包含数据和结构信息
* 1.对象转化为字节时对象的序列化
* 2.字节存入硬盘时对象的持久化
oos.writeObject(p);
System.out.println("写出对象完毕!");
oos.close();
}
}

public class C_ObjectInputStream {
public static void main(String[] args) throws Exception {
FileInputStream fis=new FileInputStream("person.obj");
 //字节-->对象(反序列化)
 //ois读取的必须是oos将对象序列化得到的字节,不然没意义
ObjectInputStream ois=new ObjectInputStream(fis);
Person p=(Person)ois.readObject();
System.out.println(p);
ois.close();
}
}

public class D_CopyDemo1 {
public static void main(String[] args) throws Exception {
FileInputStream src=new FileInputStream("demo1.txt");
FileOutputStream desc=new FileOutputStream("demo2.txt");
byte[] buf=new byte[100];
int len=-1;
while((len=src.read(buf))!=-1){
desc.write(buf,0,len);
}
System.out.println("复制完毕!");
src.close();
desc.close();
}
}

// 缓冲字节输入输出流加快读写效率
//组合不同的高级流获得不同的叠加效果

//有点像单字节复制☺☺
public class D_CopyDemo2 {
public static void main(String[] args) throws Exception {
FileInputStream fis=new FileInputStream("demo.txt");
FileOutputStream fos=new FileOutputStream("demo3.txt");
BufferedInputStream bis=new BufferedInputStream(fis);
BufferedOutputStream bos=new BufferedOutputStream(fos);
int d=-1;
while((d=bis.read())!=-1){
bos.write(d);
}
System.out.println("复制完毕");
bis.close();
bos.close();
}

}


原创粉丝点击