流
字节流抽象类
文件字节流
FileInputStream的使用
/**
* 演示FileInputStream的使用
* 文件字节输入流
* @author wjx
*/
public class Demo01 {
public static void main(String[] args) throws Exception{
//1.创建FileInputStream,并指定文件路径
FileInputStream fis = new FileInputStream("C:\\Users\\asus\\Desktop\\IO\\aaa.txt");
//2.读取文件read()
//2.1单个字节读取 效率不高
// int data = 0;
// while((data = fis.read())!= -1){
// System.out.print((char)data);
// }//abcdefg
// System.out.println();
//2.2一次读取多个字节
//2.2.1麻烦
// byte[] buf = new byte[3];
// int count = fis.read(buf);
// System.out.println(new String(buf));//abc
// System.out.println(count);//3
// int count2 = fis.read(buf);
// System.out.println(new String(buf));//def
// System.out.println(count2);//3
// int count3 = fis.read(buf);
// System.out.println(new String(buf,0,count3));//g
// System.out.println(count3);//1
//2.2.2换种方式
// byte[] buf = new byte[3];
// int count = 0;
// while((count=fis.read(buf))!=-1){
// System.out.println(new String(buf,0,count));
// }//abc def g
//2.2.3
byte[] buf = new byte[1024];//1kb
int count = 0;
while((count=fis.read(buf))!=-1){
System.out.println(new String(buf,0,count));
}//abcdefg
//3.关闭
fis.close();
System.out.println();
System.out.println("执行完毕");
}
}
FileOutputStream的使用
public class Demo02 {
public static void main(String[] args) throws Exception{
//1.创建文件字节输出流对象
FileOutputStream fos = new FileOutputStream("C:\\Users\\asus\\Desktop\\IO\\bbb.txt",true);//接着往下写
//2.写入文件
// fos.write(97);
// fos.write('b');
// fos.write('c');
String string = "helloworld";
fos.write(string.getBytes());
//3.关闭
fos.close();
System.out.println("执行完毕");
}
}
案例:字节流复制文件
public class Demo03 {
public static void main(String[] args) throws Exception {
//1.创建流
//1.1文件字节输入流
FileInputStream fis = new FileInputStream("C:\\Users\\asus\\Desktop\\IO\\001.jpg");
//1.2文件字节输出流
FileOutputStream fos = new FileOutputStream("C:\\Users\\asus\\Desktop\\IO\\002.jpg");
//2.一边读,一边写
byte[] buf = new byte[1024];//1kb
int count = 0;
while((count = fis.read(buf)) != -1){
fos.write(buf,0,count);//最后一次可能读的不是1024byte
}
//3.关闭
fis.close();
fos.close();
System.out.println("复制完毕");
}
}
字节缓冲流
BufferedInputStream:
/**
* 使用字节缓冲流读取
* BufferedInputStream
* @author wjx
*/
public class Demo04 {
public static void main(String[] args) throws Exception {
//1.创建BufferedInputStream
FileInputStream fis = new FileInputStream("C:\\Users\\asus\\Desktop\\IO\\aaa.txt");
BufferedInputStream bis = new BufferedInputStream(fis);//8kb
//2.读取
//2.1 BufferedInputStream的缓存区 DEFAULT_BUFFER_SIZE = 8192 8kb
// int data= 0;
// while((data=bis.read())!=-1){
// System.out.print((char)data);
// }
//2.2 自己创建的缓存区
System.out.println("----------------");
int count = 0;
byte [] buf = new byte[1024];
while((count=fis.read(buf))!=-1){
System.out.println(new String(buf,0,count));
System.out.println(count);
}
//3.关闭
bis.close();
}
}
BufferedOutputStream:
/**
* 只用字节缓冲流写入文件
* BufferedOutputStream
* @author wjx
*/
public class Demo05 {
public static void main(String[] args) throws Exception{
//1.创建字节输出缓冲流
FileOutputStream fos = new FileOutputStream("C:\\Users\\asus\\Desktop\\IO\\buffer.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
//2.写入文件
for(int i = 0;i<10;i++){
bos.write("helloworld\r".getBytes());//先写入8kb缓冲区
bos.flush();//刷新到硬盘
}
//3.关闭(内部调用flush方法)
bos.close();
fos.close();
}
}
对象流
序列化
/**
* 使用ObjectOutputStream实现对象的序列化(写入操作)
* 注意事项:
* (1)序列化类必须要实现Serializable接口
* (2)序列化类中对象属性要求实现Serializable接口
* @author wjx
*/
public class Demo06 {
public static void main(String[] args) throws Exception{
//1.创建对象流
FileOutputStream fos = new FileOutputStream("C:\\Users\\asus\\Desktop\\IO\\stu.bin");
ObjectOutputStream oos = new ObjectOutputStream(fos);
//2.序列化(写入操作)
Student s1 = new Student("张三",20);//对象必须实现Serializable接口标记一下表示可被序列化,不然无法写入会异常
oos.writeObject(s1);
oos.flush();
//3.关闭
oos.close();
System.out.println("序列化完毕");
}
}
注意事项:
(1)序列化类必须要实现Serializable接口标记一下,表示可被序列化。
(2)序列化类中对象属性要求实现Serializable接口。
public class Student implements Serializable {
private String name;
private int age;
public Student(String name,int age) {
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
序列化和反序列化注意事项:
(1)序列化类必须要实现Serializable接口
(2)序列化类中对象属性要求实现Serializable接口
(3)序列化版本号ID,保证序列化的类和反序列化的类是同一个类,对于程序来讲,序列化的类和反序列化的类不是同一个类。
(4)使用transient(瞬间的)修饰属性,这个属性不能序列化
(5)静态属性不能序列化
(6)序列化多个对象
序列化:
/**
* 使用ObjectOutputStream实现对象的序列化(写入操作)
* 注意事项:
* (1)序列化类必须要实现Serializable接口
* (2)序列化类中对象属性要求实现Serializable接口
* (3)序列化版本号ID,保证序列化的类和反序列化的类是同一个类,对于程序来讲,序列化的类和反序列化的类不是同一个类。
* (4)使用transient(瞬间的)修饰属性,这个属性不能序列化
* (5)静态属性不能序列化
* (6)序列化多个对象
* @author wjx
*/
public class Demo06 {
public static void main(String[] args) throws Exception{
//1.创建对象流
FileOutputStream fos = new FileOutputStream("C:\\Users\\asus\\Desktop\\IO\\stu.bin");
ObjectOutputStream oos = new ObjectOutputStream(fos);
//2.序列化(写入操作)
Student s1 = new Student("张三",20);//对象必须实现Serializable接口标记一下表示可被序列化,不然无法写入会异常
Student s2 = new Student("李四",22);
// oos.writeObject(s1);
// oos.writeObject(s2);
// oos.flush();
//序列化多个对象
ArrayList<Student> list = new ArrayList<>();
list.add(s1);
list.add(s2);
oos.writeObject(list);
//3.关闭
oos.close();
System.out.println("序列化完毕");
}
}
反序列化:
/**
* 使用ObjectInputStream实现反序列化(读取重构成的对象)
* @author wjx
*/
public class Demo07 {
public static void main(String[] args) throws Exception {
//1.创建对象流
FileInputStream fis = new FileInputStream("C:\\Users\\asus\\Desktop\\IO\\stu.bin");
ObjectInputStream ois = new ObjectInputStream(fis);
//2.反序列化(读取操作)
// Student s1 = (Student)ois.readObject();
// Student s2 = (Student)ois.readObject();
// System.out.println(s1.toString());//transient修饰前Student{name='张三', age=20} transient修饰后Student{name='张三', age=0}
// System.out.println(s2.toString());//Student{name='李四', age=0}
ArrayList <Student> list = (ArrayList<Student>) ois.readObject();
System.out.println(list.toString());//[Student{name='张三', age=0}, Student{name='李四', age=0}]
//3.关闭
ois.close();
fis.close();
System.out.println("反序列化完毕");
}
}
对象:
public class Student implements Serializable {
//serialVersionUID:序列化版本号ID
@java.io.Serial
private static final long serialVersionUID = 100L;
private String name;
//使用transient(瞬间的)修饰属性,这个属性不能序列化
private transient int age;
//静态属性不能被序列化
public static String country = "中国";
public Student(String name,int age) {
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
字符流
字符流抽象类
文件字符流
FileReader的使用
public class Demo02 {
public static void main(String[] args) throws Exception {
//1.创建FileReader 文件字符输入流
FileReader fr = new FileReader("C:\\Users\\asus\\Desktop\\IO\\hello.txt");
//2.读取
//2.1单个字符读取
// int data = 0;
// while((data=fr.read())!=-1){//读取一个字符
// System.out.print((char)data);
// }//好好学习helloworld
//2.2
int count = 0;
char[] buf = new char[1024];
while((count=fr.read(buf))!=-1){
System.out.println(new String(buf,0,count));
}//好好学习helloworld
//3.结束
fr.close();
}
}
FileWriter的使用
/**
* 使用FileWriter写入文件
* @author wjx
*/
public class Demo03 {
public static void main(String[] args) throws Exception {
//1.创建FileWriter对象
FileWriter fw = new FileWriter("C:\\Users\\asus\\Desktop\\IO\\hello1.txt");
//2.写入
for (int i = 0; i < 10; i++) {
fw.write("java是世界上最好的语言");
fw.flush();
}
//3.关闭
fw.close();
System.out.println("写入完毕");
}
}
字符流复制文件
/**
* 使用FileReader和FileWriter复制文本文件,不能复制图片或二进制文件
*/
public class Demo04 {
public static void main(String[] args) throws Exception {
FileReader fr = new FileReader("C:\\Users\\asus\\Desktop\\IO\\hello.txt");
FileWriter fw = new FileWriter("C:\\Users\\asus\\Desktop\\IO\\hello2.txt");
//2.1第一种方式
int data = 0;
while((data=fr.read())!=-1){
fw.write(data);
fw.flush();
}
fw.close();
fr.close();
System.out.println("复制完毕");
}
}
字符缓冲流
BufferedReader的使用
public class Demo05 {
public static void main(String[] args) throws Exception {
//1.创建缓冲流
FileReader fr = new FileReader("C:\\Users\\asus\\Desktop\\IO\\hello1.txt");
BufferedReader br = new BufferedReader(fr);
//2.读取
//2.1第一种方式
// char [] buf = new char[1024];
// int count=0;
// while((count=br.read(buf))!=-1){
// System.out.print(new String(buf,0,count));
// }
//2.2第二种方式,一行一行的读取
String line = null;
while((line = br.readLine())!=null){
System.out.println(line);
}
br.close();
fr.close();
}
}
BufferedWriter的使用
public class Demo06 {
public static void main(String[] args) throws Exception {
//1.创建BufferedWriter对象
FileWriter fw = new FileWriter("C:\\Users\\asus\\Desktop\\IO\\hello3.txt");
BufferedWriter bw = new BufferedWriter(fw);
//2.写入
for (int i = 0; i < 10; i++) {
bw.write("好好学习,天天向上");
bw.newLine();//写入一个换行符\r\n
bw.flush();
}
bw.close();
fw.close();
System.out.println("写入完毕");
}
}
打印流
/**
* 演示PrintWriter的使用
*
*/
public class Demo07 {
public static void main(String[] args) throws Exception {
//1.创建打印流
PrintWriter pw = new PrintWriter("C:\\Users\\asus\\Desktop\\IO\\print.txt");
//2.打印
pw.println(97);
pw.println(true);
pw.println(3.14);
pw.println('a');
//3.关闭
pw.close();
System.out.println("执行完毕");
}
}
转换流
InputStreamReader
/**
* 使用InputStreamReader读取文件,指定使用的编码
* @author wjx
*/
public class Demo01 {
public static void main(String[] args) throws Exception {
//1.创建InputStreamReader对象
FileInputStream fis = new FileInputStream("C:\\Users\\asus\\Desktop\\IO\\write.txt");
InputStreamReader isr = new InputStreamReader(fis, "utf-8");//要保证文件编码和指定编码要一致,不然会乱码
//2.读取文件
int data = 0;
while((data=isr.read())!=-1){
System.out.println(data);
System.out.println((char)data);
}
//3.关闭
isr.close();
}
}
OutputStreamWriter
public class Demo02 {
public static void main(String[] args) throws Exception {
//1.创建OutputS
FileOutputStream fos = new FileOutputStream("C:\\Users\\asus\\Desktop\\IO\\write2.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
//2.写入
int data = 0;
for (int i = 0; i < 10 ; i++) {
osw.write("我爱北京,我爱故乡\r\n");
}
//3.关闭
osw.close();
System.out.println("执行成功");
}
}
File类
文件操作
/**
* File类的使用
* (1)分隔符
* (2)文件操作
* (3)文件夹操作
* @author wjx
*/
public class Demo01 {
public static void main(String[] args) throws Exception {
separator();
fileOpe();
directoryOpe();
}
//(1)分隔符
public static void separator(){
System.out.println("路径分隔符" + File.pathSeparator);
System.out.println("名称分隔符" + File.separator);
}
//(2)文件操作
public static void fileOpe() throws IOException, InterruptedException {
//1.创建文件
File file = new File("C:\\Users\\asus\\Desktop\\IO\\file.txt");//这里只是创建一个文件对象,并不是创建一个文件
// System.out.println(file.toString());C:\Users\asus\Desktop\IO\file.txt
if(!file.exists()){//判断一下该文件是否已经存在,存在的话就不用再创建了
boolean b = file.createNewFile();//创建一个文件
System.out.println("创建结果:" + b);//创建结果:true
}
//2.删除文件
//2.1直接删除
// System.out.println("删除结果:" + file.delete());//删除结果:true
//2.2使用jvm退出时删除(当程序运行结束,JVM终止时才真正调用deleteOnExit()方法实现删除操作)
// file.deleteOnExit();
// Thread.sleep(5000);
//3.获取文件信息
System.out.println("获取文件的绝对路径:" + file.getAbsolutePath());//获取文件的绝对路径:C:\Users\asus\Desktop\IO\file.txt
System.out.println("获取路径:" + file.getPath());//获取路径:C:\Users\asus\Desktop\IO\file.txt
System.out.println("获取文件名称:" + file.getName());//获取文件名称:file.txt
System.out.println("获取父目录:" + file.getParent());//获取父目录:C:\Users\asus\Desktop\IO
System.out.println("获取文件长度:" + file.length());//获取文件长度:21
System.out.println("文件创建时间:" + new Date(file.lastModified()).toLocaleString());//文件创建时间:2021-12-11 15:38:43 lastModified()返回此抽象路径名表示的文件上次修改的时间。
//4.判断
System.out.println("是否可写:" + file.canWrite());//是否可写:true 如果把文件设置成只读那就不能写了false
System.out.println("是否是文件:" + file.isFile());//是否是文件:true 有可能是文件夹有可能是文件
System.out.println("是否隐藏:" + file.isHidden());//是否隐藏:false
}
//(3)文件夹操作
public static void directoryOpe() throws IOException, InterruptedException {
//1.创建文件夹
File dir = new File("C:\\Users\\asus\\Desktop\\IO\\aaa\\bbb\\ccc");
System.out.println(dir.toString());
if(!dir.exists()){
//dir.mkdir();只能创建单级目录
boolean t = dir.mkdirs();//创建多级目录
System.out.println("创建结果:" + t);
}
//2.删除文件夹
//2.1直接删除
// System.out.println("删除结果:"+dir.delete());//并不是把全部目录删除,而是只删除最里面的目录,而且这个目录必须是个空目录
//2.2使用jvm删除
dir.deleteOnExit();
Thread.sleep(5000);
//3.获取文件夹信息
System.out.println("获取文件的绝对路径:" + dir.getAbsolutePath());//获取文件的绝对路径:C:\Users\asus\Desktop\IO\aaa\bbb\ccc
System.out.println("获取路径:" + dir.getPath());//获取路径:C:\Users\asus\Desktop\IO\aaa\bbb\ccc
System.out.println("获取文件名称:" + dir.getName());//获取文件名称:ccc
System.out.println("获取父目录:" + dir.getParent());//获取父目录:C:\Users\asus\Desktop\IO\aaa\bbb
System.out.println("获取文件长度:" + dir.length());//获取文件长度:0
System.out.println("文件创建时间:" + new Date(dir.lastModified()).toLocaleString());//文件创建时间:2021-12-11 15:38:51 lastModified()返回此抽象路径名表示的文件上次修改的时间。
//4.判断
System.out.println("是否可写:" + dir.canWrite());//是否可写:true 如果把文件设置成只读那就不能写了false
System.out.println("是否是文件:" + dir.isFile());//是否是文件:false 有可能是文件夹有可能是文件
System.out.println("是否隐藏:" + dir.isHidden());//是否隐藏:false
//5.遍历文件夹
File dir2 = new File("C:\\Users\\asus\\Desktop\\IO");
String[] files = dir2.list();
for(String string : files){
System.out.println(string);
}//001.jpg aaa aaa.txt bbb.txt buffer.txt file.txt hello.txt ....
}
}
文件夹删除并不是把全部目录删除,而是只删除最里面的目录,而且这个目录必须是个空目录
案例:递归遍历和递归删除文件夹
/**
* 案例1:递归遍历文件夹
* 案例2:递归删除文件夹
* @author wjx
*/
public class ListDemo {
public static void main(String[] args) {
listDir(new File("C:\\Users\\asus\\Desktop\\IO"));
deletDir(new File("C:\\Users\\asus\\Desktop\\IO - 副本"));
}
//案例1:递归遍历文件夹
public 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());
}
}
}
}
//案例2:递归删除文件夹 先删文件再删文件夹
public static void deletDir(File dir){
File[] files = dir.listFiles();
if(files!=null&&files.length>0){
for(File file : files){
if(file.isDirectory()){
deletDir(file);//递归
}else{
//删除文件
System.out.println(file.getAbsolutePath() + "删除:" +file.delete());
}
}
}
System.out.println(dir.getAbsolutePath() + "删除:" +dir.delete());
}
}
IO总结