读取磁盘文件输入流
// import java.io.*;
public static void readFile(String[] args){
// 获取文件
String filePath = "/home/README.md";
File file = new File(filePath);
// 借助 BufferedInputStream() 起到缓冲作用
try(InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"))){
String line;
StringBuilder sb = new StringBuilder();
while((line = br.readLine()) != null){
sb.append(line);
}
// stream handler ...
} catch(IOException e){
// IOException handler ...
}
}
输出(动态追加)文件
// import java.io.*;
public static void writeFile(String[] args) {
String outputFilePath = "/home/README.md";
try (FileOutputStream fileOutputStream = new FileOutputStream(outputfilePath, true);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream)) {
// 使用 str 字符串代替多文本文件
String str = "str ...";
bufferedOutputStream.write(sql.getBytes("UTF-8"), 0, str.getBytes().length);
bufferedOutputStream.write(str.getBytes("UTF-8"));
bufferedOutputStream.flush();
} catch (IOException e) {
// IOException handler ...
}
}
备注: .flush()
作用:
flush()
这个函数是清空的意思,用于清空缓冲区的数据流,进行流的操作时,数据先被读到内存中,然后再用数据写到文件中,那么当你数据读完时,我们如果这时调用close()
方法关闭读写流,这时就可能造成数据丢失,为什么呢,因为,读入数据完成时不代表写入数据完成,一部分数据可能会留在缓存区中,为了理解这个问题,我们举一个例子:
比如,在农村,几乎每家都有抽水机,抽水机的作用是什么呢,就是把水井里的水抽到水缸中,这时我们就会用水管连接抽水机和水缸(水管就好比是缓冲区),当我们想把水井中的水都抽到水缸中时,我们就让抽水机工作抽水,如果我们发现水井里的水刚好抽完时,我们就会关掉抽水机的开关停止抽水,那么这时,管道里就会遗留一部分水,抽水就是读数据,水缸进水就是写数据,水管充当缓存区的角色,不知道这样是不是具象化了呢
那么这样一来我们如果中途调用close()
方法,输出区也还是有数据的,就像水缸里有水,只是在缓冲区遗留了一部分,这时如果我们先调用flush()
方法,就会强制把数据输出,缓存区就清空了,最后再关闭读写流调用close()
就完成了。
- 参考:CSDN
说明: 在 try-catch-resource 语句中使用
new FileOutputStream(String name, boolean append)
构造函数实现文件动态追加功能。源码如下:
/**
* Creates a file output stream to write to the file with the specified
* name. If the second argument is <code>true</code>, then
* bytes will be written to the end of the file rather than the beginning.
* A new <code>FileDescriptor</code> object is created to represent this
* file connection.
* <p>
* First, if there is a security manager, its <code>checkWrite</code>
* method is called with <code>name</code> as its argument.
* <p>
* If the file exists but is a directory rather than a regular file, does
* not exist but cannot be created, or cannot be opened for any other
* reason then a <code>FileNotFoundException</code> is thrown.
*
* @param name the system-dependent file name
* @param append if <code>true</code>, then bytes will be written
* to the end of the file rather than the beginning
* @exception FileNotFoundException if the file exists but is a directory
* rather than a regular file, does not exist but cannot
* be created, or cannot be opened for any other reason.
* @exception SecurityException if a security manager exists and its
* <code>checkWrite</code> method denies write access
* to the file.
* @see java.lang.SecurityManager#checkWrite(java.lang.String)
* @since JDK1.1
*/
public FileOutputStream(String name, boolean append) throws FileNotFoundException {
this(name != null ? new File(name) : null, append);
}
BufferedImage 转 InputStream
将 ByfferedImage
转换成 InputStrean
输入流可以借助 ByteArrayOutputStream
。
先将 ByfferedImage
转换成 ByteArrayOutputStream
然后再调用 ByteArrayOutputStrean
的 .toByteArray()
方法转换成字节数组即可。
看一个示例,该示例是截取系统屏幕,将截取的图片转换成输入流:
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.io.*;
// ...
//获取全屏截图,截图类型为BufferedImage
BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
//创建储存图片二进制流的输出流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//将二进制数据写进ByteArrayOutputStream
ImageIO.write(image, "jpg", byteArrayOutputStream);
// ByteArrayOutputStream 转 InputStream
InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());