在说流的类别之前,先说说什么是流,流其实就是对输入输出设备的抽象,可以把输入输出流理解为是一个通道,输入输出是相对程序而言的,如果是输出流,也就是往文件中写文件,而输入流,则是从文件中读取文件。从三个方面对IO流进行总结,一、字节流(一般都是xxxStream),二、字符流(xxxRead、xxxWrite),三、缓冲流。其实也可以简单的分为两类,分别是输入流和输出流。
在讲解IO流之前,有必要说说文件的操作,毕竟IO操作大部分也就是文件嘛。好了先来看看JDK-API文档吧,首先看看构造方法。
File file = new File("a.txt");
boolean newFile = file.createNewFile();
File file1 = new File("test");
file1.mkdir();
还有判断文件是否存在的方法也很常用
boolean exists = file.exists();
输入流,是相对于程序而言的,也就是从文件中读取文件,先看构造方法。
// 创建字节输入流对象
FileInputStream fis1 = new FileInputStream("a.txt");
// 用单字节进行读取
int x = 0;
while ((x = fis1.read()) != -1) {
System.out.println((char) x);
}
这样一波操作之后,他会把a.txt文件里的内容读取出来,但是是单字节的读的,单字节的效率还是比较低的,一般根据实际情况来进行自定义字节数读取,下面通过自定义字节搞一波。
// 创建字节输入流对象
FileInputStream fis2 = new FileInputStream("a.txt");
// 用字节数组进行读取
byte[] b = new byte[1024];
int len = 0;
while ((len = fis2.read(b)) != -1) {
System.out.print(new String(b, 0, len));
}
输出流,可以将文件写入到文件中,一般日志文件写的比较多。
//创建字节输出流对象
FileOutputStream fos = new FileOutputStream("a.txt");
//调用write()方法
fos.write("hello".getBytes());
这样一波操作之后,就可以把“hello”字符串转化为字节,然后写入到文件中,也可以读取a.txt文件中的内容,写入到b.txt文件中
InputStream in = new FileInputStream("a.txt");
OutputStream os = new FileOutputStream("b.txt");
byte[] bytes = new byte[2];
int n;
while ((n = in.read(bytes)) != -1) {
os.write(bytes, 0, n);
}
一个汉字大约占两个字节,而当用字节流处理的时候,可能会出现乱码的情况。字符输入流FileRead,先来体验一下,老规矩,先来构造方法。
Reader r = new FileReader("a.txt");
int n;
char[] chars = new char[2];
while ((n = r.read(chars)) != -1) {
String s = new String(chars,0,n);
}
其实跟字节流差不多,只是这里用char[]字符数组来进行操作了。
直接上构造方法
Reader r = new FileReader("a.txt");
Writer w = new FileWriter("b.txt");
int n;
char[] chars = new char[3];
while ((n = r.read(chars)) != -1) {
w.write(chars,0,n);
}
老规矩,先看构造方法
BufferedInputStream bis2 = new BufferedInputStream(new FileInputStream("a.txt"));
// 用字节数组进行读取
byte[] b = new byte[1024];
int len = 0;
while ((len = bis2.read(b)) != -1) {
System.out.print(new String(b, 0, len));
}
字节缓冲输出流跟输入流差不多,可以类比着看。
InputStream inputStream = new FileInputStream("a.txt");
BufferedInputStream bis = new BufferedInputStream(inputStream);
OutputStream outputStream = new FileOutputStream("b.txt");
BufferedOutputStream bos = new BufferedOutputStream(outputStream);
byte[] b = new byte[1024];
int n = 0;
while (bis.read(b) != -1) {
bos.write(b);
}
字符缓冲输入流的参数是字符流
Reader in = new FileReader("a.txt");
BufferedReader bufferedReader = new BufferedReader(in);
String str;
while ((str = bufferedReader.readLine()) != null) {
System.out.println(str);
}
同样的,以读取a.txt文件的内容到b.txt为例
Reader in = new FileReader("a.txt");
Writer out = new FileWriter("b.txt");
BufferedReader bufferedReader = new BufferedReader(in);
BufferedWriter bufferedWriter = new BufferedWriter(out);
String str;
while ((str = bufferedReader.readLine()) != null) {
bufferedWriter.write(str);
bufferedWriter.newLine();
}
上面的demo中,为了让代码简介减少重复,就没有对流进行关闭操作,这里统一说明一下,流在使用后,要进行close()关闭。
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- ryyc.cn 版权所有 湘ICP备2023022495号-3
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务