FileInputStream字节流只能读ASCII码文件,文件中有中文不能用他读
IO流的分类(流怎么分类)1.流按照方向分
1,输入流
2,输出流
2.流按照传输单位
1,字节流(byte)
2,字符流(char)
3.java提供了四个基类:
1,字节输出流基类:OutputStream
2,字节输入流基类:InputStream
3,字符输出流基类:Writer
4,字符输入流基类:Reader
4.字节流中常用的有: 文件:FileOutputStream, FileInputStream
对象 :ObjectOutputStream,ObjectInputStream
二进制 :DataOutputStream,DataInputStream
5.字符流中常用的有: 文件:FileReader,FileWriter
一次读一行的缓冲流:BufferedReader,BufferWriter
序列化和反序列化对象从内存中写入磁盘的过程中称为对象的持久化,也称序列化
从磁盘中读取文件到内存还原为对象的过程称为反序列化
1,字节文件输入流
public class Demo08 {
public static void main(String[] args) {
String name="D:\io\hello";
try {
FileInputStream fs=new FileInputStream(name);
int c;
while ((c=fs.read())!=-1){
System.out.println(c);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2,字符输入流
public class Demo02 {
public static void main(String[] args) {
String name="D:\io\hello";
try {
Reader reader=new FileReader(name);
int d;
while ((d=reader.read())!=-1){
System.out.println(d);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
}
3,字符输出流
public class Demo03 {
static String[] arr = {"面向对象", "封装", "继承", "多态"
, "数组", "父类", "抽象类", "接口", "异常", "运行时异常"
, "编译时异常", "抽象方法", "成员方法", "构造方法","静态方法"
,"成员变量", "局部变量", "静态变量", "常量", "重写", "重载"
, "向上转型", "向下转型"};
static Random random = new Random();
public static void main(String[] args) {
//创建一个词频测试文件
//字符输出流
try {
String name = "D:\io\words";
Writer out = new FileWriter(name);
for (int i = 1; i <= 10000; i++) {
//每循环一次生成一个随机数,作为数组的索引
int index = random.nextInt(arr.length);
if( i % 10 == 0 ){
out.write(arr[index] +"n");
}else {
out.write(arr[index] +" ");
}
out.flush();//冲刷流中数据到磁盘
//writer输出流对象的指针
//out.write(arr[index]);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
4,对象输入流
public class Demo06 {
public static void main(String[] args) {
//对象输入流,从磁盘中读取文件反序列化还原对象
String name="D:\io\car";
try {
InputStream is=new FileInputStream(name);
ObjectInputStream ois=new ObjectInputStream(is);
//readObject()读取磁盘文件内容并反序列化为对象,返回Object类型
//需要手动向下转型,才能得到car类型
Car car=(Car)ois.readObject();
System.out.println(car.getBrand());
System.out.println(car.getModel());
System.out.println(car.getColor());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
5,对象输出流
public class Demo05 {
public static void main(String[] args) {
//对象输出流-对象持久化到磁盘文件
Car car=new Car();
car.setBrand("凯迪拉克");
car.setModel("CT6");
car.setColor("白色");
String name="D:\io\car";
try {
//创建底层的字节流对象
OutputStream os=new FileOutputStream(name);
//创建对象输出流,包装底层的字节流对象
ObjectOutputStream oos=new ObjectOutputStream(os);
oos.writeObject(car);
oos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
6,二进制文件输入,输出流
public class Demo07 {
public static void main(String[] args) {
//图片输入流
//输入流读的时候,文件必须存在,否则会抛异常
//输出流写的时候,文件可以不存在,会自动创建,但目录必须存在
String name="D:\io\a.jpg";//图片输入路径
String outName="D:\io\a_copy.jpg";//图片输出路径
try {
//创建底层的字节输入流
InputStream is=new FileInputStream(name);//字节输入流
//创建二进制流,包装底层的字节流
DataInputStream dis=new DataInputStream(is);//二进制输入流
//创建底层的字节输出流
OutputStream os=new FileOutputStream(outName);//字节输出流
DataOutputStream dos=new DataOutputStream(os);//二进制输出流
//建立字节缓冲区
byte[]data=new byte[1024];//内存缓冲区
int n;
while ((n= dis.read(data))!=-1){//循环读取图片中的二进制数据到缓冲区
dos.write(data,0,n);//循环把缓冲区中的字节写入到磁盘,考虑到最后一次可能读不到缓冲区的问题
}
dos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}



