4.文件字节流(FileInputStream 和 FileOutputStream)
文件字节流输入流 FileInputStream 通过字节的方式读取文件,适合读取所有类型的文件(图像,视频,文本文件等),java也专门提供了FileReader读取文本文件
import java.io.FileInputStream;
public class Dome03 {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("d:/zm.png");
int temp = 0;
while ((temp = fis.read()) != -1) {
System.out.print(temp);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}


2.文件字节流-输出流
文件字节流输出流 FileOutputStream 把读取到的字节流写出到指定位置

import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Dome03 {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//创建字节流输入对象
fis = new FileInputStream("d:/zm.png");
//创建字节流输出对象
fos = new FileOutputStream("d:/dome.png");
int temp = 0;
while ((temp = fis.read()) != -1) {
fos.write(temp);//这一步只是把文件字节流写到了内存中
}
fos.flush();//这一步才是把文件字节流写到磁盘中
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}



浙公网安备 33010602011771号