1.字节流与字符流:
(1)定义:
字节流是直接操作文件进行文件的读写,而字符流是通过操作 “缓冲区”(也就是内存),从而操作文件进行字符 的读写。
(2) 下面我们看一下两个例子说明:
//字节流操作
public static void main(String[] args) throws IOException {
File f=new File("d:\\test.txt");//首先通过file类 创建一个 test.txt文件
OutputStream out=null;
out=new FileOutputStream(f);
String str="hey girl where are you ?";
byte b[]=str.getBytes();
out.write(b);
}
找到test文件查看 发现里面已经写入了 我们想要输出的内容。
此时没有关闭字节流操作,但是文件中也依然存在了输出的内容,证明字节流是直接操作文件本身的
//字符流操作
public static void main(String[] args) throws IOException {
File f=new File("d:\\test.txt");//首先通过file类 创建一个 test.txt文件
Writer out=null;
out=new FileWriter(f);
String str="hey girl where are you ?";
out.write(str);
//out.close();
//强制清空缓冲区,来使字符输出
//out.flush();
}
此时如果没有关闭字符流 在test文件里面是空白的。如果加上out.close() 重新运行程序 test文件会显示 我们想要输出的字符串。说明字符流是通过操作 缓冲区从而操作文件的读写的。
如果想要不关闭字符流 那么采用out.flush()方法 也可以将字符输出出来。
所有的文件在硬盘或在传输时都是以字节的方式进行的,包括图片等都是按字节的方式存储的,而字符是只有在内存中才会形成,所以在开发中,字节流使用较为广泛。
2、包装流或打印输出流 PrintStream PrintWriter
(1)定义:PrintStream是用来封装OutputStream字节流对象的,代替进行IO操作,PrintWriter 不仅可以代替字节流OutputStream,还可以代替字符流 Writer进行输出!
(2)例子:
(1) //PrintWriter 代替字节流进行输出
public static void main(String[] args) throws IOException {
try (
//PrintWriter 代替OutputStream 进行IO输出。
FileOutputStream fos = new FileOutputStream("D:\\iotest.txt");
PrintWriter ps = new PrintWriter(fos)) {
ps.println("饺子酒,越喝越有\n");
//输出对象
ps.println(new OtherIO());
} catch (IOException e) {
e.printStackTrace();
}
}
(2) //PrintWriter 代替字符流进行输出
public static void main(String[] args) throws IOException {
try (
//PrintWriter 代替Writer进行IO输出。
FileWriter fos = new FileWriter("D:\\iotest.txt");
PrintWriter ps = new PrintWriter(fos)) {//PrintWriter或者PrintStream里面自动配置了flush(),使其不用关闭就能直接打印输出
ps.println("饺子酒,越喝越有\n");
//输出对象
ps.println(new OtherIO());
} catch (IOException e) {
e.printStackTrace();
}
}
(3) //PrintStream代替字节流对象OutputStream进行IO输出
public static void main(String[] args) throws IOException {
try (
FileOutputStream fos = new FileOutputStream("D:\\iotest.txt");
PrintStream ps = new PrintStream (fos)) {
ps.println("饺子酒,越喝越有\n");
//输出对象
ps.println(new OtherIO());
} catch (IOException e) {
e.printStackTrace();
}
}
3、缓冲流BufferedReader与BufferedWriter
(1)定义:
缓冲流:
再读写数据时,让数据在缓缓冲区能减少系统实际对原始数据来源的存取次数,因为一次能做多个数据单位的操作,相较而言,对于从文件读取数据或将数据写入文件,比起缓冲区的读写要慢多了。所以使用缓冲区的 流,一般都会比没有缓冲区的流效率更高,拥有缓冲区的流别称为缓冲流,包括BufferedInputStream、BufferedOutputStream类和BufferedReader、BufferedWriter类。缓冲流把数据从原始流成块读入或把数据积累到一个大数据块后再成批写出,通过减少通过资源的读写次数来加快程序的执行。
public static void main(String[] args) throws IOException {
try (
//BufferedReader将读取的数据存入缓冲区中,今后在读写此数据的时候 可直接从缓冲区读取,不用直接从原来的数据源中读取 ,提高效率
//InputStreamReader是从byte转成char的桥梁
InputStreamReader reader = new InputStreamReader(System.in);
//BufferedReader(Reader in)是char类型输入的包装类
BufferedReader br = new BufferedReader(reader);
) {
String line = null;
while ((line = br.readLine()) != null) {
if (line.equals("exit")) {
//System.exit(1);
break;
}
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
//BufferedReader是Reader的子类。Reader类的read()方法每次都从数据源读入一个字符(两个字节),为了提高效率,可以采用BufferedReader来配合其他字节流。BufferedReader带有缓冲区,它可以先把一批数据读入到缓冲区内。接下来的读操作都是从缓冲区获取数据,避免每次都从数据源读取数据并进行字符编码转换,从而提高操作效率。
//BufferedWriter类是Writer的子类。与Writer相比,BufferedWriter类主要改变是重写了flush()方法,该方法可以确保缓冲区里的数据确实被写到输出流中,使用BufferedWriter类时,写入的数据并不会先输出到目的地,而是先存储至缓冲区。BufferedWriter流提供了缓冲区,能更有效的写出字符流数据。
转载地址:https://blog.csdn.net/lwang_it/article/details/78886186