字节流的操作(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();
}
}
- 字节流的操作(3+1)
- IO流--字节流的读写操作
- File 类的字节流操作
- 用于操作字节数组的流对象。
- 字节的位操作
- Java字节流学习记录(3)--字节流InputStream及其字节输入流的子类
- 文件输入输出操作(字节流)
- 字节数组操作流
- Java字节流操作
- Java 字节流操作
- 字节流操作媒体文件
- Java字节流操作
- 对字节的位操作
- 基于字节的IO操作
- 返回字节的写操作
- Java常见的字节操作
- JAVA 的IO操作(四)字节流和字符流的区别
- java i/o流(5) 字节流、字符流的操作
- Shape Number HDU
- 9-4(线段树最大值定位,区间合并)
- 怎样用mysql查询语句得到下方表格
- 哈夫曼树
- 输出一个矩形(长方形)
- 字节流的操作(3+1)
- JDK提供复制文件三种方式
- 技术人生(20) ——ORA-01555错误启示录
- 序列化与反序列化
- Pointers on C——6 Pointers.15
- Pointers on C——7 Functions.1
- SQL Challenge 每日一题:2017-8-1 自定义嵌套表类型的MULTISET
- P2P网络概念的背景和基础认识
- Pointers on C——7 Functions.2