操作非文本文件 图片视频等等
代码语言:javascript复制public class Test13 {
public static void main(String[] args) {
FileInputStream inputStream = null;
FileOutputStream outputStream = null;
//输入流
try {
//参数传入文件位置
inputStream = new FileInputStream("D://作业.png");
//输出流
outputStream = new FileOutputStream("D://作业副本.png");
//复制的过程
byte[] bytes = new byte[1024];
int len;
while ((len = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
运行测试
文本文件
代码语言:javascript复制 FileInputStream inputStream=null;
try {
inputStream = new FileInputStream("D://1.txt");
byte[] bytes = new byte[1024];
//记录每次读取的字节个数
int len;
while ((len=inputStream.read(bytes))!=-1){
String str = new String(bytes, 0, len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}