一、InputStream 、 InputStreamReader和BufferedReader的区别
- InputStream :是所有字节输入流的超类,一般使用它的子类:FileInputStream等,它能输出字节流;
- InputStreamReader :是字节流与字符流之间的桥梁,能将字节流输出为字符流,并且能为字节流指定字符集,可输出一个个的字符;
- BufferedReader :提供通用的缓冲方式文本读取,readLine读取一个文本行, 从字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取。
二、示例
1、InputStream
package src;
import java.io.*;
public class FileIO {
public static void main(String[] args) throws IOException {
int i=0;
File file=new File("D:/liuyifei.jpeg");
FileInputStream fileInputStream=new FileInputStream(file);
int available = fileInputStream.available();
FileOutputStream fileOutputStream=new FileOutputStream(new File("D:/liuyifei.txt"));
byte[] bytes=new byte[available];
while(fileInputStream.read()!=-1){
fileInputStream.read(bytes);
}
System.out.println(bytes);
fileOutputStream.write(bytes);
FileInputStream fileInputStream1=new FileInputStream(file);
byte[] bytes1=new byte[available];
fileInputStream1.read(bytes1);
FileOutputStream fileOutputStream1=new FileOutputStream(new File("D:/beautifuls.jpeg"));
fileOutputStream1.write(bytes1);
}
}
2、BufferedReader
import sun.misc.BASE64Decoder;
import java.io.*;
public class Base64 {
public static void main(String[] args) throws IOException {
String fileName = "img_base64.txt";
File file = new File(fileName);
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
StringBuffer stringBuffer = new StringBuffer();
String buffer;
while ((buffer = bufferedReader.readLine()) != null) {
stringBuffer.append(buffer);
}
BASE64Decoder base64decoder = new BASE64Decoder();
byte[] bytes = base64decoder.decodeBuffer(stringBuffer.toString());
FileOutputStream outputStream = new FileOutputStream(new File("genrerate.png"));
outputStream.write(bytes);
}
}
3、InputStreamReader
package src;
import java.io.*;
public class FileIO {
public static void main(String[] args) throws IOException {
int i=0;
File file=new File("D:/liuyifei.jpeg");
FileInputStream fileInputStream=new FileInputStream(file);
int available = fileInputStream.available();
/* FileOutputStream fileOutputStream=new FileOutputStream(new File("D:/liuyifei.txt"));
byte[] bytes=new byte[available];
while(fileInputStream.read()!=-1){
fileInputStream.read(bytes);
}
System.out.println(bytes);
fileOutputStream.write(bytes);
FileInputStream fileInputStream1=new FileInputStream(file);
byte[] bytes1=new byte[available];
fileInputStream1.read(bytes1);
FileOutputStream fileOutputStream1=new FileOutputStream(new File("D:/beautifuls.jpeg"));
fileOutputStream1.write(bytes1);*/
InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream);
while (inputStreamReader.read()!=-1){
System.out.println(inputStreamReader.read());
}
}
}