文章目录
数据流
数据流就是数据传输的过程
根据流向分为:
输入流:从程序的外部流向程序
输入操作 read()
输出流:从程序内部流向程序外部
输出操作 write()
根据数据传输单位分为:
字节流:以字节为单位 (图片,视频)
字符流:以字符为单位 (文本)
形成了四中流 java.io有对流的实现
字节输入流:InputStream 所有字节输入流的超类
字节输出流:OutputStream 所有输出字节流的超类
字符输入流:Reader 读取字符的抽象类
字符输出流: Writer 写入字符的超类
按照不同的功能,又可以进行分类
1 文件流:程序与文件之间进行数据传输
FileInputStream 文件字节输入流
FileOutputStream 文件字节输出流
FileReade 文件输入流
FileWriter 文件输出流
2 网络流:程序与网络之间进行数据传输
文件流
FileInputStream(文件字节输入流)
将数据以字节的方式从文件读入到程序中
主要用来与read()方法
public class Demo04 {
public static void main(String[] args) {
FileInputStream fis = null;
try {
//创建一个文件字节输入流
fis = new FileInputStream("D:\\text.txt");
//从文件中读取数据
System.out.println(fis.available());
//只读取一个字节
while (true) {
int i = fis.read();
if (i==-1) { //i==-1说明走到了末尾
break;
}
System.out.println(i);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭流
try {
if (fis != null) fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileOutputStream (文件字节输出流)
将文件以字节的方式写入到文件中
public class Demo01 {
public static void main(String[] args) {
try {
//如果文件不存在,会自动创建
FileOutputStream fos = new FileOutputStream("D:\\test01.txt");
//追加的方式会在原有的最后写 "D:\\test01.txt", true
//不追加的方式会把原有的东西删除掉 从头开始写 "D:\\test01.txt"
//向文件中写入内容
fos.write(49);
//创建一个数组
byte [] bs = {51,52,53,54,55,56,57,58,59};
fos.write(bs); //写入整个数组
fos.write(bs,2,3);//从下标2的位置开始写 写入3个
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileReade (文件字符输入流)
用于读取字符的便捷类
public static void main(String[] args) {
FileReader fr = null;
try {
fr = new FileReader("D:\\text.txt");
//读入文件中的字符
//读取一个字符
int i = fr.read();
char c = (char) i;
System.out.println(c);
//读多个字符
while(true) {
char [] cbuf = new char[10];
int res = fr.read(cbuf);
if (res == -1) {
break;
}
for (int j=0; j<res; j++) {
System.out.println(cbuf[j]);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (fr!=null)
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
FileWriter (文件字符输出流)
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("D:\\text.txt");
//写字符串
fw.write("java");//先将这些数据写入缓冲区 通过flush写入到缓冲区
fw.write("c++");
fw.write("php");
fw.flush(); //将缓冲区的内容写入到文件
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (fw!=null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
包装流/装饰流/增强流
装饰者模式:java23设计模式中的一种
在不改变原来的流程的基础上,通过包装类对其功能增强
字符缓冲流 BufferedReader
public class Demo04 {
public static void main(String[] args) {
FileReader fr = null;
BufferedReader br =null;
try {
//先创建原始流文件字符输入流
fr = new FileReader("D:\\text.txt");
//包装起来
br = new BufferedReader(fr);//传达原始流的参数
while (true) { //while读取全部
//可以整行读
//BufferedReader读到末尾时,返回null;
String str = br.readLine();
if (str == null) {
break;
}
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
//先关闭包装流,再关别原始流
try {
if (br!=null)
br.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fr!=null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedWriter
public static void main(String[] args) {
FileWriter fw = null;
BufferedWriter bw =null;
try {
//原始流
fw= new FileWriter("D:\\text.txt");
//包装
bw = new BufferedWriter(fw); //使用原始流的参数
//先将数据存到缓冲区
bw.write("青岛工学院");
bw.newLine();//创建一个新的行 换行操作
bw.write("信息工程学院");
bw.newLine();//创建一个新的行 换行操作
bw.write("****2班");
bw.newLine();//创建一个新的行 换行操作
bw.flush(); //调用缓冲区的内容
} catch (IOException e) {
e.printStackTrace();
}finally {
//先关闭包装流,再关闭原始流
try {
//避免出现空指针
if (bw!=null)
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fw!=null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
对象流
直接把一个对象保存到流中
从流中读取一个对象
ObjectInputStream
public class Student implements Serializable {
private static final long serialVersionUID = -7280199762758808625L;
private String name;
private String sex;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", sex=" + sex + ", age=" + age + "]";
}
}
public static void main(String[] args) {
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream("D:\\student.obj");
ois = new ObjectInputStream(fis);
//从流中读取对象的操作,反序列化
Student student = (Student) ois.readObject();
System.out.println(student);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
try {
if(ois!=null)
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(fis!=null)
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
ObjectOutputStream
public class Student implements Serializable {
private static final long serialVersionUID = -7280199762758808625L;
private String name;
private String sex;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", sex=" + sex + ", age=" + age + "]";
}
}
public class Demo06 {
public static void main(String[] args) {
Student student = new Student();
student.setName("tom");
student.setAge("20");
student.setSex("男");
//保存到文件中 student.obj
//输出流
//文件字节输出流作为原始流
FileOutputStream fos = null;
ObjectOutputStream ous = null;
try {
fos = new FileOutputStream("D:\\student.obj");
ous = new ObjectOutputStream(fos);
//将对象写入流的过程叫做序列化
ous.writeObject(student);
//如果保存10个对象,将10个对象放在一个Arraylist中
//再把Arraylist放到文件中
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (ous!=null)
ous.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (fos!=null)
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}










