操作非文本文件 图片视频等等
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();
}
}
}
}
运行测试

文本文件
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();
}
}


该代码示例展示了如何使用Java进行文件操作,包括读取和复制非文本文件(如图片)以及读取文本文件。通过FileInputStream和FileOutputStream实现文件的输入输出,对图片进行复制,并使用同样的方式读取并打印文本文件内容。
625

被折叠的 条评论
为什么被折叠?



