Java IO流操作汇总: inputStream 和 outputStream【转】

我们在进行Android java 开发的时候,经常会遇到各种IO流操作。IO流操作一般分为两类:字符流和字节流。以“Reader”结尾都是字符流,操作的都是字符型的数据;以“Stream”结尾的都是字节流,操作的都是byte数据。现将各种常见IO流总结如下:

一、字节流

1. InputStream 和 OutputStream

InputStream 和 OutputStream为各种输入输出字节流的基类,所有字节流都继承这两个基类。

2. FileInputStream 和 FileOutputStream

这两个从字面意思很容易理解,是对文件的字节流操作,也会最常见的IO操作流。

/*
* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
*/
public static void readFileByBytes(String inFile, String outFile) {
File file = new File(fileName);
InputStream in = null;
OutputStream out = null;
try {
byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream(inFile);
out = new FileOutputStream(outFile);
while ((byteread = in.read(tempbytes)) != -1) {
out.write(tempbytes, 0, byteread);
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
}
try {
out.close();
} catch (IOException e1) {
}
}
}
}

完全引用自:

Java IO流操作汇总: inputStream 和 outputStream--https://blog.csdn.net/wangbaochu/article/details/53484042

new Byte[big] 导致内存溢出java.lang.OutOfMemoryError: Java heap space

在读写文件时,如果我们一下就读取1G大小的new Byte[] ,由于内存不能自动释放,马上就会报Java heap space

内存溢出的解决思路--https://www.cnblogs.com/200911/p/3965108.html

上一篇:tomcat Server.xml Context配置


下一篇:ios下,对于position:fixed支持不完美的额解决方案