JavaIO流

1.File

(1)java.io.File类
(2)File 能新建、删除、重命名⽂件和⽬录,但File 不能访问⽂件内容本身,如果需要访问⽂件 内容本身,则需要使⽤输⼊/输出流。
(3)想要在Java程序中表示⼀个真实存在的⽂件或⽬录,那么必须有⼀个File对象
(4)绝对路径和相对路径
①路径分隔符:windows:\ unix:/
②相对路径:相较于某个路径下,指明的路径
③绝对路径:包含盘符在内的文件或文件目录的路径
(5)创建File类
①File(String filePath):以filePath为路径创建File对象,可以是绝对路径或者相 对路径
②File(String parentPath,String childPath):以parentPath为⽗路径,childPath为⼦ 路径创建File对象
③File(File parentFile,String childPath):根据⼀个⽗File对象和⼦⽂件路径创建 File对象
(6)File的常用方法
getAbsolutePath( )	获取绝对路径

getPath( )	获取路径

getName( )	获取名称

getParent( )	获取上层文件目录路径。若无,返回null

length( )	获取文件长度(字节数)。不能获取目录的长度

String [ ] list( )	获取指定目录下的所有文件或者文件目录的名称数组

listFiles( )	获取指定目录下的所有文件或者文件目录的File数组

isFile( )	判断是否是文件

isDiretory( )	判断是否是目录

exists( )	判断是否存在

canRead( )	判断是否可读

canWrite( )	判断是否可写

isHidden( )	判断是否隐藏

createNewFile( )	创建文件,若文件存在,则不创建,否则返回false

mkdir( )	创建文件目录,如果此文件目录存在,就不创建,如果才文件目录的上层目录不存在,也不创建

mkdirs( )	创建文件目录,如果此文件目录存在,就不创建如果上层文件目录不存在,一并创建

delete( )	删除文件或文件夹(Java中的删除不走回收站)

2.IO流(Input/Output)

(1)Java中I/O操作主要是指使⽤Java进⾏输⼊,输出操作,Java所有的I/O机制都 是基于数据流进⾏输⼊输出
(2)I/O技术是非常实用的技术,用于处理设备之间的数据传输。如读/写文件,网络 通讯等。
(3)输入/输出流

文件——>程序 网络——>程序
文件<——程序 网络<——程序

(4)流的分类
①按操作数据单位不同分为:字节流(8bit)、字符流(16bit)
②按数据流的流向不同分为:输入流、输出流——抽象基类
③按流的角色不同分为:节点流、处理流
(5)对于⽂本⽂件(.txt,.java,.c,.cpp),使⽤字符流处理
(6)对于⾮⽂本⽂件(.jpg,.mp3,.mp4,.avi,.doc,.ppt,…),使⽤字节流处理
(7)IO流

在这里插入图片描述

3.字符流

