流
作用
传输数据
分类
按流向分类
输入流:外界给程序中输入
输出流:从程序给外界输入数据
按传输的最小数据单位:
字节流:传输的数据单位是字节
字符流:传输的数据单位是字符,只能传递文本文件
按功能分类:
节点流:从源头到程序
过滤流(包装流):加工
都有其特性
字节流
体系结构:
InputStream(抽象类)
方法:
read()
read(byte[] b);
read(byte[] b, int off, int len)
close()
OutputStream(抽象类)
方法:
write(byte b)
write(byte[] b)
write(byte[] b, int off, int len)
close()
flush():冲刷管道,防止残留
文件字节流
FileInputStream:从文件到程序
FileOutputStream:从程序到文件
练习:从一个文件夹赋值照片到另一个文件夹
import java. io. FileInputStream ;
import java. io. FileNotFoundException ;
import java. io. FileOutputStream ;
import java. io. IOException ;
public class Demo03 {
public static void main ( String [ ] args) {
FileInputStream fis = null ;
FileOutputStream fos = null ;
try {
fis = new FileInputStream ( "C://Users/豆三岁/Desktop/壁纸.jpg" ) ;
fos = new FileOutputStream ( "F://照片/壁纸.jpg" ) ;
byte [ ] b = new byte [ 1024 ] ;
int len = 0 ;
while ( ( len = fis. read ( b) ) != - 1 ) {
fos. write ( b, 0 , len) ;
fos. flush ( ) ;
}
} catch ( FileNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
if ( fis != null ) {
try {
fis. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
if ( fos != null ) {
try {
fos. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
}
}
内存字节流
ByteArrayInputStream:从内存到程序
ByteArrayOutputStream:从程序到内存
练习:将小说.txt文本文件,输出到控制台
import java. io. ByteArrayOutputStream ;
import java. io. FileInputStream ;
import java. io. FileNotFoundException ;
import java. io. IOException ;
public class Demo05 {
public static void main ( String [ ] args) {
FileInputStream fis = null ;
ByteArrayOutputStream bos = null ;
try {
fis = new FileInputStream ( "src/小说.txt" ) ;
bos = new ByteArrayOutputStream ( ) ;
byte [ ] b = new byte [ 1024 ] ;
int len = 0 ;
while ( ( len = fis. read ( b) ) != - 1 ) {
bos. write ( b, 0 , len) ;
bos. flush ( ) ;
}
byte [ ] data = bos. toByteArray ( ) ;
String str = new String ( data) ;
System . out. println ( str) ;
} catch ( FileNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
if ( bos != null ) {
try {
bos. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
if ( fis != null ) {
try {
fis. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
}
}
缓冲字节流(过滤流)
BufferedInputStream
BufferedOutputStream
作用:加快速度
练习:利用缓冲字节流复制文件
import java. io. BufferedInputStream ;
import java. io. BufferedOutputStream ;
import java. io. FileInputStream ;
import java. io. FileNotFoundException ;
import java. io. FileOutputStream ;
import java. io. IOException ;
public class Demo06 {
public static void main ( String [ ] args) {
BufferedInputStream bis = null ;
BufferedOutputStream bos = null ;
FileInputStream fis = null ;
FileOutputStream fos = null ;
try {
fis = new FileInputStream ( "src/小说.txt" ) ;
bis = new BufferedInputStream ( fis) ;
fos = new FileOutputStream ( "copy.txt" ) ;
bos = new BufferedOutputStream ( fos) ;
byte [ ] b = new byte [ 2048 ] ;
int len = 0 ;
while ( ( len = bis. read ( b) ) != - 1 ) {
bos. write ( b, 0 , len) ;
bos. flush ( ) ;
}
} catch ( FileNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
if ( bos != null ) {
try {
bos. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
if ( bis != null ) {
try {
bis. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
}
}
对象流(过滤流)
ObjectInputStream
readObject();
ObjectOutputStream
writeObject(Object obj);
作用:给指定存入对象
注意:
1、存储的对象要实现序列化接口
2、实现序列化接口的类中有static修饰的属性,该属性无法被序列化
3、transient修饰的属性也不能被序列化(transient修饰的属性称为临时属性)
练习:利用对象流存储数据
import java. io. Serializable ;
public class Person implements Serializable {
private String name;
private int age;
private transient String sex;
private static String gj;
public Person ( ) {
super ( ) ;
}
public Person ( String name, int age, String sex) {
super ( ) ;
this . name = name;
this . age = age;
this . sex = sex;
Person . gj = "中国" ;
}
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 getSex ( ) {
return sex;
}
public void setSex ( String sex) {
this . sex = sex;
}
public static String getGj ( ) {
return gj;
}
public static void setGj ( String gj) {
Person . gj = gj;
}
@Override
public String toString ( ) {
return "Person [name=" + name + ", age=" + age + ", sex=" + sex + ", gj=" + gj + "]" ;
}
}
import java. io. FileNotFoundException ;
import java. io. FileOutputStream ;
import java. io. IOException ;
import java. io. ObjectOutputStream ;
import java. util. ArrayList ;
public class Demo07 {
public static void main ( String [ ] args) {
FileOutputStream fos = null ;
ObjectOutputStream oos = null ;
try {
fos = new FileOutputStream ( "object.txt" ) ;
oos = new ObjectOutputStream ( fos) ;
Person person = new Person ( "高磊" , 16 , "男" ) ;
Person person02 = new Person ( "姬瑶葳" , 98 , "男" ) ;
ArrayList < Person > list = new ArrayList < Person > ( ) ;
list. add ( person) ;
list. add ( person02) ;
oos. writeObject ( list) ;
oos. flush ( ) ;
} catch ( FileNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} finally {
if ( oos != null ) {
try {
oos. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
System . out. println ( "OVER" ) ;
}
}
package com. qf. demo ;
import java. io. FileInputStream ;
import java. io. FileNotFoundException ;
import java. io. IOException ;
import java. io. ObjectInputStream ;
import java. io. ObjectOutputStream ;
import java. util. ArrayList ;
public class Demo08 {
public static void main ( String [ ] args) {
FileInputStream fis = null ;
ObjectInputStream ois = null ;
try {
fis = new FileInputStream ( "object.txt" ) ;
ois = new ObjectInputStream ( fis) ;
ArrayList < Person > persons = ( ArrayList < Person > ) ois. readObject ( ) ;
for ( Person person : persons) {
System . out. println ( person) ;
}
} catch ( FileNotFoundException e) {
e. printStackTrace ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
} catch ( ClassNotFoundException e) {
e. printStackTrace ( ) ;
} finally {
if ( ois != null ) {
try {
ois. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
}
}
}