JAVA全系列 教程
3762个小节阅读:7089.8k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
FileInputStream通过字节的方式读取文件,适合读取所有类型的文件(图像、视频、文本文件等)。
FileOutputStream 通过字节的方式写数据到文件中,适合所有类型的文件(图像、视频、文本文件等)。
FileInputStream文件输入字节流
xxxxxxxxxx
public class TestFileInputStream {
public static void main(String[] args) {
//使用try-with-resource方式关闭资源。
//在try中打开资源,不需要在代码中添加finally块关闭资源。
try(FileInputStream fis = new FileInputStream("d:/a.txt");){
StringBuilder sb = new StringBuilder();
int temp=0;
while((temp = fis.read()) != -1){
sb.append((char) temp);
}
System.out.println(sb);
}catch(Exception e){
e.printStackTrace();
}
}
FileOutputStream文件输出字节流
xxxxxxxxxx
public class TestFileOutputStream {
public static void main(String[] args) {
String str = "Old Lu";
// true表示内容会追加到文件末尾;false表示重写整个文件内容。
try(FileOutputStream fos = new FileOutputStream("d:/a.txt",true)){
//将整个字节数组写入到文件中。
fos.write(str.getBytes());
//将数据从内存中写入到磁盘中。
fos.flush();
}catch (IOException e){
e.printStackTrace();
}
}
}
实时效果反馈
1.FileInputStream是什么流?
A 文件字节输入流;
B 文件字符输入流;
C 文件随机访问流;
D 文件对象输入流;
2.FileOutputStream是什么流?
A 文件字节输出流;
B 文件字符输出流;
C 文件随机访问流;
D 文件对象输出流;
答案
1=>A 2=>A