Java 输入输出流,Java InputStream FileInputStream OutputStream FileOutputStream
================================
©Copyright 蕃薯耀 2021-04-20
https://www.cnblogs.com/fanshuyao/
一、字节流和文件流使用,实现文件(适用所有文件)复制
InputStream、FileInputStream :获取源文件
OutputStream、FileOutputStream:输出目标文件
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Stream1FileStream {
public static void fileInputStream() throws IOException {
InputStream is = new FileInputStream("c:/img/0.png");
OutputStream os = new FileOutputStream("c:/img/1.png");
byte[] buffer = new byte[1024];
int length;
while((length = is.read(buffer)) != -1) {
os.write(buffer, 0, length);
}
os.close();
is.close();
System.out.println("执行完成。");
}
public static void main(String[] args) throws IOException {
fileInputStream();
}
}
二、字节流和文件流使用,实现文件(文本文件)复制,无中文乱码问题
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Stream1FileStreamTxt {
public static void fileInputStream() throws IOException {
//字节流读取文件不会有中文乱码的问题
InputStream is = new FileInputStream("c:/img/0.txt");
OutputStream os = new FileOutputStream("c:/img/1-2.txt");
byte[] buffer = new byte[20];
int length;
while((length = is.read(buffer)) != -1) {
//System.out.println(new String(buffer, "GBK"));//这里直接输出会有中文乱码的问题(因为每次读取的字节数组存在将汉字分开的情况,这样就出现乱码,汉字不被分开就不会有乱码),但生成文件是没有中文乱码问题的
os.write(buffer, 0, length);
}
os.close();
is.close();
System.out.println("执行完成。");
}
public static void main(String[] args) throws IOException {
fileInputStream();
}
}
三、ByteArrayOutputStream字节数组使用
ByteArrayOutputStream的作用就是将多次读取到的所有字节都缓存起来,最后一起输入,能解决中文被截断后出现乱码的问题
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Stream1FileStreamTxtByteArray {
public static void fileInputStream() throws IOException {
//字节流读取文件不会有中文乱码的问题
InputStream is = new FileInputStream("c:/img/0.txt");//0.txt为GBK编码
OutputStream os = new FileOutputStream("c:/img/1-3.txt");
//将字节存储在ByteArrayOutputStream中,解决中文汉字截断后在控制台输出存在中文乱码的问题
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[20];
int length;
while((length = is.read(buffer)) != -1) {
os.write(buffer, 0, length);
byteArrayOutputStream.write(buffer, 0, length);
}
//System.out.println(byteArrayOutputStream.toString("GBK"));
System.out.println(new String(byteArrayOutputStream.toByteArray(), "GBK"));//处理中文乱码,GBK编码设置,取决于文件的编码,如果不是UTF-8,就必须设置,不然会有乱码
byteArrayOutputStream.close();
os.close();
is.close();
System.out.println("执行完成。");
}
public static void fileInputStreamUtf8() throws IOException {
//字节流读取文件不会有中文乱码的问题,但文件的编码一定要是UTF-8,不然在输出时就要指定相应的编码
InputStream is = new FileInputStream("c:/img/utf8.txt");//utf8.txt为UTF-8编码
OutputStream os = new FileOutputStream("c:/img/utf8-2.txt");
//将字节存储在ByteArrayOutputStream中,解决中文汉字截断后在控制台输出存在中文乱码的问题
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[20];
int length;
while((length = is.read(buffer)) != -1) {
os.write(buffer, 0, length);
byteArrayOutputStream.write(buffer, 0, length);
}
System.out.println(byteArrayOutputStream.toString());
byteArrayOutputStream.close();
os.close();
is.close();
System.out.println("执行完成。");
}
public static void main(String[] args) throws IOException {
//fileInputStream();
fileInputStreamUtf8();
}
}
四、字符流
字符流就是用来读取文件内容的,如txt,但这个一般都会有中文乱码
Reader、FileReader:输入
Writer、FileWriter:输出
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
public class Stream2FileReader {
public static void fileInputStream() throws IOException {
Reader r = new FileReader("c:/img/0.txt");//FileReader:直接使用会有中文乱码
Writer w = new FileWriter("c:/img/2.txt");
char[] buffer = new char[1024];
int length = 0;
while((length = r.read(buffer)) != -1) {
System.out.println(new String(buffer));
w.write(buffer, 0, length);
}
r.close();
w.close();
System.out.println("执行完成。");
}
public static void main(String[] args) throws IOException {
fileInputStream();
}
}
五、InputStreamReader:将字节流转换成字符流
将字节流转换成字符流,转换时,可以声明文件编码,解决中文乱码问题
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Stream3FileInputStreamReader {
public static void fileInputStream() throws IOException {
//使用转换流InputStreamReader解决中文乱码问题。InputStreamReader:将字节流inputStream转换成字符流Reader
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("c:/img/0.txt"), "GB2312");//InputStreamReader中的GB2312为具体文件的编码,默认是UTF-8的编码,不一致就会出现中文乱码
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("c:/img/3.txt"), "UTF-8");//生成新的文件,使用UTF-8编码
char[] buffer = new char[1024];
int length = 0;
while((length = inputStreamReader.read(buffer)) != -1) {
//System.out.println(new String(buffer));
outputStreamWriter.write(buffer, 0, length);
}
outputStreamWriter.close();
inputStreamReader.close();
System.out.println("执行完成。");
}
public static void main(String[] args) throws IOException {
fileInputStream();
}
}
六、字节缓冲流
BufferedInputStream、BufferedOutputStream:加快文件输入输出,小文件影响不大,但大文件影响很明显。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Stream4FileStreamBufferStream {
public static void fileInputStream() throws IOException {
long start = System.currentTimeMillis();
InputStream is = new FileInputStream("c:/img/00.mp4");
OutputStream os = new FileOutputStream("c:/img/11.mp4");
//缓冲流,加快文件输入输出
BufferedInputStream bufferedInputStream = new BufferedInputStream(is);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(os);
byte[] buffer = new byte[1024];
int length;
while((length = bufferedInputStream.read(buffer)) != -1) {
bufferedOutputStream.write(buffer, 0, length);
}
bufferedOutputStream.close();
bufferedInputStream.close();
os.close();
is.close();
long end = System.currentTimeMillis();
System.out.println("end - start=" + (end - start));//307 12767 14004
System.out.println("执行完成。");
}
public static void fileInputStreamBuffer() throws IOException {
long start = System.currentTimeMillis();
InputStream is = new FileInputStream("c:/img/00.mp4");
OutputStream os = new FileOutputStream("c:/img/22.mp4");
//缓冲流,加快文件输入输出
BufferedInputStream bufferedInputStream = new BufferedInputStream(is);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(os);
byte[] buffer = new byte[1024];
int length;
while((length = bufferedInputStream.read(buffer)) != -1) {
bufferedOutputStream.write(buffer, 0, length);
}
bufferedOutputStream.close();
bufferedInputStream.close();
os.close();
is.close();
long end = System.currentTimeMillis();
System.out.println("end - start=" + (end - start));//276 4307 7262 7130
System.out.println("执行完成。");
}
public static void main(String[] args) throws IOException {
//fileInputStream();
fileInputStreamBuffer();
}
}
七、字符缓存流
BufferedReader、BufferedWriter
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Stream5BufferedReader {
public static void fileInputStream() throws IOException {
//使用转换流InputStreamReader解决中文乱码问题。InputStreamReader:将字节流inputStream转换成字符流Reader
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("c:/img/0.txt"), "GB2312");//InputStreamReader中的GB2312为文件的编码,默认是UTF-8的编码,不一致就会出现中文乱码
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("c:/img/5.txt"), "UTF-8");//生成新的文件,使用UTF-8编码
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
char[] buffer = new char[1024];
int length = 0;
while((length = bufferedReader.read(buffer)) != -1) {
//System.out.println(new String(buffer));
bufferedWriter.write(buffer, 0, length);
}
bufferedWriter.close();
bufferedReader.close();
outputStreamWriter.close();
inputStreamReader.close();
System.out.println("执行完成。");
}
public static void main(String[] args) throws IOException {
fileInputStream();
}
}
八、Java 数据流
DataInputStream、DataOutputStream
用于数据存储,类似map的键值对,而且读写顺序要一致。
省略。
.
九、Java 对象流
ObjectInputStream、ObjectOutputStream
用于在网络中实现对象的传输,对象需要实现序列化接口。
省略。
注:
示例代码只是往外抛异常,没有使用Try…Catch…Finally,是不对的,只是为了演示代码整洁,实际是要使用Try…Catch…Finally来关闭流。

(如果文章对您有所帮助,欢迎捐赠,^_^)
================================
©Copyright 蕃薯耀 2021-04-20
https://www.cnblogs.com/fanshuyao/