一、概述
什么是流?
概念:内存与存储设备之间传输数据的通道。
流的分类:
按方向分为:输入流和输出流
- 输入流:将<存储设备>中的内容读入到内存中。
- 输出流:将<内存>中的内容写入到《存储设备》中
按照单位分为:字节流和字符流
- 字节流:以字节为单位,可以读写所有的数据。(视频、图片、文件)
- 字符流:以字符为单位,只能读写文本数据。
按照功能分为:节点流和过滤流
- 节点流:具有实际传输数据的读写功能。
- 过滤流:在节点的基础之上增强功能。
IO框架结构图:
字节流:
字符流:
- FileInputStream:
public int read(byte[] b):从流中读取多个字节,将读取到的内容存入b数组当中,返回实际读取到的字节数,如果读取到文件的结尾,则返回-1; - FileOutputStream:
public int write(byte[] b):一次写多个字节,将b数组中所有字节,写入输入流;
FileInputStream与FlieOutputStream的使用:
/**
* 练习: e盘的a.txt拷贝到d盘
* 分析: 先读后写
*/
public class TestCopyChar {
public static void main(String[] args)throws IOException{
//读取E盘的a.txt文件
FileInputStream fileInputStream=new FileInputStream("E:\\io\\a.txt");
//将E盘a.txt文件写入D盘
FileOutputStream fileOutputStream=new FileOutputStream("D:\\a.txt");
int len=0;
byte[] bytes=new byte[1024];
while ((len=fileInputStream.read(bytes))!=-1){
fileOutputStream.write(bytes,0,len);
}
IOUtils.closeAll(fileInputStream,fileOutputStream);
}
}
字节缓冲流:
- 缓冲流:BufferedInputStream 、BufferedOutputStream
*提高IO的读写效率,减少访问磁盘的次数。
*数据存储在缓冲区,flush是将缓存区的内容写入文件中。在调用close方法时也会自动 调用flush方法。
/**
* BufferedInputStream字符缓冲流读取文件
*/
public class TestBufferedInputStream {
public static void main(String[] args) throws IOException {
FileInputStream fileInputStream = new FileInputStream("E:\\io\\b.txt");
BufferedInputStream bufferedInputStream=new BufferedInputStream(fileInputStream);
byte[] bytes=new byte[1024];
int len=0;
while ((len=bufferedInputStream.read(bytes))!=-1){
bufferedInputStream.read();
System.out.println((char)len);
}
IOUtils.closeAll(fileInputStream,bufferedInputStream);
}
对象流:
-
对象流:ObjectOutputStream、ObjectInputStream
1)增强了缓冲区的功能
2)增强了读写8中基本数据类型和字符串的功能
3)增强了读写对象的功能:readObject()从中读取一个对象、writeObject(Oject obj)向流中写入一个对象 -
将对象写入到内存的过程称为序列化,将对象读取到磁盘的过程称为反序列化;
序列化:
/**
* 对象流ObjectInputStream的使用
* 要求:若需实现序列化和序列化,则必须实现Serializable接口
*/
public class Student implements Serializable {
private String name;
public Student() {
}
/**
* 对象流ObjectOutputStream的使用:序列化
* 注意事项:
* (1)序列化类必须要实现Serializeble
* (2)序列化类中对象属性要求实现Serializable
* (3)序列化版本ID,保证序列化的类和反序列化的类是同一个类
* (4)使用transinent(瞬间的)修饰属性,这个属性不能序列化
* (5)静态属性不能序列化
*/
public class TestObjectInputStream {
public static void main(String[] args) throws Exception {
//对象流写出-----反序列化操作
ObjectInputStream objectInputStream=new ObjectInputStream(new FileInputStream("a.txt"));
//取出自定义对象
Student student = (Student) objectInputStream.readObject();
try {
while (student!=null){
System.out.println(student);
student = (Student) objectInputStream.readObject();
}
}catch (Exception e){
IOUtils.closeAll(objectInputStream);
}
System.out.println("--------读写成功--------");
}
}
反序列化:
对象流ObjectInputStream的使用:反序列化
public class TestObjectInputStream {
public static void main(String[] args) throws Exception {
//对象流写出-----反序列化操作
ObjectInputStream objectInputStream=new ObjectInputStream(new FileInputStream("a.txt"));
//取出自定义对象
Student student = (Student) objectInputStream.readObject();
try {
while (student!=null){
System.out.println(student);
student = (Student) objectInputStream.readObject();
}
}catch (Exception e){
IOUtils.closeAll(objectInputStream);
}
System.out.println("--------读写成功--------");
}
}
字符流
文件字符流
字符串缓冲流
- 缓冲流:BufferedReader /BufferedWriter
*高效读写
*支持输入换行符
*可一次写一行、读一行
字符输入缓冲流:
/**
* BufferedReader的使用
*/
public class TestBufferdFlieReader {
public static void main(String[] args) throws IOException {
//创建文件字符流对象
FileReader reader=new FileReader("a.txt");
//创建缓冲流对象
BufferedReader br=new BufferedReader(reader);
//创建缓冲区
char[] buf=new char[1024];
int len=0;
//单个字符读取
// while ((len=br.read(buf))!=-1){
// System.out.println(new String(buf,0,len));
// }
//换行读取 一次读取一行
String line=null;
while ((line=br.readLine())!=null){
System.out.println(line);
}
//关闭资源
IOUtils.closeAll(reader,br);
}
}
字符输出缓冲流:
/**
* BufferedWriter的使用
*/
public class TestBufferedWriter {
public static void main(String[] args) throws IOException {
FileWriter fw=new FileWriter("a.txt");
BufferedWriter bw=new BufferedWriter(fw);
for (int i = 0; i <10 ; i++) {
bw.write("好好敲代码!");
bw.newLine(); //写入换行符
bw.flush();
}
fw.close();
bw.close();
System.out.println("执行完成!");
}
}
转换流
桥转换流: InputStreamReader / OutputStreamWriter
(1) 可将字节流转换为字符流;
(2)可设置字符的编码格式;
//字符转换流OutputStream、InputStream,读写编码格式需保持一致,否则乱码
@Test
public void outputstreamTest()throws IOException{
OutputStreamWriter outputStreamWriter=new OutputStreamWriter(new FileOutputStream("a.txt"),"utf-8");
outputStreamWriter.write("java,字符转换流");
outputStreamWriter.close();
}
@Test
public void inputstreamTest()throws IOException {
InputStreamReader inputStreamReader=new InputStreamReader(new FileInputStream("a.txt"));
char[] buf = new char[1024];
int len = inputStreamReader.read(buf);
System.out.println(new String(buf,0,len));
inputStreamReader.close();
}
}
File类
常用方法:
递归操作文件夹:
/**
* File类:操作文件或目录属性的类,与文件中的读写无关
* 递归遍历和删除文件夹
*/
public class TestFile {
public static void main(String[] args) {
File file = new File("E:\\io\\a\\b.txt");
//获取父路径
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
if (parentFile.mkdirs()) {
System.out.println("创建成功!");
}
}
//递归遍历文件夹
listDir(new File("d:\\myfiles"));
//递归删除文件夹
deleteDir(new File("d:\\myfiles"));
}
private static void listDir(File dir) {
File[] files = dir.listFiles();
if (files != null && files.length > 0) {
for (File file : files) {
if (file.isDirectory()) {
listDir(file); //递归
} else {
System.out.println(file.getAbsolutePath());
}
}
}
}
private static void deleteDir(File dir) {
File[] files = dir.listFiles();
if (files != null && files.length > 0) {
for (File file : files) {
if (file.isDirectory()) {
listDir(file); //递归
} else {
System.out.println(file.getAbsolutePath()+"删除"+file.delete());
}
}
}
System.out.println(dir.getAbsolutePath()+"删除"+dir.delete());
}
}
properties(属性集合)
- 特点:
1、储存属性名和属性值
2、属性名和属性值都是字符串类型
3、没有泛型
4、和流有关、且线程安全
propreties的应用:
/**
* Properties集合可以与文件进行交互
*/
public class TestProperties {
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
// properties.setProperty("苹果8", "3599");
// properties.setProperty("华为p30", "4299");
// properties.setProperty("小米9", "2599");
//
// //遍历
// Set<String> set = properties.stringPropertyNames();
// for (String key : set) {
// System.out.println(key + "价格为:" + properties.getProperty(key));
// }
Properties properties1=new Properties();
//将键值存储到文件中
properties.store(new FileWriter("a.txt"),"note: ");
//从文件中加载内容
properties1.load(new FileReader("a.txt"));
System.out.println();
System.out.println(properties1.getProperty("苹果"));
System.out.println(properties1.toString());
}
}