常用其基本子类FileOutputStream构造,OutputStream的常用方法有:
write(int b)
将指定的字节写入此输出流。b为某字符的byte值。write(byte[] b)
将 b.length字节从指定的字节数组写入此输出流。write(byte[] b, int off, int len)
从指定的字节数组写入 len字节,从偏移量 off开始输出到此输出流。try (OutputStream out1 = new FileOutputStream("c.txt")) {
// 1. 直接写入字节
// out1.write('g');
out1.write('c');
// 2. 写入字节数组
byte[] b = "爱老虎油".getBytes();
out1.write(b);
// 3. 写入数组的一部分
byte[] b2 = {97, 98, 99, 100};
out1.write(b2, 0, 2); // 从0的位置开始向后写入数组b2的两个元素
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
该部分主要讲解构造方法;
FileOutputStream(File file)
创建文件输出流以写入由指定的 File对象表示的文件。如果文件存在且有内容会覆盖。FileOutputStream(File file, boolean append)
创建文件输出流以写入由指定的 File对象表示的文件。追加 。FileOutputStream(String name)
创建文件输出流以指定的名称写入文件。 覆盖。FileOutputStream(String name, boolean append)
创建文件输出流以指定的名称写入文件。 追加。利用BufferedOutputStream类会在电脑硬盘与程序之间建立缓冲区,默认是8k,用于将读取的内容暂存在内存中,从而加快程序读取内容的效率。
构造方法有:
BufferedOutputStream(OutputStream out)
创建一个新的缓冲输出流,以将数据写入指定的底层输出流。BufferedOutputStream(OutputStream out, int size)
创建一个新的缓冲输出流,以便以指定的缓冲区大小将数据写入指定的底层输出流。 size是缓冲区的大小,默认8k。public class Demo {
public static void main(String[] args) {
try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("a.txt", true)); // 由于OutputStream是抽象类,故用FileOutputStream来创建。
BufferedInputStream in = new BufferedInputStream(new FileInputStream("配置文件.txt"))) { // 输出流--缓冲流
int t = 0;
byte[] b = new byte[1024];
while ((t = in.read(b)) != -1) {
out.write(b, 0, t);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
注:其写入方法write()用法相同。
1).什么是“序列化”:将一个“对象”连同“属性值”整体存储到一个文件中,这个过程叫:序列化。
2).什么是“反序列化”:将之前被序列化的对象,再读取到内存中,并重新生成对象,并设置属性值,这个过程叫:反序列化。
注意:被序列化的类,必须实现java.io.Serializable接口
ObjectOutputStream(OutputStream out)
创建一个写入指定的OutputStream的ObjectOutputStream。writeObject(Object obj);
序列化的写入方法public class Demo {
public static void main(String[] args) {
Student stu = new Student("成龙", 17);
//序列化
try (ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("demo07_obj.txt"))) {
//序列化一个对象
oos.writeObject(stu);//将Student对象连同属性值一起存储到了demo07_obj.txt文件中
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
注:字节流的输入方法与输出方法用法几乎相同,这里就不再列举了。
1.write(int c)
写一个字符
2.void write(String str)
写一个字符串
3.void write(String str, int off, int len)
写一个字符串的一部分。
FileWriter(File file)
给一个File对象构造一个FileWriter对象。FileWriter(File file, boolean append)
给一个File对象构造一个FileWriter对象。FileWriter(String fileName)
构造一个给定文件名的FileWriter对象。FileWriter(String fileName, boolean append)
构造一个FileWriter对象,给出一个带有布尔值的文件名,表示是否附加写入的数据。BufferedWriter(Writer out)
创建使用默认大小的输出缓冲区的缓冲字符输出流。BufferedWriter(Writer out, int sz)
创建一个新的缓冲字符输出流,使用给定大小的输出缓冲区。转换流是字符与字节之间的桥梁,例如读取GBK编码的文件时,普通读法是直接读取,由于程序默认是以utf8编码都进来的,故而出现乱码。此时我们就要指定编码格式。
OutputStreamWriter(OutputStream out)
创建一个使用默认字符编码的OutputStreamWriter。OutputStreamWriter(OutputStream out, String charsetName)
创建一个使用命名字符集的OutputStreamWriter。public class Demo {
public static void main(String[] args) {
try (OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("a.txt"), "GBK");
InputStreamReader in = new InputStreamReader(new FileInputStream("测试文件.txt"))) {
int t = 0;
char[] ch = new char[1024];
while ((t = in.read(ch)) != -1) {
out.write(ch, 0, t);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
同样,转换流的输入流用法相同,这里就不再列举了。