IO流(文件字节,字符流)
IO流(文件字节,字符流)
一,前言
了解流的分类,掌握字节字符流的概念和常用方法。
二,Java流的分类
按流的方向分:输入流,输出流
输入流:InputStream,Reader
输出流:OutputStream,Writer
按数据单位分:字节流,字符流
字节流:InputStream,OutputStream
字符流:Reader,Writer
注:都为抽象类,需使用子类实现
三,字节流
1.概念
字节流:可用操作任意数据,因为计算机中任何数据都是以字节流的形式处理的,一般用户用字节流处理视屏、音频、图片等,字节流处理纯文本比较慢,在操作时本身不会用到缓冲区(内存),是文件本身直接操作的;
2.文件字节输入流(FileInputStream)
InputStream类是表示字节输入流的所有类的超类,实现需要定义 InputStream 子类。如下使用文件字节输入流(FileInputStream)示例:
构造方法:
FileInputStream(File file):
通过打开与实际文件的连接创建一个 FileInputStream ,该文件由文件系统中的 File对象 file命名。
FileInputStream(String name):
通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系统中的路径名 name命名。
常用方法:
void close() :
关闭此输入流并释放与该流关联的所有系统资源。
int read(byte[] b) :
从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。
实现例:
public static void main(String[] args) throws IOException {
//创建文件字节输入流
File file = new File("E:\\aaa.txt");
InputStream fis = new FileInputStream(file);
//创建byte数组容器接收流读取的字节,提高效率
byte bt[] = new byte[3];
//读取数据
int len = 0;
while ((len = fis.read(bt))!=-1 ) {
//输入读写的数据,构造不多读
System.out.print(new String(bt,0, len));
}
//关流
fis.close();
}
3.文件字节输出流(OutputStream)
OutputStream类是表示字节输入流的所有类的超类,实现需要定义 OutputStream子类。如下使用文件字节输出流(OutputStream)示例:
构造方法:
FileOutputStream(File file):
创建文件输出流以写入由指定的 File对象表示的文件。
FileOutputStream(String name):
创建文件输出流以指定的名称写入文件。
常用方法:
void close():
关闭此文件输出流并释放与此流相关联的任何系统资源。
void write(byte[] b,int off,int len):
将 b.length个字节从指定的字节数组写入此文件输出流。
void flush():
刷新此输出流并强制任何缓冲的输出字节被写出。
实现例:
public static void main(String[] args) throws IOException {
//创建文件字节输出流
File file = new File("C:\\Users\\29854\\Desktop\\aaa.txt");
OutputStream fos = new FileOutputStream(file,true);
//创建需写的字符串
String str = "asdascasd";
//创建byte数组容器接收写入的字节,提高效率
byte by[] = str.getBytes();
//写入数据
fos.write(by);
//刷新
fos.flush();
//关流
fos.close();
}
四,字符流
1.概念
字符流操作纯文本的数据比较方便,最小单位按单个字符读取,避免乱码,在操作时使用了缓冲区,通过缓冲区再操作文件。
2.文件字符输入流(Reader)
Reader类是表示字符输入流的所有类的超类,实现需要定义 Reader子类。如下使用文件字符输入流(FileReader)示例:
构造方法:
FileReader(File file):
创建一个新的 FileReader ,给出 File读取。
FileReader(String fileName):
创建一个新的 FileReader ,给定要读取的文件的名称。
常用方法:
void close():
关闭流并释放与之相关联的任何系统资源。
int read(char[] cbuf):
将字符读入数组。
实现例:
public static void main(String[] args) throws IOException {
//创建文件字符输入流
File file = new File("E:/aaa.txt");
Reader fr = new FileReader(file);
//创建char数组容器接受读取的字符,提高效率
char c[] = new char[1024];
//读取数据
int len = 0;
while ((len = fr.read(c)) != -1) {
//输出读取的数据,构造不多读
System.out.println(new String(c,0,len));
}
//关流
fr.close();
}
3.文件字符输出流(Writer)
Writer类是表示字符输出流的所有类的超类,实现需要定义 Writer子类。如下使用文件字符输出流(FileWriter)示例:
构造方法:
FileWriter(File file)
给一个File对象构造一个FileWriter对象。
FileWriter(String fileName)
构造一个给定文件名的FileWriter对象。
常用方法:
void close():
关闭流并释放与之相关联的任何系统资源。
void write(char[] cbuf, int off, int len):
写入字符数组的一部分。
void flush():
刷新此输出流并强制任何缓冲的输出字符被写出。
实现例:
public static void main(String[] args) throws IOException {
//创建文件字符输出流
File file = new File("E:/Java/aaa.txt");
Writer fw = new FileWriter(file);
//创建需写的字符串
String str = "asdascasdasdascasdasdasc阿斯顿";
//写入数据
fw.write(str);
//刷新
fw.flush();
//关流
fw.close();
}
五,字节字符流的操作示例
- 字节流例:
public static void main(String[] args) {
File file = new File("E:\\aaa.txt");
File f = new File("E:\\java\\bbb.txt");
InputStream fis = null;
OutputStream ops = null;
byte by[] = new byte[1024];
int len = 0;
try {
fis = new FileInputStream(file);
ops = new FileOutputStream(f);
while ((len = fis.read(by)) != -1) {
ops.write(by,0,len);
ops.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ops != null) {
try {
ops.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 字符流例:
public static void main(String[] args) {
File file = new File("E:/aaa.txt");
File f = new File("C:\\Users\\29854\\Desktop\\aaa.txt");
Reader fr = null;
Writer fw = null;
try {
fr = new FileReader(file);
fw = new FileWriter(f,true);
char c[] = new char[1024];
int len = 0;
while ((len = fr.read(c)) != -1) {
fw.write(c,0,len);
fw.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr != null) {
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律