public class FileUtils02 {
public static void main(String[] args) {
//文件到文件
try {
InputStream is = new FileInputStream("abc.txt");
OutputStream os = new FileOutputStream("abc-copy.txt");
copy(is, os);
} catch (IOException e) {
e.printStackTrace();
}
//文件到字节数组
byte[] datas = null;
try {
InputStream is = new FileInputStream("1.jpg");
ByteArrayOutputStream os = new ByteArrayOutputStream();
copy(is, os);
datas = os.toByteArray();
System.out.println(datas);
} catch (IOException e) {
e.printStackTrace();
}
//字节数组到文件
try {
InputStream is = new ByteArrayInputStream(datas);
OutputStream os = new FileOutputStream("1-copy.jpg");
copy(is, os);
} catch (IOException e) {
e.printStackTrace();
}
int g = 1;
}
/*
* 对接输入输出流
* try...with...resource
*/
public static void copy(InputStream is,OutputStream os) {
try(is;os) {
//3、操作(分段读取 )
byte[] flush = new byte[1024]; //缓冲容器
int len = -1; //接受长度
while ((len = is.read(flush))!=-1) {
os.write(flush, 0, len); //分段写出
}
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
其中这个地方显示is cannot be resolved to a type问题
新手学习代码感觉没有什么问题,但是不知道具体怎样解决,请路过的大神指点迷津,万分感谢!