苏州新区做网站免费营销培训
十三章:IO 流
一、File 类的使用
1、File 类的一个对象,代表一个文件或文件目录(俗称:文件夹)
2、File 类声明在 Java.io 包下面
相对路径:相较于某个路径下,指明的路径。
绝对路径:包含盘符在内的文件或文件目录的路径
File 中三种构造器:
File 类中的常用方法
13-2 IO 流原理及流的分类
I/O 是 Input/Output 的缩写 用于处理设备之间的数据传输
IO 流分类
以下的代码可以仔细看一看
package com.java4.kcw;import org.junit.Test;import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;/**一、流的分类* 操作数据单位:字节流、字符流* 数据的流向:流入流、流出流* 流的角色:节点流、处理流** 二、流的体系结构* 抽象基类 节点流 缓冲流* InputStream FileInputStream BufferedInputStream* OutputStream FileOutputStream BufferedOutputStream* Reader FileReader BufferedReader* Writer FileWriter BufferedWriter* @author Jackson_kcw* @Time 2025-03-02 PM4:51*/
public class FileReaderWriterTest {//将这个 hello.txt的内容读入程序,并输出到控制台//异常处理,为了保证流资源一定会被关闭,需要使用 try-catch-finally处理//读入的文件一定要存在,否则就会报 FileNotFoundException@Testpublic void testFileReader() {FileReader fr= null;try {//1、实例化 File 类的对象,指明要操作的文件File file=new File("hello.txt");//相较于当前 Module//2、提供具体的流fr = new FileReader(file);//3、数据的读入//read():返回读入的一个字符。如果达到文件末尾,返回-1int data= fr.read();while(data!=-1){System.out.print((char)data);data=fr.read();}} catch (IOException e) {throw new RuntimeException(e);} finally {//4、流的关闭操作if(fr!=null){try {fr.close();} catch (IOException e) {throw new RuntimeException(e);}}}}
/*
**********************************************************************************************8*///对 read()操作升级:使用 read的重载方法@Testpublic void testFileReader1() {FileReader fr= null;try {//1、File类的实例化File file =new File("hello.txt");//2、FileReader 流的实例化fr = new FileReader(file);//3、读入的操作//read(char[] cbuf) 返回每次读入 cbuf 数组中的字符的个数。如果达到文件末尾,返回-1char [] cbuf=new char[5];int len;while((len=fr.read(cbuf))!=-1){for(int i=0;i<len;i++){System.out.println(cbuf[i]);}}} catch (IOException e) {throw new RuntimeException(e);} finally {//4、资源关闭if(fr!=null){try {fr.close();} catch (IOException e) {throw new RuntimeException(e);}}}}//从内存中写出数据到硬盘的文件里/*说明:1、输出操作,对应的 File 可以不存在,如果不存在,会自动创建文件如果存在:如果流使用的构造器是 FileWriter(file,false):对原有文件的覆盖如果流使用的构造器是 FileWriter(file,true):不会覆盖,而是原文件下面继续添加内容*/@Testpublic void testFileWriter() {FileWriter fw= null;try {//1、提供 File 类的对象,指明写出到的文件File file=new File("hello1.txt");//2、提供 FileWriter 对象,用于数据的写出//下面的地方 若构造器中有append 如果写 true 则在原有基础上添加,如果为 false 就会覆盖操作fw = new FileWriter(file,true);//3、写出的操作fw.write("I have a dream\n");fw.write("Everyday is a new day!!!");} catch (IOException e) {throw new RuntimeException(e);} finally {if(fw!=null){//4、资源的关闭try {fw.close();} catch (IOException e) {throw new RuntimeException(e);}}}}@Testpublic void testFileReaderFileWriter(){FileReader fr= null;FileWriter fw= null;try {//1、创建 File 类的对象,指明读入和写出的文件File srcFile=new File("hello.txt");File destFile=new File("hello2.txt");//2、创建输入流和输出流的对象fr = new FileReader(srcFile);fw = new FileWriter(destFile);//3、数据的读入和写出操作char [] cbuf=new char[5];int len;//记录每次读入到 cbuf 数组中的字符的个数while((len=fr.read(cbuf))!=-1){//每次写出 len 个字符fw.write(cbuf,0,len);}} catch (IOException e) {throw new RuntimeException(e);} finally {//4、关闭流资源if(fr!=null){try {fr.close();} catch (IOException e) {throw new RuntimeException(e);}}if(fw!=null){try {fw.close();} catch (IOException e) {throw new RuntimeException(e);}}}}}
13-3 转换流
1、转换流:属于字符流
InputStreamReader:将一个字节的输入流转换为字符的输入流
OutputStreamWriter :将一个字符的输出流转换为字节的输出流
2、作用:提供字节流与字符流之间的转换
3、解码: 字节、字节数组—>字符数组、字符串
编码:字符数组、字符串—>字节、字节数组
总结
因为时间的关系,这部分的内容我主要是跳过,只了解第一种,等以后遇到这方面的问题时,再去具体去处理以及学习