(1)FileReader——字符输入流
①读入数据的基本操作
1)建⽴⼀个流对象,将已存在的⼀个⽂件加载进流。
2)创建⼀个临时存放数据的数组。
3)调⽤流对象的读取⽅法将流中的数据读⼊到数组中
4)关闭资源
②第一种方式:一个一个读取
int read = fileReader.read();
while (read!=-1){
	System.out.println((char) read);
	read= fileReader.read();
}
③第二种方式:
int data;
while ((data=fileReader.read())!=-1){
	System.out.println((char) data);
}
④第三种方式:
char [] chars =new char[3];
int length;
while ((length=fileReader.read(chars))!=-1){
	System.out.println(chars);
	String s = new String(chars, 0, length);
	System.out.println(s);
}
⑤read( )方法:返回读入的第一个字符,如果达到文件末尾,返回-1
(2)FileWriter——字符输出流
①创建流对象,建⽴数据存放⽂件
②调⽤流对象的写⼊⽅法,将数据写⼊流
③关闭流资源,并将流中的数据清空到⽂件中
④File对应的硬盘中的⽂件如果不存在,在输出的过程中,会⾃动创建此⽂件
⑤FileWriter(file,false) / FileWriter(file):对原有⽂件的覆盖
⑥FileWriter(file,true):不会对原有⽂件覆盖,⽽是在原有⽂件基础上追加内容
File file = new File("hello2.txt");
    FileWriter fileWriter =null;
    try {
        //1.创建流对象,建立数据存放文件
//            fileWriter = new FileWriter(file);
        //默认append参数是false,true的话写入的东西会拼接到原来文件内容的后面
        fileWriter = new FileWriter(file,true);
        //2.调用流对象的写入方法,将数据写入流
        fileWriter.write("你好哈哈哈哈");
    } catch (IOException e) {
        throw new RuntimeException(e);
    }finally {
        if (fileWriter!=null){
            try {
                //3.关闭流资源,并将流中的数据清空到文件中
                fileWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

4.字节流

(1)FileInputStream 和 FileOutputStream
(2)基本操作
①创建⽂件
②创建字节流
③读取数据 写⼊数据
④关闭流操作
//1.创建文件
File file = new File("微信图片_20220303205509.jpg");
File file1 = new File("copy.jpg");

//2.创建字节流
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
   //2.创建字节流,输出流
   fileInputStream = new FileInputStream(file);
//写入字节流
    fileOutputStream = new FileOutputStream(file1);

   //3.读取数据
   byte [] bytes = new byte[1024];
   int len;
   while ((len = fileInputStream.read(bytes))!=-1){
    fileOutputStream.write(bytes,0,len);

} catch (FileNotFoundException e) {
    throw new RuntimeException(e);
}catch (IOException e) {
throw new RuntimeException(e);
}finally {
if (fileInputStream!=null){
    try {
        fileInputStream.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
if (fileOutputStream!=null){
    try {
        fileOutputStream.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
}

5.缓冲流

(1)缓冲过程
①当使⽤BufferedInputStream读取字节⽂件时,BufferedInputStream会⼀次性 从⽂件中读取8192个(8Kb),存在缓冲区中,直到缓冲区装满了,才重新从 ⽂件中 读取下⼀个8192个字节数组。
②向流中写⼊字节时,不会直接写到⽂件,先写到缓冲区中直到缓冲区写满, BufferedOutputStream才会把缓冲区中的数据⼀次性写到⽂件⾥。使⽤⽅法 flush()可以强制将缓冲区的内容全部写⼊输出流
(2)缓冲字节流:BufferInputStream 和 BufferOutputStream
//1.创建文件
File file = new File("项目二.pptx");
File file1 = new File("项目二2.pptx");

//2.创建字节流
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
//2.创建字节流,输出流
fileInputStream = new FileInputStream(file);
//写入字节流
fileOutputStream = new FileOutputStream(file1);

//3.创建bufferedInputStream
bufferedInputStream = new BufferedInputStream(fileInputStream);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

//4.读取数据
byte [] bytes = new byte[1024];
int len ;
while ((len=bufferedInputStream.read(bytes))!=-1){
    bufferedOutputStream.write(bytes,0,len);
}

} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}catch (IOException e) {
throw new RuntimeException(e);
}finally {
if (fileInputStream!=null){
    try {
        fileInputStream.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
if (fileOutputStream!=null){
    try {
        fileOutputStream.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
}
(3)缓冲字符流:BufferReader 和 BufferWriter
File file = new File("hello.txt");
    File file1 = new File("hello2.txt");

    FileReader fileReader = null;
    FileWriter fileWriter = null;
    BufferedReader bufferedReader = null;
    BufferedWriter bufferedWriter = null;
    try {
        fileReader = new FileReader(file);
        fileWriter = new FileWriter(file1);
        bufferedReader = new BufferedReader(fileReader);
        bufferedWriter = new BufferedWriter(fileWriter);

        //第一种方式
        int len;
        char [] chars = new char[1024];
        while ((len = bufferedReader.read(chars))!=-1) {
            bufferedWriter.write(chars, 0, len);

        }
        //String s = bufferedReader.readLine();
//            String s1 = bufferedReader.readLine();
//            System.out.println(s);
//            System.out.println(s1);

        //第二种方式
        //第二种方式
        String s ;
        while ((s=bufferedReader.readLine())!=null){
            bufferedWriter.write(s);
            bufferedWriter.newLine();
        }
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }catch (IOException e) {
        throw new RuntimeException(e);
    }

6.转换流

(1)转换流提供了再字节流和字符流之间的转换
(2)字节流中的数据都是字符时,转成字符流操作更高效
(3)使用转换流来处理文件乱码问题,实现编码(字符—>字节)和解码(字节—> 字符)的功能
(4)InputStreamReader 和 OutputStreamWriter
InputStreamReader  	将InputStream转换为Reader	实	现将字节的输入流按指定字符集转换为字符的输入流
OutputStreamWriter	将Writer转换为OutputStream	实现将字符的输出流按指定字符集转换为字节的输出流

File file = new File("hello.txt");
    File file1 = new File("hello4.txt");

    FileInputStream fileInputStream  = null;
    FileOutputStream fileOutputStream = null;
    InputStreamReader inputStreamReader  = null;
    OutputStreamWriter outputStreamWriter = null;
    try {
        fileInputStream = new FileInputStream(file);
        fileOutputStream = new FileOutputStream(file1);
	//添加编码格式
        inputStreamReader = new InputStreamReader(fileInputStream,"gbk");
        outputStreamWriter = new OutputStreamWriter(fileOutputStream,"gbk");
        int len=0;
        char [] c =new char[1024];
        while ((len=inputStreamReader.read(c))!=-1){
//                System.out.println(c);
            outputStreamWriter.write(c,0 ,len );
        }

    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }catch (IOException e) {
        throw new RuntimeException(e);
    }finally {
        try {
            if (inputStreamReader!=null){
                inputStreamReader.close();
            }
            if (outputStreamWriter!=null){
                outputStreamWriter.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

7.对象流

(1)ObjectInputStream 和 ObjectOutputStream
(2)⽤于存储和读取基本数据类型数据或对象的处理流
(3)将java对象写⼊到数据源中
(4)也可以把对象从数据源⾥还原回来
(5)对象的序列化
①序列化:⽤ObjectOutputStream类保存基本类型数据或对象的机制
②反序列化:⽤ObjectInputStream类读取基本类型数据或对象的机制
Person person = new Person("大人");
File file = new File("Person.dat");

FileOutputStream fileOutputStream = null;
ObjectOutputStream objectOutputStream = null;
FileInputStream fileInputStream = null;
ObjectInputStream objectInputStream = null;
try {
  //序列化
    fileOutputStream = new FileOutputStream(file);
 	 objectOutputStream = new 	ObjectOutputStream(fileOutputStream);
	 objectOutputStream.writeObject(person);

//反序列化
  fileInputStream = new FileInputStream(file);
   objectInputStream = new ObjectInputStream(fileInputStream);

	Person p = (Person) objectInputStream.readObject();
	System.out.println(p);

} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}finally {
if (objectOutputStream!=null){
    try {
        objectOutputStream.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
}

8.serialVersionUID的理解

(1)自定义对象序列化和反序列化
(2)凡是实现Serializable接⼝的类都有⼀个表示序列化版本标识符的静态变量:private static final long serialVersionUID其⽬的是以序列化对象进⾏版本控制
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值