1.OutputStream类
OutputStream是一个抽象类,是表示字节输出流的所有类的超类。操作的数据都是字节,定义了输出字节流的基本共性功能方法。
OutputStream类中常见的方法:
2.FileOutputStream类
OutputStream 有很多子类,其中子类 FileOutputStream 可用来写入数据到文件。
FileOutputStream类 通过字节的方式写数据到文件,适合所有类型文件(图像、视频、文本文件等)。
- 构造方法
- 写入数据到文件中
【示例】将数据写到文件中
public class FileOutputStreamDemo {
public static void main(String[] args) {
// 创建存储数据的文件
File file = new File("test.txt");
OutputStream os = null;
try {
// 创建一个字节输出流,明确需要操作的文件
// 如果文件不存在,则创建;如果文件存在,则覆盖!
os = new FileOutputStream(file);
// 调用父类的方法存数据。立刻完成存储,不需要强制刷新存入
os.write("bjsxt".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 一定要判断 os 是否为 null,只有不为null时,才可以关闭资源
if(os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
- 给文件中续写
我们直接 new FileOutputStream(file)这样创建对象,写入数据,会覆盖原有的文件,那么我们想
在原有的文件中续写内容怎么办呢?
继续查阅 FileOutputStream 的 API。发现在 FileOutputStream 的构造函数中,可以接受一个 boolean类型的值,如果值 true,就会在文件末位继续添加。
【示例】将数据续写到文件中
public class FileOutputStreamDemo {
public static void main(String[] args) {
// 创建存储数据的文件
File file = new File("test.txt");
OutputStream os = null;
try {
// 创建一个字节输出流,明确需要操作的文件
os = new FileOutputStream(file, true);
// 调用父类的方法存数据。
os.write("bjsxt".getBytes());
// 添加换行
os.write("\r\n".getBytes());
os.write("bjsxt".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 一定要判断 os 是否为null,只有不为null时,才可以关闭资源
if(os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
3.InputStream类
InputSteam是一个抽象类,是表示字节输入流的所有类的超类。操作的数据都是字节,定义了字节输入流的基本共性功能方法。
InputStream类中常见的方法:
int read(): 读取一个字节的数据,并将字节的值作为int类型返回(0-255之间的一个值)。如果未读出字节则返回-1。
int read(byte[]): 读取一定量的字节数,并存储到字节数组中,返回读取到的字节数。如果未读出字节则返回-1。
4.FileInputStream类
InputStream 有很多子类,其中子类 FileInputStream 可用来写入数据到文件。
FileInputStream通过字节的方式读取文件,适合读取所有类型文件(图像、视频、文本文件等)。
- 构造方法
- 读取数据 read()方法
在读取文件中的数据时,调用 read 方法一次读一个字节,从而实现从文件中读取数据。
【示例】读取数据 read() 方法
public class FileInputStreamDemo {
public static void main(String[] args) {
FileInputStream fs = null;
try {
// 创建一个字节输入流对象,必须明确数据源
// 如果文件不存在,则抛出FileNotFoundException异常!
fs = new FileInputStream("input.txt");
// 一次读一个字节
int len = 0;
// 当返回值为-1,则数据读取完毕
while((len = fs.read()) != -1) {
System.out.println((char)len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fs != null) {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
- 读取数据 read(byte[])方法
在读取文件中的数据时,调用 read 方法,每次只能读取一个字节,太麻烦了,于是我们可以定义数组作为临时的存储容器,这时可以调用重载的 read 方法,一次可以读取多个字节。
【示例】读取数据 read(byte[])方法
public class FileInputStreamDemo {
public static void main(String[] args) {
FileInputStream fs = null;
try {
// 创建一个字节输入流对象,必须明确数据源
fs = new FileInputStream("test.txt");
StringBuilder sb = new StringBuilder();
byte[] by = new byte[1024];
int len = 0;//保存获取到字节的长度
// 当返回值为-1,则数据读取完毕
while((len = fs.read(by)) != -1) {
sb.append(new String(by, 0, len));
}
// 输出内容
System.out.println(sb);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fs != null) {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
- 一次性读取文件全部数据
使用FileInputStream类的available()方法,可以获取文件的所有字节个数。但是该方法建议少用,如果遇到文件数据量很大,容易造成内存溢出。
【示例】一次性读取文件全部数据
public class FileInputStreamDemo {
public static void main(String[] args) {
FileInputStream fs = null;
try {
// 创建一个字节输入流对象,必须明确数据源
fs = new FileInputStream("test.txt");
int len = 0;
// 获取文件字节个数
int available = fs.available();
byte[] by = new byte[available];
// 一次性获取文件所有数据
fs.read(by);
System.out.println(new String(by));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fs != null) {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
保存文件时编码的区别:
utf-8:英文字符占用一个字节,中文字符占用三个字节。
GBK:英文字符占用一个字节,中文字符占用两个字节。
ANSI:不同的国家和地区采用不同的编码,例如在大陆的本地码,就是GBK编码。
Unocode:每个字符都是占用两个字节!
5.字节流文件拷贝案例
原理:读取一个已有的数据,并将这些读到的数据写入到另一个文件中。
【示例】字节流文件的拷贝案例
public class FileCopyDemo {
public static void main(String[] args) throws IOException {
// 明确字节流,输入流和源相关联,输出流和目的关联。
FileInputStream fis = new FileInputStream("E://movie.mp4"); // 输入流
FileOutputStream fos = new FileOutputStream("F://111.mp4"); // 输出流
// 定义一个数组,用于缓存取出来的数据
byte[] by = new byte[1024];
int len = 0;
// 输入流,读取数据
while ((len = fis.read(by)) != -1) {
// 输出流,保存数据
fos.write(by, 0, len);
}
// 关闭流
fis.close();
fos.close();
}
}
ps:如需最新的免费文档资料和教学视频,请添加QQ群(627407545)领取。