FileInputStream与FileOutputStream
程序员文章站
2022-06-10 08:35:05
...
FileInputStream与FileOutputStream分别是InputStream与OutputStream的实现类。FileInputStream是文件输入流,用于将文件中的数据读入程序中。一般用于读取诸如图像数据之类的原始字节流。要读取字符流,考虑使用 FileReader。FileOutputStream 是文件输出流,一般用于写入诸如图像数据之类的原始字节的流。要写入字符流,考虑使用 FileWriter。
FileInputStream常用方法:
FileInputStream常用方法:
示例:
public static void test()throws Exception{
File file = new File("e:/test.txt");
File file1 = new File("e:/test.mp4");
FileInputStream inputStream = new FileInputStream(file);
FileOutputStream outputStream = new FileOutputStream(file1);
byte[] b = new byte[inputStream.available()];
int len = -1;
while((len = inputStream.read(b)) != -1){
outputStream.write(b,0,len);
}
inputStream.close();
outputStream.flush();
outputStream.close();
}
源码:
https://github.com/wolf521/demo/tree/master/src/main/java/com/example/demo/io