字节流与字符流
File类不支持对文件内容进行相关的操作,所有若要处理文件的内容,则需要通过流操作模式来完成。
流的基本操作步骤:
Step1:根据文件路径创建File类对象.
Step2:根据字节流或字符流的子类实例化父类对象.
Step3:进行数据的读取或写入操作.
Step4:关闭流(Close()).
(1)字节流
A. 字节输出流(OutputStream)
(1)Outputstream的类结构
public abstract class OutputStream implements Closeable, Flushable
观察OutputStream的结构,可以发现其为一个抽象类(要想对父类实例化必须使用子类)并且实现了Closeable和Flushable两个接口:
public interface Closeable extends AutoCloseable
public interface Flushable
若要通过程序对内容进行输出,则需要引入java.io.OutputStream.
(1)Outputstream类提供的方法
因为OutputStream为抽象类,所以要用子类对其进行实例化操作。我们若要对文件进行操作,则需用到FileOutputStream类对其进行实例化。
|
将给定字符数组的内容全部输出: public void write(byte b[]) throws IOException |
|
将部分字节数组的内容输出: public void write(byte b[], int off, int len) throws IOException |
|
输出单个字节: public abstract void write(int b) throws IOException |
-
public class Test2 {
-
public static void main(String[] args) throws IOException {
-
String singal = File.separator;
-
//1.根据文件路径创建File对象
-
File file = new File("G:"+singal+"lemon"+singal+"testIO"+singal+"TestIO.java");
-
//保证父目录存在
-
if(!file.getParentFile().exists()) {
-
//若目录不存在则创建多级目录
-
file.getParentFile().mkdirs();
-
}
-
//根据字节流的子类实例化父类对象
-
//此时只能操作File类
-
OutputStream outputStream = new FileOutputStream(file);
-
//使得内容实现追加操作,而非覆盖
-
//OutputStream outputStream = new FileOutputStream(file,true);
-
//将指定语句输出到文件的内容中
-
String string = "今天真热啊!";
-
//将内容变为字节数组
-
outputStream.write(string.getBytes());
-
//将内容变为字节数组,并且设置要输出的范围
-
//outputStream.write(string.getBytes(),0,5);
-
outputStream.close();
-
}
-
}
在进行文件输出的时候,所有的文件会自动帮助用户创建,不在需要调用createFile()方法手工创建。
当我们运行多次时发现文件中的内容仍然只有一句,这是因为FileOutputStream(File file)该构造函数只能实现内容的覆盖并不能实现内容的追加处理,若想使得内容得到追加,则需使用提供的另外一个构造函数 FileOutputStream(File file,boolean append)来实现。
实现追加输出运行结果如下:
将给定部分内容输出结果如下:
(2)字符流
字符流适用于处理中文数据
A. 字符输出流(Writer)
public abstract class Writer implements Appendable, Closeable, Flushable比OutputStream多实现了一个Appendable接口。
在Writer类里面也提供write()方法,而且该方法接收的类型都是char型,要注意的是,Writer类提供了一个直接输出字符串的方法
public void write(String str) throws IOException
Writer类的结构与方法的使用与OutputStream非常相似,只是Writer类对于中文的支持很好并且提供了直接写入 String的方法而已。
-
/*
-
* 字符输出流
-
* */
-
public class Test2{
-
public static void main(String[] args) throws IOException {
-
String singal = File.separator;
-
//1.根据文件路径创建File对象
-
File file = new File("G:"+singal+"lemon"+singal+"testIO"+singal+"TestIO.java");
-
//利用子类实例化父类对象
-
Writer writer = new FileWriter(file);
-
writer.write("你好");
-
writer.close();
-
}
-
}
B. 字符输入流(Reader)
public abstract class Reader implements Readable, Closeable
Reader仍然为抽象类,若要对文件进行读取操作,则要使用FileReader对其进行实例化操作。
由上面的Writer类中可知其提供的方法可以直接向目标源写入字符串,但是Reader类提供的方法中只能通过字符数组进行读取操作,而没有直接的方法使其可以直接读取字符串类型。
-
/*
-
* 字符输入流
-
* */
-
public class Test2{
-
public static void main(String[] args) throws IOException {
-
String singal = File.separator;
-
//1.根据文件路径创建File对象
-
File file = new File("G:"+singal+"lemon"+singal+"testIO"+singal+"TestIO.java");
-
//利用子类实例化父类对象
-
Reader reader = new FileReader(file);
-
char[] cs = new char[1024];
-
int len = reader.read(cs);
-
String result = new String(cs,0,len);
-
System.out.println(result);
-
//关闭流
-
reader.close();
-
}
-
}
(3)字符流与字节流的区别
A. 字节流是原生操作,而字符流是经过加工处理后的操作。
B. 字符流适合处理中文,字节流适合处理一切数据类型(对中文支持不好)。
C. 所有字符流的操作,无论是写入还是输出,数据都先保存在缓存中。
D. 如果字符流不关闭,数据就有可能保存在缓存中并没有输出到目标源。这种情况下就必须强制刷新才能够得到完整数据。
-
public class Test2{
-
public static void main(String[] args) throws IOException {
-
String singal = File.separator;
-
//1.根据文件路径创建File对象
-
File file = new File("C:"+singal+"Users"+singal+"lenovo"+singal+"DeskTop"+singal+"Test.txt");
-
if(!file.getParentFile().exists()) {
-
file.getParentFile().mkdirs();
-
}
-
Writer writer = new FileWriter(file);
-
writer.write("hello world !");
-
//进行刷新,若不进行刷新,则内容无法写入
-
//强制清空所有缓冲区内容,所有内容都输出
-
//writer.flush();
-
}
-
}