参考链接:
java IO体系的学习总结
https://blog.csdn.net/nightcurtis/article/details/51324105
Java 常用IO流操作详解
https://blog.csdn.net/MAGIC_JSS/article/details/51475923
Java中常见的IO流及其使用
https://blog.csdn.net/iispring/article/details/46821033
JavaIO流分类详解与常用流用法实例
https://blog.csdn.net/zhangliangzi/article/details/51226652
/**
* 方式一:一次读取一个字节(慢)
* FileInputStream
* FileOutputStream
* @param File or FileName
*
* 字节流,文件拷贝处理
*/
try {
String filePath1 = "1.txt";
String filePath2 = "2.txt";
File file1 = new File(filePath1);
File file2 = new File(filePath2);
//FileInputStream fin = new FileInputStream(filePath1);
//FileOutputStream fout = new FileOutputStream(filePath1);
FileInputStream fin = new FileInputStream(file1);
FileOutputStream fout = new FileOutputStream(file2);
int length = 0;
while ((length=fin.read())!=-1){
fout.write(length);
}
fin.close();
fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/**
* 方式二:一次读取一个字节数组(中)
* FileInputStream
* FileOutputStream
* @param File or FileName
*
* 字节流,文件拷贝处理
*/
try {
String filePath1 = "1.txt";
String filePath2 = "2.txt";
File file1 = new File(filePath1);
File file2 = new File(filePath2);
//FileInputStream fin = new FileInputStream(filePath1);
//FileOutputStream fout = new FileOutputStream(filePath1);
FileInputStream fin = new FileInputStream(file1);
FileOutputStream fout = new FileOutputStream(file2);
byte[] buf = new byte[1024];
int length = 0;
while ((length=fin.read(buf))>0){
fout.write(buf,0, length);
System.out.println(new String(buf,0,length));
fout.flush();//大文件可能持续阻塞
}
fin.close();
fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/**
* 方式三:利用缓冲流读取字节(快)
* FileInputStream
* FileOutputStream
* BufferedInputStream
* BufferedOutputStream
* @param File or FileName
*
* 字节流,文件拷贝处理
*/
try {
String filePath1 = "1.txt";
String filePath2 = "2.txt";
File file1 = new File(filePath1);
File file2 = new File(filePath2);
//FileInputStream fin = new FileInputStream(filePath1);
//FileOutputStream fout = new FileOutputStream(filePath1);
FileInputStream fin = new FileInputStream(file1);
BufferedInputStream bufFin = new BufferedInputStream(fin);
FileOutputStream fout = new FileOutputStream(file2);
BufferedOutputStream bufOut = new BufferedOutputStream(fout);
int length = 0;
while ((length=bufFin.read())!=-1){
bufOut.write(length);
}
bufFin.close();
bufOut.close();
fin.close();
fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/**
*
* ByteArrayInputStream
* ByteArrayOutputStream
* @param bytes 数组
*
* 字节流,转文件流
*/
try {
byte[] bytes = "I am iSpring".getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
ByteArrayOutputStream out = new ByteArrayOutputStream(bytes.length);
OutputStream outputStream = new FileOutputStream("3.txt");
byte[] b=new byte[1024];
int len = 0;
while ((len=in.read(b))!=-1){
out.write(b,0,len);
out.writeTo(outputStream);
System.out.println(new String(b,0,len));
out.flush();
}
in.close();
out.close();
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/**
* 传入字符串以读取字节
* StringBufferInputStream
* @param str
* 字节流
*/
String message = "I am iSpirng.";
StringBufferInputStream sbis = new StringBufferInputStream(message);
byte[] buf = new byte[1024];
try{
int length = sbis.read(buf);
if(length > 0){
System.out.println(new String(buf, 0, length, "UTF-8"));
}
sbis.close();
}catch (IOException e){
e.printStackTrace();
}
/**
*
* FileReader
* FileWriter
* BufferedReader
* BufferedWriter
* @param File or FileName 生成字节输入流(文件)
*
* 字符流,文件拷贝处理
*/
try {
FileReader reader = new FileReader("1.txt");
FileWriter writer = new FileWriter("2.txt");
BufferedReader bufferedReader = new BufferedReader(reader);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
char[] b=new char[1024];
int len = 0;
while ((len=bufferedReader.read(b))!=-1){
bufferedWriter.write(b,0,len);
System.out.println(new String(b,0,len));
bufferedWriter.flush();
}
bufferedReader.close();
bufferedWriter.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/**
*
* InputStreamReader
* OutputStreamWriter
* BufferedReader
* BufferedWriter
* @param File or FileName
*
* 字节流转字符流(转换流),文件拷贝处理
*/
try {
InputStreamReader reader = new InputStreamReader(new FileInputStream("1.txt"));
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("2.txt"));
BufferedReader bufferedReader = new BufferedReader(reader);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
char[] b=new char[1024];
int len = 0;
while ((len=bufferedReader.read(b))!=-1){
bufferedWriter.write(b,0,len);
System.out.println(new String(b,0,len));
bufferedWriter.flush();
}
bufferedReader.close();
bufferedWriter.close();
reader.close();
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/**
*
* StringReader
* StringWriter
* BufferedReader
* BufferedWriter
* @param str
*
* 字符流,文件拷贝处理
*/
try {
String str = "I am iSpring";
StringReader reader = new StringReader(str);
StringWriter writer = new StringWriter(str.length());
BufferedReader bufferedReader = new BufferedReader(reader);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
char[] b=new char[1024];
int len = 0;
while ((len=bufferedReader.read(b))!=-1){
bufferedWriter.write(b,0,len);
System.out.println(new String(b,0,len));
bufferedWriter.flush();
}
bufferedReader.close();
bufferedWriter.close();
reader.close();
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ObjectInputStream
PipedInputStream
SequenceInputStream
参考:https://blog.csdn.net/iispring/article/details/46821033
PrintStream
PushbackInputStream
参考:https://blog.csdn.net/zhangliangzi/article/details/51226652
DataInputStream
FilterInputStream
参考:https://blog.csdn.net/u013087513/article/details/52148934