IO流按使用方式可以简单的分为4种:
1.字符字节流
2.缓冲字符字节流
3.转换流
4.数据流
字节流例子:
运行前
运行后
字符流例子:
运行前
运行后
缓冲流先将数据读入到缓冲区中,目的是提高程序读出的性能。缓冲流也分为字节缓冲流和字符缓冲流。
示例:
运行后:
转换流,操作字符文件时有可能因为编码不一样产生乱码。字节流向字符流转换:
运行后:
数据流:方便将程序中数据存入文件。
运行后:
贴上面的测试代码
public static void main(String[] args) {
/*
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
//字节流字节文件。视频,音频,图片,文档。
try {
//输入流
fileInputStream = new FileInputStream("E:\\Document\\temp\\甜点.jpg");
//输出流
fileOutputStream = new FileOutputStream("E:\\Document\\temp\\newFood.jpg");
//输入输出流小车,加速读写。
byte []car = new byte[1024];
int length = 0;
while((length=fileInputStream.read(car))!=-1) {
fileOutputStream.write(car,0,length);
}
fileOutputStream.flush();
System.out.println("完毕");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(fileInputStream!=null) {
try {
fileInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
*/
/*
//字符流:操作字符流文件(记事本)
FileReader fileReader = null;
FileWriter fileWriter = null;
try {
fileReader = new FileReader("E:\\Document\\temp\\test.txt");//输入流
fileWriter = new FileWriter("E:\\Document\\temp\\new.txt");//输出流
//小车
char []car = new char[1024];
int length = 0;
while((length=fileReader.read(car))!=-1) {
fileWriter.write(car,0,length);
}
fileWriter.flush();//清空缓存
System.out.println("完毕");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(fileReader!=null) {
try {
fileReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
*/
/*
//缓冲流
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream("E:\\Document\\temp\\甜点.jpg"));
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("E:\\Document\\temp\\不好吃.jpg"));
byte []car = new byte[1024];
int length = 0;
while((length=bufferedInputStream.read(car))!=-1) {
bufferedOutputStream.write(car,0,length);
}
bufferedOutputStream.flush();
System.out.println("完毕");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(bufferedInputStream!=null) {
try {
bufferedInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
*/
/*
//转换流,操作字符文件 时有可能因为编码不一样产生乱吗,字节流向字符流转换
InputStreamReader inputStreamReader = null;
OutputStreamWriter outputStreamWriter = null;
try {
inputStreamReader = new InputStreamReader(new FileInputStream("E:\\Document\\temp\\test.txt"), "UTF-8");
outputStreamWriter = new OutputStreamWriter(new FileOutputStream("E:\\Document\\temp\\真香.txt"), "UTF-8");
char []car = new char[1024];
int length = 0;
while((length=inputStreamReader.read(car))!=-1) {
outputStreamWriter.write(car,0,length);
}
outputStreamWriter.flush();
System.out.println("完毕");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(inputStreamReader!=null) {
try {
inputStreamReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
*/
double price = 7.8;
//数据流:方便将程序中数据存入文件。
try {
DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream("E:\\Document\\temp\\data.temp"));
dataOutputStream.writeDouble(price);//输出到硬盘
dataOutputStream.flush();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
DataInputStream dataInputStream = new DataInputStream(new FileInputStream("E:\\Document\\temp\\data.temp"));
double p = dataInputStream.readDouble();//读取刚才输出的数据
System.out.println(p);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}