java.io.OutputStream 抽象类是表示字节输出流的所有类的超类,将指定的字节信息写出到目的地。它定义了字节输出流的基本共性功能方法。
java.io.FileOutputStream 类是文件输出流,用于将数据写出到文件。
构造方法
当你创建一个流对象时,必须传入一个文件路径。该路径下,如果没有这个文件,会创建该文件。如果有这个文件,会清空这个文件的数据。
public class FileOutputStreamConstructor throws IOException {
public static void main(String[] args) {
// 使用File对象创建流对象
File file = new File("a.txt");
FileOutputStream fos = new FileOutputStream(file);
// 使用文件名称创建流对象
FileOutputStream fos = new FileOutputStream("b.txt");
}
}
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo {
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象
FileOutputStream fos = new FileOutputStream("fos.txt");
// 写出数据
fos.write(97); // 写出第1个字节
fos.write(98); // 写出第2个字节
fos.write(99); // 写出第3个字节
// 关闭资源
fos.close();
}
}
输出结果:
abc
public class Demo {
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象
FileOutputStream fos = new FileOutputStream("fos.txt");
//字符串转换成字节数组
byte[] b="我是字节数组".getBytes();
// 写出字节数组数据
fos.write(b);
// 关闭资源
fos.close();
}
}
输出结果:
我是字节数组
public class Demo {
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象
FileOutputStream fos = new FileOutputStream("fos.txt");
//字符串转换成字节数组
byte[] b = "abcdefg".getBytes();
// 写出从索引3开始,2个字节。索引3是d,两个字节,也就是de。
fos.write(b,3,2);
// 关闭资源
fos.close();
}
}
输出结果:
de
这两个构造方法,参数中都需要传入一个boolean类型的值, true 表示追加数据, false 表示清空原有数据
public class Demo {
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象
FileOutputStream fos = new FileOutputStream("fos.txt",true);
//字符串转换成字节数组
byte[] b = "abcdefg".getBytes();
fos.write(b);
// 关闭资源
fos.close();
}
}
文件操作前:de
文件操作后:deabcdefg
Windows系统里,换行符号是 \r\n 。代码演示:
public class Demo {
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象
FileOutputStream fos = new FileOutputStream("fos.txt");
// 定义字节数组
byte[] words = {97,98,99,100,101};
// 遍历数组
for (int i = 0; i < words.length; i++) {
// 写出一个字节
fos.write(words[i]);
// 写出一个换行, 换行符号转成数组写出
fos.write("\r\n".getBytes());
}
// 关闭资源
fos.close();
}
}
输出结果:
a
b
c
d
e
java.io.InputStream 抽象类是表示字节输入流的所有类的超类,可以读取字节信息到内存中。它定义了字节输入流的基本共性功能方法。
java.io.FileInputStream 类是文件输入流,从文件中读取字节。
构造方法
当你创建一个流对象时,必须传入一个文件路径。该路径下,如果没有该文件,会抛出 FileNotFoundException
public class Demo throws IOException{
public static void main(String[] args) {
// 使用File对象创建流对象
File file = new File("a.txt");
FileInputStream fos = new FileInputStream(file);
// 使用文件名称创建流对象
FileInputStream fos = new FileInputStream("b.txt");
}
}
public class Demo {
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象
FileInputStream fis = new FileInputStream("a.txt");// 文件中为abc
// 读取数据,返回一个字节
int read = fis.read();
System.out.println((char) read);
read = fis.read();
System.out.println((char) read);
read = fis.read();
System.out.println((char) read);
// 读取到末尾,返回-1
read = fis.read();
System.out.println( read);
// 关闭资源
fis.close();
}
}
输出结果:
a
b
c
-1
改进代码:
public class Demo {
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象
FileInputStream fis = new FileInputStream("a.txt");// 文件中为abc
// 定义变量,保存数据
int b;
// 循环读取
while ((b = fis.read())!=-1) {
System.out.println((char)b);
}
// 关闭资源
fis.close();
}
}
输出结果:
a
b
c
public class Demo {
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象
FileInputStream fis = new FileInputStream("a.txt");// 文件中为abc
// 定义变量,作为有效个数
int len;
// 定义字节数组,作为装字节数据的容器
byte[] b = new byte[2];
// 循环读取
while (( len = fis.read(b))!=-1) {
// 每次读取后,把数组变成字符串打印
System.out.println(new String(b));
}
// 关闭资源
fis.close();
}
}
输出结果:
ab
cb
错误数据 b,是由于最后一次读取时,只读取一个字节 c ,数组中,上次读取的数据没有被完全替换,所以要通过 len ,获取有效的字节,代码演示:
public class Demo {
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象
FileInputStream fis = new FileInputStream("a.txt");// 文件中为abc
// 定义变量,作为有效个数
int len;
// 定义字节数组,作为装字节数据的容器
byte[] b = new byte[2];
// 循环读取
while (( len = fis.read(b))!=-1) {
// 每次读取后,把数组变成字符串打印
System.out.println(new String(b,0,len));
}
// 关闭资源
fis.close();
}
}
输出结果:
ab
c
复制图片文件,代码演示:
public class Demo {
public static void main(String[] args) throws IOException {
// 1.创建流对象
// 1.1 指定数据源
FileInputStream fis = new FileInputStream("D:\\test.png");
// 1.2 指定目的地
FileOutputStream fos = new FileOutputStream("D:\\test_copy.png");
// 2.读写数据
// 2.1 定义数组
byte[] b = new byte[1024];
// 2.2 定义长度
int len;
// 2.3 循环读取
while ((len = fis.read(b))!=-1) {
// 2.4 写出数据
fos.write(b, 0 , len);
}
// 3.关闭资源
fos.close();
fis.close();
}
}
Java提供一些字符流类,以字符为单位读写数据,专门用于处理文本文件。
java.io.Reader 抽象类是表示用于读取字符流的所有类的超类,可以读取字符信息到内存中。它定义了字符输入流的基本共性功能方法。
构造方法
public class Demo throws IOException{
public static void main(String[] args) {
// 使用File对象创建流对象
File file = new File("a.txt");
FileReader fr = new FileReader(file);
// 使用文件名称创建流对象
FileReader fr = new FileReader("b.txt");
}
}
public class Demo {
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象
FileReader fr = new FileReader("a.txt");//内容为:我喜欢编程
// 定义变量,保存数据
int b ;
// 循环读取
while ((b = fr.read())!=-1) {
System.out.println((char)b);
}
// 关闭资源
fr.close();
}
}
输出结果:
我
喜
欢
编
程
public class Demo {
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象
FileReader fr = new FileReader("a.txt");//内容为:我喜欢编程
// 定义变量,保存数据
int len ;
// 定义字符数组,作为装字符数据的容器
char[] b = new char[2];
// 循环读取
while ((len = fr.read(b))!=-1) {
System.out.println(new String(b,0,len));
}
// 关闭资源
fr.close();
}
}
输出结果为:
我喜
欢编
程
java.io.Writer 抽象类是表示用于写出字符流的所有类的超类,将指定的字符信息写出到目的地。它定义了字节输出流的基本共性功能方法。
构造方法
public class Demo{
public static void main(String[] args) throws IOException {
// 使用File对象创建流对象
File file = new File("a.txt");
FileWriter fw = new FileWriter(file);
// 使用文件名称创建流对象
FileWriter fw = new FileWriter("b.txt");
}
}
写出字符: write(int b) 方法,每次可以写出一个字符数据,代码演示:
public class Demo {
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象
FileWriter fw = new FileWriter("b.txt");
// 写出数据
fw.write(97); // 写出第1个字符
fw.write('b'); // 写出第2个字符
fw.write('C'); // 写出第3个字符
fw.write(33333); // 写出第4个字符,中文编码表中30000对应一个汉字。
//如果不关闭,数据只是保存到缓冲区,并未保存到文件。
fw.close();
}
}
输出结果:
abC舵
因为内置缓冲区的原因,如果不关闭输出流,无法写出字符到文件中。但是关闭的流对象,是无法继续写出数据的。如果我们既想写出数据,又想继续使用流,就需要 flush 方法了。
flush :刷新缓冲区,流对象可以继续使用。 close :先刷新缓冲区,然后通知系统释放资源。流对象不可以再被使用了。
public class Demo {
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象
FileWriter b = new FileWriter("a.txt");
// 字符串转换为字节数组
char[] chars = "我喜欢编程".toCharArray();
// 写出字符数组
b.write(chars); // 我喜欢编程
// 写出从索引1开始,1个字节。索引1是'喜',两个字节,也就是'喜欢'。
b.write(chars,1,2); // 喜欢
// 关闭资源
b.close();
}
}
输出结果:
我喜欢编程喜欢
public class Demo {
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象
FileWriter b = new FileWriter("a.txt");
//字符串
String msg = "我喜欢编程";
// 写出字符数组
b.write(msg); //我喜欢编程
// 写出从索引1开始,2个字节。索引1是'喜',两个字节,也就是'喜欢'。
b.write(msg,1,2); // 喜欢
// 关闭资源
b.close();
}
}
public class Demo{
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象,可以续写数据
FileWriter b= new FileWriter("a.txt",true);
// 写出字符串
b.write("我喜欢");
// 写出换行
b.write("\r\n");
// 写出字符串
b.write("编程");
// 关闭资源
b.close();
}
}
输出结果:
我喜欢
编程
字符流,只能操作文本文件,不能操作图片,视频等非文本文件,要使用字节流