利用java输入流输入流拷贝图片(mac系统)
package com.hspedu.file.outputstream_;
import java.io.*;
public class FileCopy {
public static void main(String[] args) {
//完成文件拷贝
//思路分析
//1.创建文件的输入流,将文件读入到程序
//2.创建文件的输出流,将读取到的文件数据,写入到指定的文件
String srcFilePath = "/Users/leijinquan/Desktop/java/photo.png";
String destFilePatch = "/Users/leijinquan/Desktop/java/copy/photo.png";
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream(srcFilePath);
fileOutputStream = new FileOutputStream(destFilePatch);
//定义一个字节数组
byte [] buf = new byte[1024];
int readLen = 0;
while ((readLen = fileInputStream.read(buf)) != -1){
//读取到后就写入到文件 通过fileOutputStream
//即边读边写
fileOutputStream.write(buf,0,readLen);//一定要有这个方法
}
System.out.println("拷贝成功");
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭输入流输出流,释放资源
if (fileInputStream != null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream!=null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
演示java输入流
package com.hspedu.file.inputstream_;
import org.junit.jupiter.api.Test;
import java.io.FileInputStream;
import java.io.IOException;
//演示FileInputStream的使用
//单个字节读取,效率低
//使用readbyte读取
public class FileInputStream_ {
public static void main(String[] args) {
}
public void readFile01() {
String filePath = "/Users/leijinquan/Desktop/java/hello.txt";
int readData = 0;
FileInputStream fileInputStream = null;
try {
//创建FilInputStream对象,用于读取文件
fileInputStream = new FileInputStream(filePath);
//从该输入读取一个字节的数据,如果没有输入可用,此方法将被阻止
//如返回-1,表示读取完毕
while ((readData = fileInputStream.read()) != -1) {
System.out.print((char)readData);//转成char显示
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭文件流,释放资源
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void readFile02() {
String filePath = "/Users/leijinquan/Desktop/java/hello.txt";
//字节数组
byte[] buf = new byte[8];//一次读8个字节
int readLen = 0;
FileInputStream fileInputStream = null;
try {
//创建FilInputStream对象,用于读取文件
fileInputStream = new FileInputStream(filePath);
//从该输入流读取最多b.length字节的数据到字节数组,此方法将阻塞,直到某些输入可用
//如返回-1,表示读取完毕
//如果读取正常,返回实际读取的字节数
while ((readLen =(fileInputStream.read(buf))) != -1) {
System.out.print(new String(buf,0,readLen));
//显示这样就知道了String newStr = new String(array,0,index);的含义:
// 就是将数组array中 索引值从0开始 长度为index 的部分截取出来然后建立一个新的字符串newStr
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭文件流,释放资源
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
演示java输出流
package com.hspedu.file.outputstream_;
import org.junit.jupiter.api.Test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class FileOutputStream01 {
public static void main(String[] args) {
}
@Test
//演示使用FileOutputStream将数据写到文件中
//如果该文件不存在,则创建该文件
public void writeFile() {
//先创建 FileOutputStream对象
String filePath = "/Users/leijinquan/Desktop/java/a.txt";
FileOutputStream fileOutputStream = null;
try {
//得到 FileOutputStream
//1.new FileOutputStream(filePath)创建方式,写入内容是,会覆盖原来的内容
//2.new FileOutputStream(filePath,true);创建方式,当写入内容,是追加到文件后面
fileOutputStream = new FileOutputStream(filePath,true);
//写入一个字节
// fileOutputStream.write('H');//char ---> int
String str = "age";
//str.getBytes()可以把字符串转成字节数组
//fileOutputStream.write(str.getBytes());
fileOutputStream.write(str.getBytes(),0,str.length());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}